English 中文(简体)
PyGtk 胎面在主食之前一直没有。
原标题:PyGtk thread not running until main quits
  • 时间:2011-10-21 13:32:04
  •  标签:
  • python
  • gtk

现在,我一直试图将这一问题分几个小时,鼓励网络,阅读文件目录。 我试图单独地完成一项长期的任务,并展示在《倡议》中取得进展的进展。 新的胎面已经开始,但直到主要胎盘离去为止,我已打电话到<条码>Gdk.threads_init(,然后在<条码>上填写,我用<条码>对电离星号)和/code>。

下面的法典照搬了这一问题,点击纽顿没有效果,但一旦窗户关闭,主要休息室一开始,我看看第二线工作(即,我看到印刷台半部)。

class App(Gtk.Window):

    def __init__(self):
        super(App, self).__init__()
        self.connect("destroy", self.on_destroy)

        self.layout = Gtk.VBox()
        self.progress = Gtk.ProgressBar()
        self.layout.pack_start(self.progress, False, False, 0)

        self.set_size_request(100,100)

        self.go_button = Gtk.Button("Start")
        self.go_button.connect("clicked", self.do_work_subclass)
        self.layout.pack_start(self.go_button, False, False, 0)

        self.add(self.layout)
        self.show_all()



    def on_destroy(widget, event):
        Gtk.main_quit()

    def do_work(self, widget):
        def worker_thread():
            so_far = 0
            while so_far < 10:
                time.sleep(0.5)
                print("work so far: %s" % so_far)
                Gdk.threads_enter()
                try:
                    if so_far > 0:
                        self.progress.set_fraction(so_far / 10.0)
                finally:
                    Gdk.threads_leave()
                so_far += 1
        threading.Thread(target=worker_thread).start()


if __name__ == "__main__":
    app = App()
    Gdk.threads_init()
    Gdk.threads_enter()
    Gtk.main()
    Gdk.threads_leave() 

Could this be related to the fact that I am using Gtk3?

问题回答

And having spent hours trying to find an answer before posting a question I find the solution minutes after posting. Turns out (from here) that for GTK3 you need to call GLib.threads_init(), the code works with an initial from gi.repository import GLib and a GLib.threads_init() before the call to Gtk.main(), so a working version of the code looks like this:

from gi.repository import Gtk,Gdk, GLib
import threading 
import time

class App(Gtk.Window):

    def __init__(self):
        super(App, self).__init__()
        self.connect("destroy", self.on_destroy)

        self.layout = Gtk.VBox()
        self.progress = Gtk.ProgressBar()
        self.layout.pack_start(self.progress, False, False, 0)

        self.set_size_request(100,100)

        self.go_button = Gtk.Button("Start")
        self.go_button.connect("clicked", self.do_work)
        self.layout.pack_start(self.go_button, False, False, 0)

        self.add(self.layout)
        self.show_all()



    def on_destroy(widget, event):
        Gtk.main_quit()

    def do_work(self, widget):
        def worker_thread():
            so_far = 0
            while so_far < 10:
                time.sleep(0.5)
                print("work so far: %s" % so_far)
                Gdk.threads_enter()
                try:
                    if so_far > 0:
                        self.progress.set_fraction(so_far / 10.0)
                finally:
                    Gdk.threads_leave()
                so_far += 1
        threading.Thread(target=worker_thread).start()



if __name__ == "__main__":
    app = App()
    GLib.threads_init()
    Gdk.threads_init()
    Gdk.threads_enter()
    Gtk.main()
    Gdk.threads_leave()




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签