demo.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. PY3 = sys.version_info[0] == 3
  9. # local modules
  10. from common import splitfn
  11. # built-in modules
  12. import webbrowser
  13. from glob import glob
  14. from subprocess import Popen
  15. if PY3:
  16. import tkinter as tk
  17. from tkinter.scrolledtext import ScrolledText
  18. else:
  19. import Tkinter as tk
  20. from ScrolledText import ScrolledText
  21. #from IPython.Shell import IPShellEmbed
  22. #ipshell = IPShellEmbed()
  23. exclude_list = ['demo', 'common']
  24. class LinkManager:
  25. def __init__(self, text, url_callback = None):
  26. self.text = text
  27. self.text.tag_config("link", foreground="blue", underline=1)
  28. self.text.tag_bind("link", "<Enter>", self._enter)
  29. self.text.tag_bind("link", "<Leave>", self._leave)
  30. self.text.tag_bind("link", "<Button-1>", self._click)
  31. self.url_callback = url_callback
  32. self.reset()
  33. def reset(self):
  34. self.links = {}
  35. def add(self, action):
  36. # add an action to the manager. returns tags to use in
  37. # associated text widget
  38. tag = "link-%d" % len(self.links)
  39. self.links[tag] = action
  40. return "link", tag
  41. def _enter(self, event):
  42. self.text.config(cursor="hand2")
  43. def _leave(self, event):
  44. self.text.config(cursor="")
  45. def _click(self, event):
  46. for tag in self.text.tag_names(tk.CURRENT):
  47. if tag.startswith("link-"):
  48. proc = self.links[tag]
  49. if callable(proc):
  50. proc()
  51. else:
  52. if self.url_callback:
  53. self.url_callback(proc)
  54. class App:
  55. def __init__(self):
  56. root = tk.Tk()
  57. root.title('OpenCV Demo')
  58. self.win = win = tk.PanedWindow(root, orient=tk.HORIZONTAL, sashrelief=tk.RAISED, sashwidth=4)
  59. self.win.pack(fill=tk.BOTH, expand=1)
  60. left = tk.Frame(win)
  61. right = tk.Frame(win)
  62. win.add(left)
  63. win.add(right)
  64. scrollbar = tk.Scrollbar(left, orient=tk.VERTICAL)
  65. self.demos_lb = demos_lb = tk.Listbox(left, yscrollcommand=scrollbar.set)
  66. scrollbar.config(command=demos_lb.yview)
  67. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  68. demos_lb.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
  69. self.samples = {}
  70. for fn in glob('*.py'):
  71. name = splitfn(fn)[1]
  72. if fn[0] != '_' and name not in exclude_list:
  73. demos_lb.insert(tk.END, name)
  74. self.samples[name] = fn
  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. loc = {}
  94. if PY3:
  95. exec(open(fn).read(), loc)
  96. else:
  97. execfile(fn, loc)
  98. descr = loc.get('__doc__', 'no-description')
  99. self.linker.reset()
  100. self.text.config(state='normal')
  101. self.text.delete(1.0, tk.END)
  102. self.format_text(descr)
  103. self.text.config(state='disabled')
  104. self.cmd_entry.delete(0, tk.END)
  105. self.cmd_entry.insert(0, fn)
  106. def format_text(self, s):
  107. text = self.text
  108. lines = s.splitlines()
  109. for i, s in enumerate(lines):
  110. s = s.rstrip()
  111. if i == 0 and not s:
  112. continue
  113. if s and s == '='*len(s):
  114. text.tag_add('header1', 'end-2l', 'end-1l')
  115. elif s and s == '-'*len(s):
  116. text.tag_add('header2', 'end-2l', 'end-1l')
  117. else:
  118. text.insert('end', s+'\n')
  119. def add_link(start, end, url):
  120. for tag in self.linker.add(url):
  121. text.tag_add(tag, start, end)
  122. self.match_text(r'http://\S+', add_link)
  123. def match_text(self, pattern, tag_proc, regexp=True):
  124. text = self.text
  125. text.mark_set('matchPos', '1.0')
  126. count = tk.IntVar()
  127. while True:
  128. match_index = text.search(pattern, 'matchPos', count=count, regexp=regexp, stopindex='end')
  129. if not match_index:
  130. break
  131. end_index = text.index( "%s+%sc" % (match_index, count.get()) )
  132. text.mark_set('matchPos', end_index)
  133. if callable(tag_proc):
  134. tag_proc(match_index, end_index, text.get(match_index, end_index))
  135. else:
  136. text.tag_add(tag_proc, match_index, end_index)
  137. def on_run(self, *args):
  138. cmd = self.cmd_entry.get()
  139. print('running:', cmd)
  140. Popen(sys.executable + ' ' + cmd, shell=True)
  141. def run(self):
  142. tk.mainloop()
  143. if __name__ == '__main__':
  144. App().run()