簡體   English   中英

Gtk 對話框只出現一次

[英]Gtk Dialogs appear only once

我正在用 python 和 GTK (PyGobject) 編寫一個 GUI 應用程序。 這是我的應用程序 class:

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

當我在應用程序菜單中單擊首選項或關於時,一切都很好。 但是在關閉對話框后,如果我再次單擊它們,我會收到錯誤,並且會出現一個空的 window。

您需要覆蓋它們關閉時發生的情況,以免它們被破壞,而只是隱藏它們。 您可以通過向銷毀事件的對話框添加一個事件處理程序來做到這一點,並且只需執行dialog_window.hide()以便您可以使用 present 重新顯示它們。 也不要忘記返回正確的 boolean 以抑制進一步的事件傳播。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM