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

andyinno
  • 1,021
  • 2
  • 9
  • 24
  • hey, man! I'm now tryng to do exactly the same thing. Have you managed to solve this problem? – Elias Dorneles Apr 14 '13 at 20:14
  • I've tried to set up accelerators for the labels used in the notebook, but it does not help. I'll try to dig more into the docs to see what else can I try. – Elias Dorneles Apr 14 '13 at 20:46
  • Hello! sorry but I left this thing behind because I wasn't able to find a solution to this. I was doing a small project for internal utility and didn't have too much time to spend on this. Maybe in the future I'll have the same problem. The strange thing is that I see it working in different programs... Gnome terminal and many others. I was hoping about it was a feature internal to gtk+. Probably it isn't. – andyinno Apr 17 '13 at 15:34

2 Answers2

2

This will solve the tab number switching problem.

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()

Here are some better links for this. Gtk.Notebook set_current_page Gtk Accelerators These links are good. Just use and translate anything that says something like:

gtk_function_name(object, args)

into:

gtk.object.function(args)

Quentin Engles
  • 2,744
  • 1
  • 20
  • 33
  • Sorry for the downvote, but your answer really does not suffice to solve the OP's problem. Your second link actually points to a method that adds accelerators for action items - menus and toolbar items, not gtk.Notebook tabs. – Elias Dorneles Apr 14 '13 at 20:44
  • Wow. I had forgotten about this. – Quentin Engles Apr 14 '13 at 21:37
  • Your welcome. I still have not tried wheel scrolling with gtk notebook. From what I've read there seems to be a problem with the implementation in gtk3. There has been a history of bugs with capturing the scroll wheel event. I'm not complaining. gtk is cool. Just another little thing. :) – Quentin Engles Apr 14 '13 at 23:39
  • @elias It might seem like a lot to get some simple functionality, but I've made a bigger mess trying to shoehorn other special functions into gtk3. It should work well for a tab class in a module. – Quentin Engles Apr 15 '13 at 00:17
  • I agree, it really seems a lot for very little... If you're interested, what I'm trying to do is to add support for alt-N shortcuts in [guake](https://github.com/Guake/guake/pull/11). I hacked it into the previous versions adding accelerators to the buttons but the new gtk3 branch now uses gtk.Notebook, and that got me confused. – Elias Dorneles Apr 15 '13 at 02:19
  • Thanks for your example, it works alright! The behavior is a bit weird -- if I keep the shortcut pressed the button keeps blinking, but it works. I assume there is no other way without adding the buttons? – Elias Dorneles Apr 15 '13 at 02:27
  • 1
    I don't think there's a way without buttons except maybe using the Gtk.Notebook.set_action_widget method instead to keep the buttons off the tabs. Buttons have a default appearance when they are activated. Look up gtk css providers. You can set the buttons to have the same look when they are active or not. Here's some links to that info. https://developer.gnome.org/gtk3/3.5/GtkCssProvider.html http://stackoverflow.com/questions/11927785/how-to-make-buttons-different-colours-in-python-gtk3-using-gi If you have any trouble with that start another question. – Quentin Engles Apr 15 '13 at 02:43
  • @elias I finally looked at your project. It looks pretty cool. Just thought you should know. The simplicity is nice. – Quentin Engles Jan 29 '15 at 15:32
  • Hello, Quentin! Oh, guake is not really my project, I was just a guake user, making a PR for something that was bothering me at the time. I'm not a guake user anymore. :) – Elias Dorneles Jan 29 '15 at 19:11
2

First I googled, I found your question.

Then I googled more. This resource GtkWidget points me to 'scroll-event'. There it says:

To receive this signal, the GdkWindow associated to the widget needs to enable the GDK_SCROLL_MASK mask.

I followed the link to GDK_SCROLL_MASK to find out one more mask GDK_SMOOTH_SCROLL_MASK.

So here is solution:

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()

Have a nice day :-)