简体   繁体   English

Python Gtk Notebook焦点链

[英]Python gtk notebook focus chain

I'm trying to setup a window with a notebook and some pages. 我正在尝试设置一个带有笔记本和一些页面的窗口。 Within the pages, there will be some entries (see example code). 在页面内,将有一些条目(请参见示例代码)。

I'd like to handle the tab key and arrow keys on my own. 我想自己处理Tab键和箭头键。 I don't want the arrow up key to jump to the page title, I don't want the arrow left / right key to cycle through the pages. 我不希望向上箭头键跳到页面标题,也不希望向左/右箭头键在页面之间循环。 I don't want the tab key to cycle through the entries. 我不希望使用Tab键来遍历所有条目。

Connecting to the key-press signal and checking for tab or arrow keys does not really work. 连接到按键信号并检查选项卡或箭头键并不能真正起作用。

I tried to change the focus chain, but it still focusses on the page title or cycles through the pages with left / rigth arrow. 我试图更改焦点链,但是它仍然专注于页面标题或带有左/右箭头的页面循环显示。

Any help / idea is highly appreciated. 任何帮助/想法都受到高度赞赏。

#!/usr/bin/env python

import pygtk
import gtk

class NotebookExample:


    def __init__(self):
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.set_size_request(200,200)

        notebook = gtk.Notebook()
        notebook.set_tab_pos(gtk.POS_TOP)

        notebook.show()

        hbox1 = gtk.HBox()
        notebook.append_page(hbox1, gtk.Label("Page1"))
        hbox2 = gtk.HBox()
        notebook.append_page(hbox2, gtk.Label("Page2"))

        window.add(notebook)

        entry1 = gtk.Entry()
        entry2 = gtk.Entry()
        entry3 = gtk.Entry()
        entry1.set_width_chars(5)
        entry2.set_width_chars(5)
        entry3.set_width_chars(5)

        hbox1.pack_start(entry1, False, False, 10)
        hbox1.pack_start(entry2, False, False, 10)
        hbox1.pack_start(entry3, False, False, 10)

        hbox1.set_focus_chain([entry2, entry3])

        window.show_all()

def main():
    gtk.main()
    return 0

if __name__ == "__main__":
    NotebookExample()
    main()

Found it. 找到了。

Listening to the key-press-event and returning "True to stop other handlers from being invoked for the event. False to propagate the event further. 侦听按键事件,并返回“ True,以阻止为该事件调用其他处理程序。为False,则进一步传播事件。

        window.connect("key-press-event", self.do_key_press)

    def do_key_press(self, widget, event):
        if gtk.gdk.keyval_name(event.keyval) == 'Tab':
            print "tab"
            return True
        if gtk.gdk.keyval_name(event.keyval) == 'Left' or gtk.gdk.keyval_name(event.keyval) == 'Right':
            print "cursor"
            return True
        return

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

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