简体   繁体   中英

python gtk+ events

I am working with python and gtk. There is a checker thread, which looks for variables. If all conditions are ok, it should raise an event to the gtk.main loop. The raised event should open a window and start some other things.

its something like this:

if x==True and y==True:
   raise event

def event:
   newWindow()

Please give a code example how to implement this

gtk events always have a target. You cannot send an event "to the main loop". Most targets are widgets. You can:

  1. Just create the window, instead of sending an event. The window will get events during creation which you can handle if you like.
  2. Create new new window before, but hide it, and send the event to the window.
  3. Create a pipe and set it up for reading (with glib.io_add_watch), then send a byte to it (or if it's fire-once, close it) and the listener will receive the event.

The first option makes most sense to me. I included the second just to tell you that it can be done (it most resembles what you ask for). The third may be useful if you are working with multiple threads or processes, and it is important to you which thread or process does the window creation.

So.. got it done by myself.

the code is easy:

import gobject

class XX(gobject.GObject):
    # define signal parameters
    __gsignals__ = { 'signal-name' : (gobject.SIGNAL_RUN_FIRST, gobject.SIGNAL_TYPE_NONE, ())}

    gobject.__init__(self)

    def __init__(self):
        # some code

    def function(self):
        # emit signal
        self.emit('signal-name')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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