简体   繁体   中英

manage 2 Tkinter Threads in python 2.7

I'm working on an application using Tkinter in Python 2.7, to simplify:

from Tkinter import *

class rootWindow(Thread):
    def __init__(self):
        Thread.__init(self)
        self.root = Tk()
        self.button = Button(self.root,text="Execute",command=self.exec)
        self.start()
        self.join()

    def run(self):
        self.root.mainloop()

    def exec(self):
        # instruction 1
        # instruction 2
        # ...
        # End instructions

the problem is the exec() function takes a time (because it modify's an excel file).

I want to run a second window (with a 'please wait' animation or progressbar or something) while the exec() function is doing it's job. So I created:

class waitWindow(Thread):
    def __init__(self):
        Thread._init__(self)
        self.wait = Tk()
        self.pb = ttk.Progressbar()
        self.pb.pack()
        self.start()

    def run(self):
        self.wait.after(16000, self.des)
        self.wait.mainloop()

    def des(self):
        self.wait.destroy()

and I called this Thread before executing the exec() function. The launching is good but I can't stop the second Thread after the 16000 ms I passed in the second thread.

Any ideas? How can I stop the second thread within 16000 ms? thank's in advance!

There are two problems with your code. The first is that all tkinter code must run in the same thread, and ideally that's the main thread. The code that processes the excel file is the code that should be in another thread.

Second, if you need more than one window, the second and subsequent windows need to be instances of Toplevel rather than Tk .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM