demo.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #!/usr/bin/env python
  2. '''
  3. Sample-launcher application.
  4. '''
  5. # Python 2/3 compatibility
  6. from __future__ import print_function
  7. import sys
  8. # local modules
  9. from common import splitfn
  10. # built-in modules
  11. import webbrowser
  12. from glob import glob
  13. from subprocess import Popen
  14. try:
  15. import tkinter as tk # Python 3
  16. from tkinter.scrolledtext import ScrolledText
  17. except ImportError:
  18. import Tkinter as tk # Python 2
  19. from ScrolledText import ScrolledText
  20. #from IPython.Shell import IPShellEmbed
  21. #ipshell = IPShellEmbed()
  22. exclude_list = ['demo', 'common']
  23. class LinkManager:
  24. def __init__(self, text, url_callback = None):
  25. self.text = text
  26. self.text.tag_config("link", foreground="blue", underline=1)
  27. self.text.tag_bind("link", "<Enter>", self._enter)
  28. self.text.tag_bind("link", "<Leave>", self._leave)
  29. self.text.tag_bind("link", "<Button-1>", self._click)
  30. self.url_callback = url_callback
  31. self.reset()
  32. def reset(self):
  33. self.links = {}
  34. def add(self, action):
  35. # add an action to the manager. returns tags to use in
  36. # associated text widget
  37. tag = "link-%d" % len(self.links)
  38. self.links[tag] = action
  39. return "link", tag
  40. def _enter(self, event):
  41. self.text.config(cursor="hand2")
  42. def _leave(self, event):
  43. self.text.config(cursor="")
  44. def _click(self, event):
  45. for tag in self.text.tag_names(tk.CURRENT):
  46. if tag.startswith("link-"):
  47. proc = self.links[tag]
  48. if callable(proc):
  49. proc()
  50. else:
  51. if self.url_callback:
  52. self.url_callback(proc)
  53. class App:
  54. def __init__(self):
  55. root = tk.Tk()
  56. root.title('OpenCV Demo')
  57. self.win = win = tk.PanedWindow(root, orient=tk.HORIZONTAL, sashrelief=tk.RAISED, sashwidth=4)
  58. self.win.pack(fill=tk.BOTH, expand=1)
  59. left = tk.Frame(win)
  60. right = tk.Frame(win)
  61. win.add(left)
  62. win.add(right)
  63. scrollbar = tk.Scrollbar(left, orient=tk.VERTICAL)
  64. self.demos_lb = demos_lb = tk.Listbox(left, yscrollcommand=scrollbar.set)
  65. scrollbar.config(command=demos_lb.yview)
  66. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  67. demos_lb.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
  68. self.samples = {}
  69. for fn in glob('*.py'):
  70. name = splitfn(fn)[1]
  71. if fn[0] != '_' and name not in exclude_list:
  72. self.samples[name] = fn
  73. for name in sorted(self.samples):
  74. demos_lb.insert(tk.END, name)
  75. demos_lb.bind('<<ListboxSelect>>', self.on_demo_select)
  76. self.cmd_entry = cmd_entry = tk.Entry(right)
  77. cmd_entry.bind('<Return>', self.on_run)
  78. run_btn = tk.Button(right, command=self.on_run, text='Run', width=8)
  79. self.text = text = ScrolledText(right, font=('arial', 12, 'normal'), width = 30, wrap='word')
  80. self.linker = _linker = LinkManager(text, self.on_link)
  81. self.text.tag_config("header1", font=('arial', 14, 'bold'))
  82. self.text.tag_config("header2", font=('arial', 12, 'bold'))
  83. text.config(state='disabled')
  84. text.pack(fill='both', expand=1, side=tk.BOTTOM)
  85. cmd_entry.pack(fill='x', side='left' , expand=1)
  86. run_btn.pack()
  87. def on_link(self, url):
  88. print(url)
  89. webbrowser.open(url)
  90. def on_demo_select(self, evt):
  91. name = self.demos_lb.get( self.demos_lb.curselection()[0] )
  92. fn = self.samples[name]
  93. descr = ""
  94. try:
  95. if sys.version_info[0] > 2:
  96. # Python 3.x
  97. module_globals = {}
  98. module_locals = {}
  99. with open(fn, 'r') as f:
  100. module_code = f.read()
  101. exec(compile(module_code, fn, 'exec'), module_globals, module_locals)
  102. descr = module_locals.get('__doc__', 'no-description')
  103. else:
  104. # Python 2
  105. module_globals = {}
  106. execfile(fn, module_globals) # noqa: F821
  107. descr = module_globals.get('__doc__', 'no-description')
  108. except Exception as e:
  109. descr = str(e)
  110. self.linker.reset()
  111. self.text.config(state='normal')
  112. self.text.delete(1.0, tk.END)
  113. self.format_text(descr)
  114. self.text.config(state='disabled')
  115. self.cmd_entry.delete(0, tk.END)
  116. self.cmd_entry.insert(0, fn)
  117. def format_text(self, s):
  118. text = self.text
  119. lines = s.splitlines()
  120. for i, s in enumerate(lines):
  121. s = s.rstrip()
  122. if i == 0 and not s:
  123. continue
  124. if s and s == '='*len(s):
  125. text.tag_add('header1', 'end-2l', 'end-1l')
  126. elif s and s == '-'*len(s):
  127. text.tag_add('header2', 'end-2l', 'end-1l')
  128. else:
  129. text.insert('end', s+'\n')
  130. def add_link(start, end, url):
  131. for tag in self.linker.add(url):
  132. text.tag_add(tag, start, end)
  133. self.match_text(r'http://\S+', add_link)
  134. def match_text(self, pattern, tag_proc, regexp=True):
  135. text = self.text
  136. text.mark_set('matchPos', '1.0')
  137. count = tk.IntVar()
  138. while True:
  139. match_index = text.search(pattern, 'matchPos', count=count, regexp=regexp, stopindex='end')
  140. if not match_index:
  141. break
  142. end_index = text.index( "%s+%sc" % (match_index, count.get()) )
  143. text.mark_set('matchPos', end_index)
  144. if callable(tag_proc):
  145. tag_proc(match_index, end_index, text.get(match_index, end_index))
  146. else:
  147. text.tag_add(tag_proc, match_index, end_index)
  148. def on_run(self, *args):
  149. cmd = self.cmd_entry.get()
  150. print('running:', cmd)
  151. Popen(sys.executable + ' ' + cmd, shell=True)
  152. def run(self):
  153. tk.mainloop()
  154. if __name__ == '__main__':
  155. App().run()