Introduction to PyGObject
PyGObject is a powerful library for creating graphical user interfaces (GUIs) in Python using the GObject Introspection libraries. It allows developers to build modern, responsive, and feature-rich applications by leveraging the GTK toolkit, GLib, and other GObject-based technologies. Whether you’re looking to build desktop applications, tools, or even dynamic widgets, PyGObject simplifies the process of creating robust GUIs.
Getting Started with PyGObject
To get started with PyGObject, you need to install it via pip. Use the following command:
pip install PyGObject
Once installed, you can start creating your first GTK application in Python.
Dozens of Useful API Examples
Below are some of the most useful PyGObject APIs with code snippets to help you get started:
1. Creating a Simple GTK Window
import gi gi.require_version("Gtk", "3.0") from gi.repository import Gtk class MyWindow(Gtk.Window): def __init__(self): super().__init__(title="Hello, PyGObject!") self.set_size_request(400, 300) win = MyWindow() win.connect("destroy", Gtk.main_quit) win.show_all() Gtk.main()
2. Adding a Button to Your Window
class MyWindow(Gtk.Window): def __init__(self): super().__init__(title="Button Example") self.set_size_request(400, 300) button = Gtk.Button(label="Click Me") button.connect("clicked", self.on_button_clicked) self.add(button) def on_button_clicked(self, widget): print("Button clicked!") win = MyWindow() win.connect("destroy", Gtk.main_quit) win.show_all() Gtk.main()
3. Using a Grid for Layout
class MyWindow(Gtk.Window): def __init__(self): super().__init__(title="Grid Example") self.set_size_request(400, 300) grid = Gtk.Grid() button1 = Gtk.Button(label="Button 1") button2 = Gtk.Button(label="Button 2") button3 = Gtk.Button(label="Button 3") grid.attach(button1, 0, 0, 1, 1) grid.attach(button2, 1, 0, 1, 1) grid.attach(button3, 0, 1, 2, 1) self.add(grid) win = MyWindow() win.connect("destroy", Gtk.main_quit) win.show_all() Gtk.main()
4. Adding a Header Bar
class MyWindow(Gtk.Window): def __init__(self): super().__init__() header = Gtk.HeaderBar(title="Header Bar Example") header.set_subtitle("An amazing subtitle!") header.props.show_close_button = True self.set_titlebar(header) self.set_size_request(400, 300) win = MyWindow() win.connect("destroy", Gtk.main_quit) win.show_all() Gtk.main()
5. File Chooser Dialog
def on_file_clicked(widget): dialog = Gtk.FileChooserDialog( title="Please choose a file", parent=None, action=Gtk.FileChooserAction.OPEN, ) dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK) response = dialog.run() if response == Gtk.ResponseType.OK: print("File selected:", dialog.get_filename()) dialog.destroy() window = Gtk.Window(title="File Chooser Example") button = Gtk.Button(label="Choose File") button.connect("clicked", on_file_clicked) window.add(button) window.connect("destroy", Gtk.main_quit) window.show_all() Gtk.main()
Complete App Example
Here is a complete example of a simple PyGObject application that combines the APIs above:
class MyApp(Gtk.Window): def __init__(self): super().__init__(title="My PyGObject App") self.set_size_request(400, 300) # Create a layout grid = Gtk.Grid() self.add(grid) # Add a header bar header = Gtk.HeaderBar(title="My App") header.set_subtitle("All in one!") header.props.show_close_button = True self.set_titlebar(header) # Add Buttons button1 = Gtk.Button(label="Click Me") button1.connect("clicked", self.on_click_me) grid.attach(button1, 0, 0, 1, 1) button2 = Gtk.Button(label="Choose File") button2.connect("clicked", self.on_file_clicked) grid.attach(button2, 1, 0, 1, 1) def on_click_me(self, widget): print("Button clicked!") def on_file_clicked(self, widget): dialog = Gtk.FileChooserDialog( title="Please choose a file", parent=self, action=Gtk.FileChooserAction.OPEN, ) dialog.add_buttons(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OPEN, Gtk.ResponseType.OK) response = dialog.run() if response == Gtk.ResponseType.OK: print("File selected:", dialog.get_filename()) dialog.destroy() app = MyApp() app.connect("destroy", Gtk.main_quit) app.show_all() Gtk.main()
With PyGObject, the possibilities for building stunning and functional GUIs are endless. Whether you’re a beginner or an experienced developer, exploring this library unlocks tremendous potential.