Execute command on clicking on a GTK notebook tab

Viewed 54

I want to run a command when clicking on a GTK notebook tab. Like the motherboard tab in the image below.

example app

My goal here is to run some terminal commands to get motherboard info. But I don't want to run them as soon as i open the application. It would make the app slow. Inside the tab i have a box layout. and inside it i have two labels.

motherboard tab

I did try I am using GTK+ from python3.8, And designing the UI file using glade.

1 Answers

You can use the switch_page signal and run command in the signal handler.

Simple example:

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


def on_page_switch(notebook, page, page_num):
    print("Page %d selected" % page_num)
    

win = Gtk.Window()
win.connect("destroy", Gtk.main_quit)

notebook = Gtk.Notebook()

win.add(notebook)

page1 = Gtk.Box()
notebook.append_page(page1)
page2 = Gtk.Box()
notebook.append_page(page2)

notebook.connect("switch_page", on_page_switch)

win.show_all()
Gtk.main()

You get the page widget in the signal handler so you can "fill" the motherboard info from it.

Related