0

我完成了一个应用程序设计,没有发生任何有用的事情,但我有两个窗口。一个是主窗口,一个是关于对话。它们都在 Glade 的一个 .ui 文件中。

当我使用帮助>关于(在我的程序中)打开关于对话框时,一切正常。使用相同的方法将其关闭并再次打开会导致空白窗口。

这是我的代码:

#! /usr/bin/env python3

from gi.repository import Gtk

class window():
    def __init__(self):
        self.builder = Gtk.Builder()

        self.builder.add_from_file("./personalinfo.ui")

        self.mainWindow = self.builder.get_object("mainWindow")

        self.mainWindow.connect("destroy", self.on_mainWindow_destroy)

        self.mainWindow.set_title("Persona")

        self.mainWindowMainBoxButtonBoxCancel = self.builder.get_object("mainWindowMainBoxButtonBoxCancel")

        self.mainWindowMainBoxButtonBoxCancel.connect("clicked", self.on_mainWindowMainBoxButtonBoxCancel_clicked)

        self.mainWindowMainBoxButtonBoxOK = self.builder.get_object("mainWindowMainBoxButtonBoxOK")

        self.mainWindowMainBoxButtonBoxOK.connect("clicked", self.on_mainWindowMainBoxButtonBoxOK_clicked)

        self.mainWindowMainBoxGenderBoxCombo  = self.builder.get_object("mainWindowMainBoxGenderBoxCombo")

        self.mainWindowMainBoxGenderBoxCombo.set_active(0)

        self.mainWindowMainBoxMenuFileMenuQuit = self.builder.get_object("mainWindowMainBoxMenuFileMenuQuit")

        self.mainWindowMainBoxMenuFileMenuQuit.connect("activate", self.on_mainWindowMainBoxMenuFileMenuQuit_activate)

        self.mainWindowMainBoxMenuHelpMenuAbout = self.builder.get_object("mainWindowMainBoxMenuHelpMenuAbout")

        self.mainWindowMainBoxMenuHelpMenuAbout.connect("activate", self.on_mainWindowMainBoxMenuHelpMenuAbout_activate)

        self.mainWindow.show_all()

    def on_mainWindow_destroy(self, widget):
        print("destroy: 'mainWindow'")

        Gtk.main_quit()

    def on_mainWindowMainBoxButtonBoxCancel_clicked(self, widget):
        print("clicked: 'mainWindowMainBoxButtonBoxCancel'")

        Gtk.main_quit()

    def on_mainWindowMainBoxButtonBoxOK_clicked(self, widget):
        print("clicked: 'mainWindowMainBoxButtonBoxOK'")

        Gtk.main_quit()

    def on_mainWindowMainBoxMenuFileMenuQuit_activate(self, widget):
        print("activate: 'mainWindowMainBoxMenuFileMenuQuit'")

        Gtk.main_quit()

    def on_mainWindowMainBoxMenuHelpMenuAbout_activate(self, widget):
        print("activate: 'mainWindowMainBoxMenuHelpMenuAbout'")

        self.aboutWindow = self.builder.get_object("aboutWindow")

        self.aboutWindow.set_title("About Persona")

        self.aboutWindowMainBoxButtonBoxOK = self.builder.get_object("aboutWindowMainBoxButtonBoxOK")

        self.aboutWindowMainBoxButtonBoxOK.connect("clicked", self.on_aboutWindowMainBoxButtonBoxOK_clicked)

        self.aboutWindow.show_all()

    def on_aboutWindowMainBoxButtonBoxOK_clicked(self, widget):
        print("clicked: 'aboutWindowMainBoxButtonBoxOK'")

        self.aboutWindow.destroy()

w = window()

Gtk.main()
4

1 回答 1

2

每次调用self.aboutWindow = self.builder.get_object("aboutWindow")时都不会创建 About 窗口,它会在加载 UI 文件时创建一次。因此,当您调用self.aboutWindow.destroy()它时,它将永远销毁它,并且下次您尝试显示该窗口时,它不再存在。

您可能不想破坏窗口,而是想隐藏它self.aboutWindow.hide()

于 2013-02-17T23:19:51.710 回答