在长时间的过程中与Tkinter窗口交互

时间:2013-08-04 00:22:36

标签: python user-interface tkinter

我有一个基本的python类,它使用标准Tkinter库创建一个窗口:

import Tkinter

class GUI(Tkinter.Tk):

    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def lock_func(self):
        while 1==1:
            print "blah"

    def initialize(self):
        self.processBtn = Tkinter.Button(self, text="Process", command=self.lock_func)
        self.processBtn.pack()        

app = GUI(None)
app.mainloop()

当我点击Process按钮时,窗口没有响应。 我希望能够在lock_func运行时关闭程序(使用x按钮)。

3 个答案:

答案 0 :(得分:4)

您可以使用generator来保持循环中的状态,并使用yield将控制权交还给主循环。然后使用self.after重复调用生成器的next方法来模拟while True的效果 - 但是这样做对Tkinter的主循环很友好。

import Tkinter as tk

class App(object):
    def __init__(self, master):
        self.master = master
        self.initialize()

    def lock_func(self):
        def step():
            while True:
                print("blah")
                self.nextstep_id = self.master.after(1, nextstep)
                yield
        nextstep = step().next
        self.nextstep_id = self.master.after(1, nextstep)

    def stop(self):
        self.master.after_cancel(self.nextstep_id)
        print("stopped")

    def initialize(self):
        self.nextstep_id = 0
        self.process_button = tk.Button(self.master, text="Process",
                                        command=self.lock_func)
        self.stop_button = tk.Button(self.master, text="Stop",
                                     command=self.stop)        
        self.process_button.pack()
        self.stop_button.pack(expand='yes', fill='x')

root = tk.Tk()
app = App(root)
root.mainloop()

答案 1 :(得分:3)

您可以使用window.update()方法,每次更改GUI时,都可以保持GUI的活动和功能。在根mainloop期间,这会自动发生,但如果您延长主循环,那么手动执行它可能是个好主意。将window.update()置于需要一段时间的循环中。 注意:windowTk()对象

答案 2 :(得分:0)

一种方法是使用线程:

import Tkinter
import thread

class GUI(Tkinter.Tk):

    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def lock_func(self):
        while 1==1:
            print "blah"

    def initialize(self):
        self.processBtn = Tkinter.Button(self, text="Process", command=lambda: thread.start_new_thread(self.lock_func, ()))
        self.processBtn.pack()        

app = GUI(None)
app.mainloop()

但是,在关闭Python控制台之前,它会一直打印。

要停止它,您可以使用另一个更改变量的按钮:

import Tkinter
import thread

class GUI(Tkinter.Tk):

    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.shouldPrint = True
        self.initialize()

    def lock_func(self):
        while self.shouldPrint:
            print "blah"

    def setShouldPrint(self, value):
        self.shouldPrint = value

    def initialize(self):
        self.processBtn = Tkinter.Button(self, text="Process", command=lambda: thread.start_new_thread(self.lock_func, ()))
        self.stopBtn = Tkinter.Button(self, text = "Stop", command = lambda: self.setShouldPrint(False))
        self.processBtn.grid(row = 1)
        self.stopBtn.grid(row = 2)


app = GUI(None)
app.mainloop()
相关问题