-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive_tkwindow.py
127 lines (115 loc) · 4.61 KB
/
live_tkwindow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
from PIL import Image, ImageTk
import tkinter
from tkinter import font as TKfont
from io import BytesIO
from os import _exit
class tkvariables:
#extra_button is a tuple of (name,command)
def __init__(self,name,tkvar,window,update,time_getter=None,extra_button=None) -> None:
self.name = name
self.tkvar = tkvar
self.getter = update
self.window = window
self.time_getter = time_getter
self.extra_button = extra_button
if time_getter is not None:
self.timetk = tkinter.IntVar()
def time_update(self):
self.timetk.set(self.time_getter())
self.window.after(1000,self.time_update)
def update(self):
self.tkvar.set(self.getter())
self.window.after(1000,self.update)
## update should call after method to update the value
class tkwindow:
def __init__(self,logger,fontsize,quithandlers:tuple=None) -> None:
self.window = tkinter.Tk()
self.window.geometry('{}x{}'.format(1000,1000))
self.window.title('Frame Viewer')
self.window.protocol("WM_DELETE_WINDOW", self.__onQuit)
self.__imageLabel = tkinter.Label(self.window,text="Waiting...",font=TKfont.Font(size=fontsize));
self.__imageLabel.place(x=0,y=0,relwidth=0.48,relheight=0.64)
self.__tkpi = None
self.__quithandlers = quithandlers
button = tkinter.Button(self.window,text="!!Force Stop!!",command=self.__onQuit,font=TKfont.Font(size=fontsize))
button.place(x=0,rely=0.8,relwidth=0.48,relheight=0.13)
self.window.after(100,self.__updateImageLabel)
self.logger = logger
def __updateImageLabel(self,direct=False):
if self.__tkpi is not None:
self.__imageLabel.config(image=self.__tkpi)
if direct != True:
self.window.after(500,self.__updateImageLabel)
def __onQuit(self):
self.__imageLabel.destroy()
self.window.destroy()
if self.__quithandlers is not None:
for handler in self.__quithandlers:
handler()
## Be carefuk of this _exit as it kill everthing in a bad way
_exit(0)
def start(self):
self.logger.info("Start GUI")
self.window.mainloop()
def updateImage(self,frame=None,image=None):
img = None
if frame is None and image is None:
self.__imageLabel.config(image=None,text="Waiting...")
return
elif frame is None:
img = image
else:
memoryFile = BytesIO(frame)
img = Image.open(memoryFile)
## resize image to fit frame size while keeping ratio
try:
self.__tkpi = ImageTk.PhotoImage(img)
self.__imageLabel.config(image=self.__tkpi)
self.logger.info("Update Image")
except:
pass
self.window.after_idle(self.__updateImageLabel, True);
class tkdialog:
## extratk ((lambda,bool(before is true,after is false)))
## Lambda: please call pack()
def __init__(self,title:str,label:tuple,default:tuple,extratk:tuple(tuple())=None,**kwargs):
self.input = list()
self.__window = tkinter.Tk()
self.__width = 300
self.__height = 150
for key,value in kwargs.items():
if key == 'width':
self.__width = value
elif key == 'height':
self.__height = value
self.__window.geometry('{}x{}'.format(self.__width,self.__height))
self.__window.title(title)
self.__entry = list()
if extratk is not None:
for item in extratk:
(func,before) = item
if before:
func()
# create an entry widget for user input
for txt,detxt in zip(label,default):
tkinter.Label(self.__window,text=txt).pack()
self.__entry.append(tkinter.Entry(self.__window))
self.__entry[-1].insert(0,detxt)
self.__entry[-1].pack()
if extratk is not None:
for item in extratk:
(func,before) = item
if not before:
func()
# create a button to submit the input
self.__button = tkinter.Button(self.__window, text="Submit", command=self.__submit)
self.__window.bind_all('<Return>',self.__submit)
self.__button.pack()
def start(self):
self.__window.mainloop()
def __submit(self,event=None):
# event use to catch <return> event
# get the input from the entry widget
for x in self.__entry:
self.input.append(x.get())
self.__window.destroy()