【问题标题】:Gtk Dialogs appear only onceGtk 对话框只出现一次
【发布时间】:2020-12-18 16:19:54
【问题描述】:

我正在用 python 和 GTK (PyGobject) 编写一个 GUI 应用程序。这是我的应用程序类:

class Application(Gtk.Application):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, application_id='org.tractor.carburetor', **kwargs)
        self.window = None
        self.prefs = None
        self.about = None

    def do_startup(self):
        Gtk.Application.do_startup(self)

        action = Gio.SimpleAction.new('preferences', None)
        action.connect('activate', self.on_preferences)
        self.add_action(action)

        action = Gio.SimpleAction.new('about', None)
        action.connect('activate', self.on_about)
        self.add_action(action)

        action = Gio.SimpleAction.new("quit", None)
        action.connect('activate', self.on_quit)
        self.add_action(action)

    def do_activate(self):
        if not self.window:
            window = AppWindow(application=self) #GtkApplicationWindow
            self.window = window
        self.window.present()

    def on_preferences(self, action, param):
        if not self.prefs:
            prefs_window = ui.get('PreferencesWindow') #HdyPreferencesWindow
            prefs_window.set_transient_for(self.window)
            self.prefs = prefs_window
        self.prefs.show()

    def on_about(self, action, param):
        if not self.about:
            about_dialog = ui.get('AboutDialog') #GtkAboutDialog
            about_dialog.set_transient_for(self.window)
            self.about = about_dialog
        self.about.show()

    def on_quit(self, action, param):
        self.quit()

当我在应用程序菜单中单击首选项或关于时,一切都很好。但是关闭对话框后,如果我再次单击它们,则会出现错误并出现一个空窗口。

以下是错误:

(carburetor:157852): Gtk-CRITICAL **: 19:41:29.887: gtk_widget_show: assertion
'GTK_IS_WIDGET (widget)' failed
(carburetor:157852): Gtk-CRITICAL **: 19:41:29.887: gtk_label_set_markup:
assertion 'GTK_IS_LABEL (label)' failed

【问题讨论】:

    标签: python user-interface gtk gtk3 pygobject


    【解决方案1】:

    您需要覆盖它们关闭时发生的情况,以便它们不会被破坏,而是简单地隐藏它们。您可以通过在 destroy 事件的对话框中添加一个事件处理程序来做到这一点,并且只需执行 dialog_window.hide() 以便您可以使用 present 重新显示它们。也不要忘记返回正确的布尔值以抑制进一步的事件传播。

    【讨论】:

    • 我设法用“hide()”解决了“关于对话框”的问题,这取决于“run()”的“响应”。但首选项窗口不是对话框,也没有“run()”方法。
    • 捕获“删除事件”并将其定义为隐藏窗口。诀窍是“返回 True”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2010-12-14
    相关资源
    最近更新 更多