gpt4 book ai didi

python - pygtk 中的多线程

转载 作者:行者123 更新时间:2023-11-30 23:37:17 30 4
gpt4 key购买 nike

我正在尝试学习 pygtk,并且正在为 axel 开发 GUI。

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

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

  2. 我用了gobject.idel_add下载开始时它卡在主窗口

  3. 尝试使用Gtk.gdk.thread_init()/thread_enter()/thread_leave()它说没有模块gdkGtk 。在页面顶部Gtk是从 gi.repository 导入的

顺便说一句,我正在使用 quickly开发应用程序有什么办法可以解决这个问题吗?或任何类似的例子。

最佳答案

检查此线程 separate threads in pygtk application可能对你有用。这里给大家留下了一个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()

关于python - pygtk 中的多线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15718390/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com