简体   繁体   English

执行def功能时,GUI冻结。 使用线程?

[英]GUI freezes when executing def function. Use threads?

I've made a small program which has 2 buttons and each does certain thing. 我制作了一个小程序,其中有2个按钮,每个按钮都有特定功能。 Here's a simplified version of the code. 这是代码的简化版本。

Thing is it works fine except that the button freezes and stays in a clicked position and whole GUI freezes until the command is completed. 一切正常,除了按钮冻结并停留在单击位置,并且整个GUI冻结直到命令完成为止。 As far as I know threads would be best to use in this situation, but I have no idea how to implement it in this example. 据我所知,在这种情况下最好使用线程,但是我不知道如何在此示例中实现它。

I use glade and pygtk for gui. 我将林间空地和pygtk用于gui。

    def do1:
        t = 2
        #do something
        time.sleep(t)
        #do something
        time.sleep(t)
def do2:
        t = 3
        #do something
        time.sleep(t)
        #do something
        time.sleep(t)

class we:
        wTree = None
        def __init__( self ):                
                self.wTree = gtk.glade.XML( "ui.glade" )

                dic = {
                        "on_buttonSone" : self.sone,
                        "on_buttonStwo" : self.stwo,
                }
                self.wTree.signal_autoconnect( dic )              
                gtk.main()

        def sone(self, widget):
                i = 0
                while i < 3:
                        t = 1
                        #do something
                        i += 1
                        time.sleep(t)           
                self.wTree.get_widget("entryResult").set_text("Done.")
        def stwo(self, widget):
                start = time.clock()
                array = ['A','B']
                adict = {'A':do1,'B':do2}
                for f in array:
                        adict[f]()
                end = time.clock()
                elapsed = end - start
                gg = round(elapsed,2)             
                self.wTree.get_widget("entryResult").set_text(str(gg))             

go=we()

pyGTK has a whole section of there FAQ devoted to threading . pyGTK的FAQ中有一整节专门讨论线程

20.6 seems like a good minimal example. 20.6似乎是一个很好的最小示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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