简体   繁体   English

Python Gtk 3.0:如何在笔记本中获取焦点

[英]Python Gtk 3.0: How to grab focus inside Notebook

I'm trying to set the focus to an Entry input field.我正在尝试将焦点设置到Entry输入字段。 If I put it inside a Box , I can set the focus via the grab_focus method.如果我把它放在Box中,我可以通过grab_focus方法设置焦点。 But if the Entry is inside a Notebook , it is not focused.但是,如果EntryNotebook内,则它没有焦点。

Example code:示例代码:

import gi

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


class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Simple Notebook Example")
        self.set_border_width(3)

        self.notebook = Gtk.Notebook()
        self.add(self.notebook)

        self.page1 = Gtk.Box()
        self.page1.set_border_width(10)
        self.page1.add(Gtk.Label(label="Default Page!"))
        self.notebook.append_page(self.page1, Gtk.Label(label="Plain Title"))

        self.note = Gtk.Entry()
        self.note.set_activates_default(True)
        self.note.set_text("")
        self.page1.add(self.note)

        self.page2 = Gtk.Box()
        self.page2.set_border_width(10)
        self.page2.add(Gtk.Label(label="A page with an image for a Title."))
        self.notebook.append_page(
            self.page2, Gtk.Image.new_from_icon_name("help-about", Gtk.IconSize.MENU)
        )

        self.note.grab_focus()  # does not work


win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

How can I focus self.note inside the notebook?如何将self.note集中在笔记本内?

Thanks for any help!谢谢你的帮助!

As it turns out , you can only grab focus to visible widgets. 事实证明,您只能将焦点放在可见的小部件上。

Calling grab_focus after show_all makes the example work:grab_focus之后调用show_all会使示例工作:

win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
win.note.grab_focus()
Gtk.main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM