from __future__ import print_function
from Tkinter import *
from tkFont import Font
#import RPi.GPIO as GPIO
from subprocess import call
import time
from time import sleep
from PIL import Image
from PIL import ImageTk
###STARTING A CLASS###
class MyDialog(Frame):
def __init__(self, parent):
Frame.__init__(self,parent)
self.parent = parent
self.mouse_pressed = False
self.initUI()
delay = 1000
def initUI(self):
self.tkimg = [None]
self.img = None
self.parent.title("High Tatras")
self.pack(fill=BOTH, expand=1)
self.img = Image.open("C:/Code_data/captures/test1_raw.png")
self.img = Image.new('1', (100, 100), 0)
self.img = self.img.resize((400, 400), Image.ANTIALIAS)
self.tkimg[0] = ImageTk.PhotoImage(self.img)
#self.config(image=self.tkimg[0])
canvas = Canvas(self, width=400, height=800+20)
canvas.create_image(10, 10, anchor=NW, image=self.tkimg[0])
canvas.pack(fill=BOTH, expand=1)
self.update_idletasks()
self.after(delay, self.initUI())
def main():
root = Tk()
ex = MyDialog(root)
ex.initUI()
root.wait_window(ex.top)
if __name__ == '__main__':
main()
我是python的初学者,可以自动刷新网络摄像头拍摄的图像,并在此处省略其他内容。图像在本地保存为与代码中相同的名称(C:/Code_data/captures/test1_raw.png)。这是我的解决方案,但是在我运行代码之后,GUI出现了一秒钟并且消失了。我做错了什么?我想知道是否有效的方法吗?
答案 0 :(得分:1)
您需要在root.mainloop()
中致电main()
。你需要在每个Tkinter应用程序中调用它。