from gi.repository import Gtk class MyWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Action Example") self.set_default_size(200, 100) # Create a simple quit action quit_action = Gtk.Action("quit", "Quit", "Quit the application", Gtk.STOCK_QUIT) quit_action.connect("activate", self.on_quit) self.add_action(quit_action) def on_quit(self, widget, data=None): Gtk.main_quit() win = MyWindow() win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main()
from gi.repository import Gtk class MyWindow(Gtk.Window): def __init__(self): Gtk.Window.__init__(self, title="Action Example") self.set_default_size(200, 100) # Create a toggle action toggle_action = Gtk.ToggleAction("bold", "Bold", "Bold text", Gtk.STOCK_BOLD) toggle_action.connect("toggled", self.on_bold_toggled) self.add_action(toggle_action) # Create a label to demonstrate bold text self.label = Gtk.Label("Hello World!") self.label.modify_font(Pango.FontDescription("Sans 12")) self.add(self.label) def on_bold_toggled(self, widget, data=None): bold = widget.get_active() if bold: self.label.modify_font(Pango.FontDescription("Sans Bold 12")) else: self.label.modify_font(Pango.FontDescription("Sans 12")) win = MyWindow() win.connect("delete-event", Gtk.main_quit) win.show_all() Gtk.main()This example creates a toggle action called "bold" that changes the font weight of a label widget from normal to bold when the action is toggled. Package library: PyGObject