Tkinter嵌套主循环 [英] Tkinter nested mainloop
问题描述
我正在使用 tkinter/python 编写视频播放器,因此基本上我有一个可以播放视频的GUI.现在,我想实现一个停止按钮,这意味着我将拥有一个mainloop()
用于GUI,以及另一个嵌套的mainloop()
以播放/停止视频并返回到GUI启动窗口. 这里,据说:
I am writing a videoplayer with tkinter/python, so basically I have a GUI that can play a video. Now, I would like to implement a stop button, meaning that I would have a mainloop()
for the GUI, and another nested mainloop()
to play/stop the video and return to the GUI startup window. Here, it is said:
事件循环可以嵌套;可以从事件处理程序中调用mainloop.
Event loops can be nested; it's ok to call mainloop from within an event handler.
但是,我不了解如何实现此嵌套.有人可以给我提供一个这样的脚本的简单例子吗?
However, I do not understand how to implement this nesting. Can someone please provide me with a simple example of such a script?
这是我的代码的有效版本,因为看来我在做异国情调.当然,我是新手,所以可能是我误以为需要嵌套主循环.
Here is a working version of my code, since it seems that I am doing something exotic. Of course, I am a newbie, so it may be that I am mistaken about the necessity to have a nested mainloop.
#!/usr/bin/python
import numpy as np
from multiprocessing import Process, Queue
import cv2
import cv2.cv as cv
from PIL import Image, ImageTk
import Tkinter as tk
def image_capture(queue):
vidFile = cv2.VideoCapture(0)
while True:
flag, frame=vidFile.read()
frame = cv2.cvtColor(frame,cv2.cv.CV_BGR2RGB)
queue.put(frame)
cv2.waitKey(10)
def update_all(root, imagelabel, queue, process, var):
if var.get()==True:
im = queue.get()
a = Image.fromarray(im)
b = ImageTk.PhotoImage(image=a)
imagelabel.configure(image=b)
imagelabel._image_cache = b # avoid garbage collection
root.update()
root.after(0, func=lambda: update_all(root, imagelabel, queue, process, var))
else:
print var.get()
root.quit()
def playvideo(root, imagelabel, queue, var):
print 'beginning'
p = Process(target=image_capture, args=(task,))
p.start()
update_all(root, imagelabel, queue, p, var)
print 'entering nested mainloop'
root.mainloop()
p.terminate()
if var.get()==False:
im = ImageTk.PhotoImage(file='logo.png')
imagelabel.configure(image=im)
imagelabel._image_cache = im # avoid garbage collection
root.update()
var.set(True)
print 'finishing'
if __name__ == '__main__':
#initialize multiprocessing
task = Queue()
#GUI of root window
root = tk.Tk()
#the image container
image_label = tk.Label(master=root)
image_label.grid(column=0, row=0, columnspan=2, rowspan=1)
#fill label with image until video is loaded
bg_im = ImageTk.PhotoImage(file='logo.png')
image_label['image'] = bg_im
#frame for buttons
button_frame = tk.Frame(root)
button_frame.grid(column=0, row=1, columnspan=1)
#load video button and a switch to wait for the videopath to be chosen
load_button = tk.Button(master=button_frame, text='Load video',command=lambda: playvideo(root, image_label, task, switch))
load_button.grid(column=0, row=0, sticky='ew')
#Stop button
switch = tk.BooleanVar(master=root, value=True, name='switch')
stop_button = tk.Button(master=button_frame, text='Stop',command=lambda: switch.set(False))
stop_button.grid(column=0, row=1, sticky='ew')
#quit button
quit_button = tk.Button(master=button_frame, text='Quit',command=root.destroy)
quit_button.grid(column=0, row=2, sticky='ew')
root.mainloop()
推荐答案
这应该是Tkinter中嵌套主循环的示例:
This should be an example of a nested mainloop in Tkinter:
import Tkinter
def main():
print 'main'
t.mainloop()
print 'end main'
t = Tkinter.Tk()
b = Tkinter.Button(t, command = main)
b.pack()
t.mainloop()
每当您按下按钮时,就会执行一个新的主循环.
Whenever you hit the button a new mainloop is executed.
main
main
main
main
main
# now close the window
end main
end main
end main
end main
end main
这篇关于Tkinter嵌套主循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!