如何在 PyGTK 中设置默认按钮?
我有一个非常简单的窗口,其中有 2 个按钮 - 一个用于取消,一个用于应用。 如何将应用按钮设置为默认按钮? (当我按回车键时,按下“应用”按钮)
但是,我想将焦点设置到第一个输入小部件(我不能在按钮上使用grab_focus())
有什么建议吗?
编辑: 在 wuub 回答之后,它的视觉效果很好。 但是,当我按下不同小部件中的按钮时,它不会运行默认按钮的回调。
示例代码:
import os, sys, pygtk, gtk
def run(button, window):
dialog = gtk.MessageDialog(window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "OK")
dialog.run()
dialog.destroy()
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
vbox = gtk.VBox(spacing = 10)
entry = gtk.Entry()
vbox.pack_start(entry)
button = gtk.Button(stock = gtk.STOCK_SAVE)
button.connect("clicked", run, window)
button.set_flags(gtk.CAN_DEFAULT)
window.set_default(button)
vbox.pack_start(button)
window.add(vbox)
window.show_all()
gtk.main()
EDIT2:必须运行每个可以激活默认小部件的输入
widget.set_activates_default(True)
I have very simple window where I have 2 buttons - one for cancel, one for apply. How to set the button for apply as default one? (When I press enter, "apply" button is pressed)
However, I want to set focus to the first input widget (I can't use grab_focus() on the button)
Any suggestions?
Edit:
After wuub's answer it works visually good. However, when I press the button in different widget, it doesn't run callback of the default button.
Example code:
import os, sys, pygtk, gtk
def run(button, window):
dialog = gtk.MessageDialog(window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "OK")
dialog.run()
dialog.destroy()
window = gtk.Window()
window.connect("destroy", gtk.main_quit)
vbox = gtk.VBox(spacing = 10)
entry = gtk.Entry()
vbox.pack_start(entry)
button = gtk.Button(stock = gtk.STOCK_SAVE)
button.connect("clicked", run, window)
button.set_flags(gtk.CAN_DEFAULT)
window.set_default(button)
vbox.pack_start(button)
window.add(vbox)
window.show_all()
gtk.main()
EDIT2: Every input which can activate default widget must be ran
widget.set_activates_default(True)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://www.pygtk .org/docs/pygtk/class-gtkdialog.html#method-gtkdialog--set-default-response
http://www.pygtk.org/docs/pygtk/class-gtkwindow.html#method-gtkwindow--set-default
http://www.pygtk.org/docs/pygtk/class-gtkdialog.html#method-gtkdialog--set-default-response
http://www.pygtk.org/docs/pygtk/class-gtkwindow.html#method-gtkwindow--set-default