Re: [Tkinter-discuss] non-GUI event detection in Tkinter mainloop()
Thanks very much to all those who responded to me and thanks to my good buddy and Python wiz, Kirke. I've included my code below, which periodically checks if a file is changed, and if so, it changes the image displayed in a window. The trick (as suggested) was to use "after" to get the event to repeatedly call itself. In the code below this is showStatus.after() at the end of the checkFileMod() function. -- import Tkinter import os root = Tkinter.Tk() # initialize parameters checkFile = "C:/BTL/Code/GC/IPSi/test3.txt" # check if this file changed img1=Tkinter.PhotoImage(file='./img1.gif') # display image 1 img2=Tkinter.PhotoImage(file='./img2.gif') # display image 2 interval = 3000 # time [msec] between checking # label widget shows status of image being displayed showStatus = Tkinter.Label() showStatus.pack() # canvas widget displays image that changes when file changes canvas=Tkinter.Canvas(root, width=400, height=400) canvas.create_image(145,280, image=img1) canvas.bind("<Button-1>", quitThis) # bind left mouse button to quit app canvas.pack() # function quits application def quitThis(event): canvas.destroy() root.destroy() root.quit() # function changes canvas if file is modified def checkFileMod(): global imgNum global fileStatus, fileStatusB4 fileStatus = os.stat(checkFile) print "file status: ", fileStatus.st_mtime if fileStatus != fileStatusB4: print "CHANGED" if imgNum == 1: showStatus.config(text="img2") imgNum = 2 canvas.create_image(145,280, image=img2) canvas.pack() else: showStatus.config(text="img1") imgNum = 1 canvas.create_image(145,280, image=img1) canvas.pack() fileStatusB4 = fileStatus showStatus.after(interval, checkFileMod) # run with initial values before loop imgNum = 1 showStatus.config(text="img1") fileStatusB4 = os.stat(checkFile) # file status before #loop checkFileMod() showStatus.mainloop()
participants (1)
-
O'Gorman, Lawrence (Larry)