用户界面上的按钮挂起



我在一些网站上发现了以下代码,这与我的问题类似。每当我按下用户界面上的按钮时,它就会挂起。请帮我解决这个问题。

import Tkinter
from Tkinter import *
import Tkinter as tk
import time
class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
    def initialize(self):
        self.grid()

        button = Tkinter.Button(self,text=u"Click me !",
                                command=self.OnButtonClick)
        button.grid(column=1,row=0)

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
    def OnButtonClick(self):
        for i in range(10):
            print 'deep'
            time.sleep(1)
    def OnPressEnter(self,event):
        self.labelVariable.set("You pressed enter !")
if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()

我相信你在追求这样的东西:

import Tkinter 
import time
class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
    def initialize(self):
        self.grid()

        button = Tkinter.Button(self,text=u"Click me !",
                                command=self.OnButtonClick)
        button.grid(column=1,row=0)

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        self.i = 0; #<- make counter 
    def OnButtonClick(self):            
        print 'deep'
        self.i += 1;
        if self.i==10: return #<1-- stop if we hit 10 iterations
        self.after(1000, self.OnButtonClick) #<- use this

    def OnPressEnter(self,event):
        self.labelVariable.set("You pressed enter !")
if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()

请查看标记的更改。基本上,最好使用after方法在给定的时间做一些事情,而不是阻塞整个tk窗口。因此,如果你想执行10次,只需让一些veriable保持计数器self.i,并使用self.after方法调用OnButtonClick

作为一种选择,您可以将循环放入一个单独的线程中。例如:

import Tkinter 
import time
import threading
class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
    def initialize(self):
        self.grid()

        button = Tkinter.Button(self,text=u"Click me !",
                                command=self.OnButtonClick)
        button.grid(column=1,row=0)

        self.grid_columnconfigure(0,weight=1)
        self.resizable(True,False)
        # define a thread, but dont start it yet.
        # start it when button is pressed.
        self.t = threading.Thread(target=self.do_in_loop)
    def do_in_loop(self):
        # this will be executed in a separate thread.
        for i in range(10):
            print i, 'deep'
            time.sleep(1)
    def OnButtonClick(self):
        # start the thread with the loop 
        # so that it does not block the tk.
        if not self.t.isAlive():
            self.t.start()

    def OnPressEnter(self,event):
        self.labelVariable.set("You pressed enter !")
if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
    app.mainloop()

最新更新