python - How do I make a timer run in a Tkinter widget while a process, started by the widget, is running? -
so, used tkinter create widget allows user input information , click run button, begin running test defined elsewhere. here code. far perfected, prototype:
from tkinter import* import controller root = tk() #create labels label = label(text = "text don't need know") label.pack() remind = label(text = "more text don't need know") remind.pack() #create text fields name = entry(root) name.pack() name.insert(0, "name") name.focus_set() testname = entry(root) testname.pack() testname.insert(0, "test name") duration = entry(root) duration.pack() duration.insert(0, "duration in minutes") def runtest(): controller.main(testname.get(), name.get(), float(duration.get())) #create run button run = button(root, text = "run", fg = "red", width = 10, command = runtest) run.pack() root.mainloop()
so, here issue. once project implemented, duration set 1-4 hours. so, have countdown appear on widget, users can reference timer @ time see how long until data produced. problem test running, widget locks until complete. i've tried put on hold until finishes running test, wanted. doesn't @ point.
anybody have experience @ implementing this? thanks.
you'll need fork off work in runtest
. threading
module friend (e.g. from threading import thread
).
then rewrite runtest
method:
def runtest(): # pack arguments in tuple mainargs = (testname.get(), name.get(), float(duration.get())) # create thread object armed function , args call thread = thread(target=controller.main, args=mainargs) # launch thread.start() #and remember, never set state (directly or indirectly) separate threads without taking appropriate precautions!
Comments
Post a Comment