如何从不敏感的小部件获得点击和其他信号?

2024-06-10 01:20:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从不敏感的小部件得到信号。但当小部件对widget.set_sensitive(False)代码行不敏感时,这个小部件的信号就不起作用了。如何确保不敏感部件的信号正常工作?在

示例

w = Gtk.Button("example")
w.set_sensitive(False)
w.connect("button-press-event", do_anything) # not work 

我只想冻结小部件。但是widget完全不能用这种方式工作。在


Tags: 代码eventfalse示例gtk信号部件example
2条回答

您必须将按钮打包到事件框中。在

e_b = Gtk.Eventbox()
button = Gtk.Button()
e_b.add(button)
e_b.set_above_child(True) #necessary for event handling
e_b.connect("button-press-event", do_anything)

然而,这让我想知道你想做什么。见@JoseFonte答案。在

这就是小部件不敏感的含义。如果是这样的话,它们就不会向事件发出信号,你也无法与它们进行交互。在

在某些情况下,您希望小部件不敏感,例如,想象一下身份验证对话框中的“登录”按钮,但是您只希望在用户名和密码都不为空的情况下允许登录。然后您可以检查条目内容,如果没有条目为空,则将“Login”设置为敏感。在

从Python API-Gtk.Widget set_sensitive方法:

Sets the sensitivity of a widget. A widget is sensitive if the user can interact with it. Insensitive widgets are “grayed out” and the user can’t interact with them. Insensitive widgets are known as “inactive”, “disabled”, or “ghosted” in some other toolkits.

相关问题 更多 >