python - How to run a function in the background of tkinter -
i new gui programming , want write python program tkinter. want run simple function in background can influenced through gui.
the function counts 0 infinity until button pressed. @ least want do. have no idea how can run function in background, because mainloop() of tkinter has control time. , if start function in endless loop, mainloop() cannot executed , gui dead.
i return control mainloop() after each cycle, how can control mainloop() runapp-function without user-triggered event?
here sample code kills gui:
from tkinter import * class app: def __init__(self, master): frame = frame(master) frame.pack() self.button = button(frame, text="start", command=self.runapp) self.button.pack(side=left) self.hi_there = button(frame, text="restart", command=self.restart) self.hi_there.pack(side=left) self.runapp() def restart(self): print "now restarting..." def runapp(self): counter = 0 while (1): counter =+ 1 time.sleep(0.1)
you find answer in other question tkinter locks python when icon loaded , tk.mainloop in thread.
in nutshell, need have 2 threads, 1 tkinter , 1 background task.
Comments
Post a Comment