Gtk对话框只出现一次



我正在用python和GTK(PyGoject(编写一个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

您需要覆盖它们关闭时发生的事情,这样它们就不会被销毁,而只是隐藏它们。您可以通过在destroy事件的对话框中添加一个事件处理程序来实现这一点,在对话框中只执行dialog_window.hide(),这样您就可以使用present重新显示它们。另外,不要忘记返回正确的布尔值以抑制进一步的事件传播。

最新更新