Histoscienology
Histoscienology

Reputation: 101

GUI with Tkinter, auto refresh an image

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()

I'm a beginner in python, biulding a GUI that will auto refresh an image taken by a webcam and do other stuff omitted here. The image is saved locally as the same name as in the code(C:/Code_data/captures/test1_raw.png). Here is my solution, but after I ran the code, the GUI appeared for a second and disappeared. What I'm doing wrong? I'm wondering if there is efficient way to do it?

Upvotes: 0

Views: 443

Answers (1)

Jonah Fleming
Jonah Fleming

Reputation: 1185

You need to call root.mainloop() in main(). You need to call this in every Tkinter app.

Upvotes: 1

Related Questions