【问题标题】:Force grab keyboard focus pygtk强制抓取键盘焦点 pygtk
【发布时间】:2012-07-15 10:52:30
【问题描述】:

我使用 pygtk 创建了一个新窗口。我希望系统立即给它键盘焦点。我所做的大部分时间都有效,但是当一个窗口已经获得焦点时,我的新窗口将被忽略。有没有办法强制我的窗口进入键盘焦点?我用来打开窗口的代码是:

    self.window = gtk.Window()
    self.window.set_position(gtk.WIN_POS_CENTER)
    self.window.connect("key-press-event", self.keypress)
    self.window.connect("focus-out-event", self.cancel)
    self.window.connect("destroy", self.cancel)
    self.entry = gtk.Entry(200)
    button = gtk.Button("go")
    button.connect("clicked", self.command)
    box = gtk.HBox()
    box.add(self.entry)
    box.add(button)
    self.window.add(box)
    self.window.set_keep_above(True)
    self.window.show_all()
    self.window.window.focus()

【问题讨论】:

    标签: python gtk pygtk


    【解决方案1】:

    进行焦点调用时,窗口不可见。试试这个:

    def create_window(self):
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        self.window.set_position(gtk.WIN_POS_CENTER)
        self.window.connect("key-press-event", self.keypress)
        self.window.connect("focus-out-event", self.cancel)
        self.window.connect("destroy", self.cancel)
        self.entry = gtk.Entry(200)
        button = gtk.Button("go")
        button.connect("clicked", self.command)
        box = gtk.HBox()
        box.add(self.entry)
        box.add(button)
        self.window.add(box)
        self.window.set_keep_above(True)
        self.window.show_all()
        gtk.idle_add(self.bring_to_front)
    
    def bring_to_front(self):
        self.window.present()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-10
      • 1970-01-01
      • 2015-09-15
      相关资源
      最近更新 更多