为什么加载 gtk 应用程序时会自动选择第一个工具栏按钮?
当我的 gtk 应用程序加载时,工具栏上的第一项会被自动选择(它会突出显示,按下回车键时会被按下)。 这只是一个小问题,但我希望默认情况下不选择任何内容。
这是一些基本的示例代码:
import gtk
import gtk.glade
window_tree = gtk.glade.XML('testtoolbar.glade')
gtk.main()
testtoolbar.glade
<?xml version="1.0"?>
<glade-interface>
<!-- interface-requires gtk+ 2.16 -->
<!-- interface-naming-policy project-wide -->
<widget class="GtkWindow" id="window1">
<property name="width_request">200</property>
<property name="visible">True</property>
<child>
<widget class="GtkToolbar" id="toolbar1">
<property name="visible">True</property>
<child>
<widget class="GtkToolButton" id="toolbutton1">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton1</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-new</property>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<widget class="GtkToolButton" id="toolbutton2">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton2</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-open</property>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>
When my gtk application loads, the first item on the toolbar is automatically selected (it is highlighted, it is pressed when it hit enter). This is just a minor problem, but I would prefer for nothing to be selected by default.
Here is some basic sample code:
import gtk
import gtk.glade
window_tree = gtk.glade.XML('testtoolbar.glade')
gtk.main()
testtoolbar.glade
<?xml version="1.0"?>
<glade-interface>
<!-- interface-requires gtk+ 2.16 -->
<!-- interface-naming-policy project-wide -->
<widget class="GtkWindow" id="window1">
<property name="width_request">200</property>
<property name="visible">True</property>
<child>
<widget class="GtkToolbar" id="toolbar1">
<property name="visible">True</property>
<child>
<widget class="GtkToolButton" id="toolbutton1">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton1</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-new</property>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
<child>
<widget class="GtkToolButton" id="toolbutton2">
<property name="visible">True</property>
<property name="label" translatable="yes">toolbutton2</property>
<property name="use_underline">True</property>
<property name="stock_id">gtk-open</property>
</widget>
<packing>
<property name="expand">False</property>
<property name="homogeneous">True</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能只是 GTK+ 将焦点交给某个小部件,如果可能的话,它喜欢让某个小部件获得焦点。
您可以使用
grab_focus()< /a>
将焦点移到其他地方,以获得所需的行为。 如果工具栏是您拥有的所有小部件,那么可能很难找到合适的焦点“目标”。This is probably just GTK+ handing the focus to some widget, it likes to have a widget focused if possible.
You can use
grab_focus()
to move the focus elsewhere, to get the desired behavior. If the toolbar is all the widgetry you have, it might be hard to find a suitable focus "target", though.gtk.ToolButton继承gtk.Bin。
所以按钮是一个容器,
不会起作用。
您需要
或对于工具栏中的每个按钮
gtk.ToolButton inherits gtk.Bin.
So button is a container and
will not work.
You need
or for every button in toolbar
您始终可以添加一个小部件并使其不可见,并将焦点转移到它。 尽管我并不认为这是一个问题,也不知道有其他人这样做过。
You can always add a widget and make it invisible, and transfer focus to it. Though I don't really find this to be an issue or know of anyone else who did.
它具有焦点,因为它是应用程序中的第一个小部件。 如果您使用glade,您可以将任何其他小部件的“具有焦点”设置为“是”,工具栏将在启动时自动失去焦点。
It has focus because it is the 1st widget in your app. If you use glade, you can set "Has focus" to 'Yes' to any other widget and the toolbar will automatically lose focus on startup.