为什么销毁信号无法到达Gtk.ScrolledWindow的子级?

时间:2018-09-17 21:42:40

标签: python gtk gtk3

对于简单的GTK窗口,请考虑以下python代码:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

def on_destroy(widget):
    print("Destroying {}".format(widget))

def on_click(button, scroll):
    scroll.destroy()

scroll = Gtk.ScrolledWindow()
scroll.connect("destroy", on_destroy)

button = Gtk.Button.new_with_label("Self-destruct")
scroll.add(button)
button.connect("destroy", on_destroy)
button.connect("clicked", on_click, scroll)

window = Gtk.Window()
window.add(scroll)
window.connect("destroy", Gtk.main_quit)
window.set_size_request(200, 75)
window.show_all()
Gtk.main()

按下按钮时,我希望得到两条输出行,报告ScrolledWindow和按钮被破坏。实际上,我只收到一行有关ScrolledWindow的报告,destroy信号永远不会到达按钮。如果将ScrolledWindow替换为普通的Box,则销毁信号到达按钮。为什么会这样?

1 个答案:

答案 0 :(得分:0)

除了这个删除按钮的代码之外,我真的不能告诉您更多信息。希望您可以适应您的需求。该按钮似乎确实会从视口中脱离出来,并没有附着任何东西,因为该按钮的destroy处理程序在应用程序关闭时运行,并导致奇怪的错误。出于其他任何想法,将其发布到Gtk邮件列表中会很有趣。

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

def on_destroy(widget, viewport):
    print("Destroying {}".format(widget))
    viewport.get_child().destroy()

def on_button_destroy(widget):
    print("Destroying button {}".format(widget))

def on_click(button):
    scroll = button.get_parent().get_parent()
    scroll.destroy()

scroll = Gtk.ScrolledWindow()
button = Gtk.Button.new_with_label("Self-destruct")
scroll.add(button)
viewport = button.get_parent()
button.connect("destroy", on_button_destroy)
scroll.connect("destroy", on_destroy, viewport)
button.connect("clicked", on_click)

window = Gtk.Window()
window.add(scroll)
window.connect("destroy", Gtk.main_quit)
window.set_size_request(200, 75)
window.show_all()
Gtk.main()