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