Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中关闭子窗口而不关闭主窗口PyGTK_Python_User Interface_Pygtk - Fatal编程技术网

在python中关闭子窗口而不关闭主窗口PyGTK

在python中关闭子窗口而不关闭主窗口PyGTK,python,user-interface,pygtk,Python,User Interface,Pygtk,我正在用PyGtk用python创建一个gui,我想创建一个从主窗口分支出来的子窗口,但与对话框不同的是,用户可以在不关闭子窗口的情况下与主窗口交互。@jcopens在我之前提出的关于如何实现子窗口的问题中向我演示了以下代码,我目前正在使用它来测试我的想法: from gi.repository import Gtk class AnotherWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self

我正在用PyGtk用python创建一个gui,我想创建一个从主窗口分支出来的子窗口,但与对话框不同的是,用户可以在不关闭子窗口的情况下与主窗口交互。@jcopens在我之前提出的关于如何实现子窗口的问题中向我演示了以下代码,我目前正在使用它来测试我的想法:

from gi.repository import Gtk

class AnotherWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="GCT")
        self.connect("destroy", lambda x: Gtk.main_quit())

        self.add(Gtk.Label("This is another window"))
        self.show_all()

class Main(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="GCT")
        self.connect("destroy", lambda x: Gtk.main_quit())

        self.box = Gtk.Box()
        self.set_default_size(300, 300)
        self.set_position(Gtk.WindowPosition.CENTER)
        self.table = Gtk.Table(6, 5)

        self.button = Gtk.Button("sub-window")
        self.button.connect("clicked", self.open_window)
        self.table.attach(self.button, 0, 2, 0, 1)

        self.box.add(self.table)
        self.add(self.box)
        self.show_all()

    def open_window(self, win):
        subw = AnotherWindow()


def main():
    m = Main()
    Gtk.main()
    return 0

if __name__ == '__main__':
    main()

当我运行此代码时,我能够打开我的子窗口,并且仍然能够像我所希望的那样与我的主窗口交互,但不幸的是,当我关闭我的子窗口时,主窗口会随之关闭。如何编写代码以仅关闭子窗口而不退出应用程序?

可以简单地隐藏窗口:

class AnotherWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="GCT")
        self.connect("destroy", self.on_destroy)

        self.add(Gtk.Label("This is another window"))
        self.show_all()

    def on_destroy(self, widget):
        widget.hide()

如果不再需要子窗口,也可以将其销毁。