用Tkinter的mainloop时插入循环用线程

import Thread

class Product():
    def __init__(self):
        pass

    def f1(self):
         while True:
             time.sleep(1)
             print(1)

    def f2(self):
         while True:
             time.sleep(2)
             print(2)

    def listen(self):                           # 不能用join(),否则到不了mainloop,Tkinter打不开。
          t = Thread(target=self.f1)
          t2 = Thread(target=self.f2)
          t.start()
          t2.start()


if __name__ == "__main__":
    window = tk.Tk()
    p = Product(window)
    p.listen()            
    window.mainloop()

你可能感兴趣的:(GUI)