I have this main thread:
Gui.py
from gi.repository import Gtk, Gdk
import Process
import gobject
class gui():
def __init__(self):
self.window = Gtk.Window()
self.window.connect('delete-event', Gtk.main_quit)
self.box = Gtk.Box()
self.window.add(self.box)
self.label = Gtk.Label('idle')
self.box.pack_start(self.label, True, True, 0)
self.progressbar = Gtk.ProgressBar()
self.box.pack_start(self.progressbar, True, True, 0)
self.button = Gtk.Button(label='Start')
self.button.connect('clicked', self.on_button_clicked)
self.box.pack_start(self.button, True, True, 0)
self.window.show_all()
gobject.threads_init()
Gdk.threads_enter()
Gtk.main()
Gdk.threads_leave()
def working1():
self.label.set_text('working1')
t = Process.Heavy()
t.heavyworks1()
self.label.set_text('idle')
def on_button_clicked(self, widget):
Gdk.threads_enter()
working1()
Gdk.threads_leave()
if __name__ == '__main__':
gui = gui()
Run Code Online (Sandbox Code Playgroud)
This code will …
所以我正忙着编写一个应用程序,需要在一定时间后检查网站的更新,我正在使用python与Gtk +3
main.py文件
class Gui:
...
def on_update_click():
update()
app=Gui()
Gtk.main()
Run Code Online (Sandbox Code Playgroud)
update.py文件
def update():
#check site for updates
time.sleep(21600) #check again in 6hrs
Run Code Online (Sandbox Code Playgroud)
我怀疑我必须使用线程.我的想法是:
Gtk.main()运行主线程.
当用户单击更新按钮时,update()在后台运行.#thread 2
我的想法是正确的还是我错过了什么?
编辑:好的,
on_update_click函数:
Thread(target=update).start().
Run Code Online (Sandbox Code Playgroud)
K,电脑不再冻结:D
所以现在发生的事情是,只有当我关闭Gtk.main()时,更新线程才会启动.在UI关闭时继续更新很好,但我也希望在UI启动时启动它.