开发者

GTK: Destroy window and run an external script on button clicked

That's pretty much the question's title. I must be missing some signals interpretation here...

On PyGTK, I'm doing:

class Foo:

    def __init__(self):
        self.gladefile = gladefile
        self.wTree = gtk.glade.XML(self.gladefile, 'some_window')
        self.window = self.wTree.get_widget('some_window')
        events = { 'on_code_submit_clicked' : self.submit }
        self.wTree.signal_autoconnect(events)

    def submit(self):
        self.window.destroy()
        os.system('external_script')
        code = Foo()

What's happening, is that when the button is clicked, it stay pressed, then the script runs, and after the external program is closed, the window "blinks", getting destroyed and recreated again.

I also tried the "pressed" and "released" signals.

The behavior I need:

  1. Click on the button
  2. Destroy the current window
  3. Run external script (that will open another program's window)
  4. Recreate the Foo() window after closing the external a开发者_开发百科pp.

What I can imagine is that the event is being run during the clicked event, not after. That's why the window remain opened. The PyGTK docs don't say anything about something like gtk_signal_connect_after on the glade page, that leaves me totally lost about it.


Why not hide the window and show it after the external script?

self.window.hide()
os.system('external_script')
self.window.show()


It looks like you'll have to call the external script from a thread.

I haven't worked with threads with python, but it looks like the threading module would do the job (at least that's what I would try). Glib also has thread support, but I can't find the pygtk docs for it.

When the external script has finished, don't recreate Foo from the thread but schedule a callback function to do so using gobject.idle_add. This is because all GUI work should be done on the gtk event thread, otherwise your program may crash.


Posting my solution... it's easy as.

    self.window.hide()
    glib.idle_add(subprocess.call, ['external_script', 'param'])
    glib.idle_add(self.window.show)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜