Skip to content
Advertisement

‘Close window’ button wont work when using tkinter + gobject

My tkinter application runs fine, but to implement dbus functionality I had to use gobject. Got it working and all, except that, running both tkinter’s and gobject’s mainloops makes the “close window” button from the standard window manager (‘x’ button in the window interface) not to work. :/ Everything else works fine, including resizing, minimizing/maximizing, restoring, and moving the window.

Any help is appreciated,

Thanks,


Little code snippet:

import dbus
from dbus.service import Object
from dbus.mainloop.glib import DBusGMainLoop

class TBOPlayerDBusInterface (Object):
    tboplayer_instance = None

    def __init__(self, tboplayer_instance):
        self.tboplayer_instance = tboplayer_instance
        dbus_loop = DBusGMainLoop()
        bus_name = dbus.service.BusName("org.tboplayer.TBOPlayer", bus = dbus.SessionBus(mainloop = dbus_loop))
        Object.__init__(self, bus_name, "/org/tboplayer/TBOPlayer")

    @dbus.service.method('org.tboplayer.TBOPlayer', in_signature = 'as')
    def openFiles(self, files):
        self.tboplayer_instance._add_files(files)

# ***************************************
# MAIN
# ***************************************

if __name__ == "__main__":
    datestring=" 28 Fev 2017"

    dbusif_tboplayer = None
    try:
        bus = dbus.SessionBus()
        bus_object = bus.get_object("org.tboplayer.TBOPlayer", "/org/tboplayer>/TBOPlayer", introspect = False)
        dbusif_tboplayer = dbus.Interface(bus_object, "org.tboplayer.TBOPlayer")
    except Exception, e:
        print e

    if dbusif_tboplayer is None:
        tk.CallWrapper = ExceptionCatcher
        bplayer = TBOPlayer()
        TBOPlayerDBusInterface(bplayer)
        def refresh_player():
            bplayer.root.update()
            return True
        def run_gobject():
            gobject.MainLoop().run()
        gobject.idle_add(refresh_player)
        bplayer.root.after(100, run_gobject)
        bplayer.root.mainloop()
    else:
        if len(sys.argv[1:]) > 0:
            dbusif_tboplayer.openFiles(sys.argv[1:])
        exit()

Advertisement

Answer

I found the problem. For some reason, using tkinter’s and gobject’s mainloops interferes with the behavior of the WM_DELETE_WINDOW event, which I was using for saving some data before closing the program. Solved the problem by binding to the Configure event instead. And now the main method is as follows:

if __name__ == "__main__":
    datestring=" 28 Fev 2017"

    dbusif_tboplayer = None
    try:
        bus = dbus.SessionBus()
        bus_object = bus.get_object("org.tboplayer.TBOPlayer", "/org/tboplayer/TBOPlayer", introspect = False)
        dbusif_tboplayer = dbus.Interface(bus_object, "org.tboplayer.TBOPlayer")
    except Exception, e:
        print e

    if dbusif_tboplayer is None:
        tk.CallWrapper = ExceptionCatcher
        bplayer = TBOPlayer()
        TBOPlayerDBusInterface(bplayer)
        gobject_loop = gobject.MainLoop()
        def refresh_player():
            try:
                bplayer.root.update()
                return True
            except Exception, e:
                bplayer.quit_omx()
                gobject_loop.quit()
        def run_gobject():
            gobject_loop.run()
        gobject.idle_add(refresh_player)
        bplayer.root.after(100, run_gobject)
        bplayer.root.mainloop()
    else:
        if len(sys.argv[1:]) > 0:
            dbusif_tboplayer.openFiles(sys.argv[1:])
        exit()
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement