2

I searched for some example about how to do what's in the title. basically I wrote a python program using gobject and Gtk, I have a notebook with 4 pages inside the main window.

I love to have the notebook change pages using <ALT>+1 <ALT>+2 <ALT>+3 <ALT>+4 as well as using mouse scrolling. I googled for a while but I did not find a proper example.

if someone have some advice on this subject I really apreciate some help

PS: Examples in any language are fine

4

2 回答 2

2

首先我用谷歌搜索,我找到了你的问题。

然后我用谷歌搜索了更多。这个资源GtkWidget将我指向'scroll-event'。那里说:

要接收此信号,与小部件关联的 GdkWindow 需要启用 GDK_SCROLL_MASK 掩码。

我按照链接GDK_SCROLL_MASK找到了另一个面具GDK_SMOOTH_SCROLL_MASK

所以这里是解决方案:

from gi.repository import Gtk, Gdk

...


class MainWindow(Gtk.Window):

    def __init__(self):
        ...
        self.notebook = Gtk.Notebook()
        self.notebook.add_events(Gdk.EventMask.SCROLL_MASK |\
                                 Gdk.EventMask.SMOOTH_SCROLL_MASK)
        self.notebook.connect('scroll-event', self.aaa)

    def aaa(self, widget, event):
        # I used `dir(event)` to find out `get_scroll_deltas()`
        if event.get_scroll_deltas()[2] < 0:
            self.notebook.prev_page()
        else:
            self.notebook.next_page()

祝你今天过得愉快 :-)

于 2013-11-26T16:14:51.743 回答
2

这将解决选项卡编号切换问题。

from gi.repository import Gtk

tabs = Gtk.Notebook()
tabs.set_scrollable(True)

button = Gtk.Button()
button.set_label('hi!')
button.set_relief(Gtk.ReliefStyle.NONE)
#here we use a grid to allow more widgets in the tab label.
box = Gtk.Grid(orientation=Gtk.Orientation.HORIZONTAL)
box.add(button)
box.show_all()

#a general tab number switcher
def on_this_tab_clicked(button, tabs, pagenum):
    tabs.set_current_page(pagenum)
#any button on a tab needs numbers in its callback argument
#as well as a argument for the tab object(Gtk.Notebook)
button.connect("clicked", on_this_tab_clicked, tabs, 0)

#Here we add  the acclerator.
accels = Gtk.AccelGroup()
#parse it so it's easy to read
key, mod = Gtk.accelerator_parse("<Alt>1")
button.add_accelerator("activate", accels, key, mod, Gtk.AccelFlags.VISIBLE)

tab_content = Gtk.Label()
tab_content.set_text('hi!')
#place the box(Gtk.Grid) in the second argument of append_page
tabs.append_page(tab_content, box)
tabs.set_tab_reorderable(tab_content, True)

#do everything with successive buttons exactly like the first button
button2 = Gtk.Button()
button2.set_label('bye!')
button2.set_relief(Gtk.ReliefStyle.NONE)
box2 = Gtk.Grid(orientation=Gtk.Orientation.HORIZONTAL)
box2.add(button2)
box2.show_all()

button2.connect("clicked", on_this_tab_clicked, tabs, 1)
key2, mod2 = Gtk.accelerator_parse("<Alt>2")
button2.add_accelerator("activate", accels, key2, mod2, Gtk.AccelFlags.VISIBLE)

tab_content2 = Gtk.Label()
tab_content2.set_text('bye!')
tabs.append_page(tab_content2, box2)
tabs.set_tab_reorderable(tab_content2, True)

window = Gtk.Window()
window.set_default_size(200, 200)
window.add(tabs)

window.add_accel_group(accels)
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()

这里有一些更好的链接。 Gtk.Notebook set_current_page Gtk Accelerators 这些链接很好。只需使用和翻译任何类似以下内容的内容:

gtk_function_name(对象,参数)

进入:

gtk.object.function(args)

于 2012-08-04T20:39:23.087 回答