Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pygtk中的多线程_Python_Pygtk - Fatal编程技术网

Python pygtk中的多线程

Python pygtk中的多线程,python,pygtk,Python,Pygtk,我正在努力学习pygtk,并且正在为axel开发一个GUI 我的问题是我需要在主窗口中显示下载进度 我使用了两个线程,一个用于下载,另一个用于进度计算。下载由axel完成 但直到第一个线程停止,我的第二个线程才运行。第二个线程没有更新gui 我使用了gobject.idel\u add它在下载开始时会阻塞主窗口 试图使用Gtk.gdk.thread\u init()/thread\u enter()/thread\u leave()它在Gtk中表示没有模块gdk。页面顶部的Gtk是从gi.rep

我正在努力学习pygtk,并且正在为axel开发一个GUI

我的问题是我需要在主窗口中显示下载进度

  • 我使用了两个线程,一个用于下载,另一个用于进度计算。下载由axel完成 但直到第一个线程停止,我的第二个线程才运行。第二个线程没有更新gui

  • 我使用了
    gobject.idel\u add
    它在下载开始时会阻塞主窗口

  • 试图使用
    Gtk.gdk.thread\u init()/thread\u enter()/thread\u leave()
    它在
    Gtk
    中表示没有模块
    gdk
    。页面顶部的
    Gtk
    是从
    gi.repository

  • 顺便说一下,我正在使用
    快速
    开发应用程序 有办法解决这个问题吗。或任何类似的例子。

    检查此线程可能对您有用。 这里留给您一个pygtk管理多线程示例

    import threading
    import random, time
    import gtk
    #Initializing the gtk's thread engine
    gtk.threads_init()
    
    
    class FractionSetter(threading.Thread):
        """This class sets the fraction of the progressbar"""
    
        #Thread event, stops the thread if it is set.
        stopthread = threading.Event()
    
        def run(self):
            """Run method, this is the code that runs while thread is alive."""
    
            #Importing the progressbar widget from the global scope
            global progressbar 
    
            #While the stopthread event isn't setted, the thread keeps going on
            while not self.stopthread.isSet() :
                # Acquiring the gtk global mutex
                gtk.threads_enter()
                #Setting a random value for the fraction
                progressbar.set_fraction(random.random())
                # Releasing the gtk global mutex
                gtk.threads_leave()
    
                #Delaying 100ms until the next iteration
                time.sleep(0.1)
    
        def stop(self):
            """Stop method, sets the event to terminate the thread's main loop"""
            self.stopthread.set()
    
    def main_quit(obj):
        """main_quit function, it stops the thread and the gtk's main loop"""
        #Importing the fs object from the global scope
        global fs
        #Stopping the thread and the gtk's main loop
        fs.stop()
        gtk.main_quit()
    
    #Gui bootstrap: window and progressbar
    window = gtk.Window()
    progressbar = gtk.ProgressBar()
    window.add(progressbar)
    window.show_all()
    #Connecting the 'destroy' event to the main_quit function
    window.connect('destroy', main_quit)
    
    #Creating and starting the thread
    fs = FractionSetter()
    fs.start()
    
    gtk.main()