python - 关闭子窗口而不关闭主窗口Python中的PyGTK

标签 python user-interface pygtk

我正在使用 PyGtk 在 python 中创建一个 gui,我想创建一个从主窗口分支出来的子窗口,但与对话框不同的是,用户将能够与主窗口交互,而无需关闭子窗口- window 。 @jcoppens 在上一个问题中向我演示了以下代码,我询问了如何实现子窗口,我目前正在使用它来测试我的想法:

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

关于python - 关闭子窗口而不关闭主窗口Python中的PyGTK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30991885/

相关文章:

python - 我如何在 SQL 查询中伪造这些数据

java - Windows Mobile 的 SWT : UI Architecture

java - 如何为 JPanel 网格使用图像?

python - pygtk 仅在一个 pixbuf 中加载图像流

python - 我如何将 gtk 模块导入我的应用程序? Python

python - 在 GTK 中嵌入 chaco

python csv阅读器不处理引号

python - 如何使用 Python 在 SPSS 中设置可变宽度和小数?

python - C/Python 中的 asn.1 解析器

user-interface - 我们可以从 Neo4j 的属性图可视化模型生成 Cypher 查询吗?