使用tkinter + gobject时,“关闭窗口”按钮不起作用

时间:2017-02-28 13:30:36

标签: linux python-2.7 tkinter dbus gobject

我的tkinter应用程序运行正常,但要实现dbus功能,我必须使用gobject。让它工作和所有,除了,运行tkinter和gobject的mainloops使标准窗口管理器(窗口界面中的'x'按钮)的“关闭窗口”按钮不起作用。 :/其他一切正常,包括调整大小,最小化/最大化,恢复和移动窗口。

感谢任何帮助,

谢谢,

小代码段:

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()

1 个答案:

答案 0 :(得分:0)

我发现了问题。出于某种原因,使用tkinter和gobject的mainloops会干扰WM_DELETE_WINDOW事件的行为,我在关闭程序之前用它来保存一些数据。通过绑定到Configure事件来解决问题。现在主要方法如下:

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()