这将解决选项卡编号切换问题。
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)