multithreading - Python app with tkinter and threading error at closing -
i writing software project homework, having trouble mixing threads , tkinter. following piece works expected, when close (after starting in python shell), windows shows error: "python stopped working".
import threading import time import tkinter import tkinter.ttk class btclient: def __init__(self, master): self.root = master self.root.bind("<destroy>", self.on_destroy) self.t = threading.thread(target=self.update) self.running = false def on_destroy(self, event): self.running = false def run_thread(self): self.running = true self.t.start() def update(self): while self.running: print("update.") time.sleep(1) def main(args): root = tkinter.tk() client = btclient(root) client.run_thread() root.mainloop() if __name__ == "__main__": import sys main(sys.argv)
how can solve problem? caused design using? should change it?
edit 1: when remove self.root
declaration in __init__
, use master
reference problem solved, need have references gui objects, first build gui , input them, don't know how solve that. maybe passing objects arguments may need them?
its hard know whats happening without seeing actual code, make threaded method function outside of btclient , move thread outside of client , add call thread.join() after mainloop have thread finish cleanup before btclient deleted gc
def update(): import time while thread._keep_alive: time.sleep(1) print("thread running") thread = threading.thread(target=update) thread._keep_alive = true thread.start()
at end
thread._keep_alive = false thread.join()
Comments
Post a Comment