A Beginner’s Guide to PyGTK: Create Rich GUI Applications with Python
What is PyGTK?
PyGTK is a Python library used to create graphical user interfaces (GUIs). It is a set of Python wrappers for the GTK+ (GIMP Toolkit), which is a powerful, cross-platform toolkit for developing graphical applications. PyGTK allows developers to build elegant and efficient applications with ease. Because it’s Python-based, it integrates seamlessly into Python applications, making it a popular choice for both small and large projects.
Key Features of PyGTK:
- Provides access to GTK+ functionalities, which include widgets (e.g., buttons, labels, text boxes).
- Cross-platform compatibility: Runs on Linux, Windows, and macOS.
- Designed for simplicity, yet powerful for advanced GUI development.
- Full customization for appearance and behavior of UI components.
- Great documentation and community support.
Let’s dive into some of its most useful APIs with examples!
Useful PyGTK APIs with Code Snippets
Below are 20+ essential PyGTK APIs along with explanations and small code snippets to get started:
1. gtk.Window
Creates the main application window.
import gtk window = gtk.Window() window.set_title("My First PyGTK App") window.set_size_request(400, 300) # Set window size (width: 400px, height: 300px) window.connect("destroy", gtk.main_quit) # Close the app when the window is closed window.show_all() gtk.main()
2. gtk.Button
Creates a clickable button.
button = gtk.Button(label="Click Me!") button.connect("clicked", lambda x: print("Button clicked!"))
3. gtk.Label
Creates a text label.
label = gtk.Label("Hello, PyGTK!")
4. gtk.Entry
Input field for text entry.
entry = gtk.Entry() entry.set_text("Default Text")
5. gtk.ComboBoxText
Dropdown box to select from a list of options.
combobox = gtk.ComboBoxText() combobox.append_text("Option 1") combobox.append_text("Option 2") combobox.append_text("Option 3") combobox.set_active(0) # Set default selection to the first option
6. gtk.TextView
A multi-line text area for text editing.
textview = gtk.TextView() textview.set_wrap_mode(gtk.WRAP_WORD)
7. gtk.Image
Displays an image in the GUI.
image = gtk.Image() image.set_from_file("example.png")
8. gtk.MenuBar and gtk.Menu
Create a menu bar and attach menus.
menubar = gtk.MenuBar() file_menu = gtk.MenuItem(label="File") menu = gtk.Menu() save_item = gtk.MenuItem(label="Save") menu.append(save_item) file_menu.set_submenu(menu) menubar.append(file_menu)
9. gtk.CheckButton
Checkbox widget for toggling options.
checkbox = gtk.CheckButton(label="Check me!") checkbox.set_active(True) # Checked by default
10. gtk.RadioButton
Radio button for grouped selection.
radio1 = gtk.RadioButton(None, "Option A") radio2 = gtk.RadioButton(radio1, "Option B")
11. gtk.ScrolledWindow
Scrollable container for large content like text views.
scroll = gtk.ScrolledWindow() scroll.add(textview) # Add a widget (e.g., textview) into the scrolling area
12. gtk.ProgressBar
Displays a progress bar.
progressbar = gtk.ProgressBar() progressbar.set_fraction(0.5) # 50% complete
13. gtk.HBox and gtk.VBox
Horizontal and vertical box layouts for widgets.
hbox = gtk.HBox(spacing=10) # Widgets arranged horizontally vbox = gtk.VBox(spacing=10) # Widgets arranged vertically hbox.pack_start(button) # Add button to hbox
14. gtk.Alignment
Aligns widgets with padding and positioning options.
alignment = gtk.Alignment(xalign=0.5, yalign=0.5, xscale=1.0, yscale=1.0) alignment.add(label) # Add a label to the alignment container
15. gtk.FileChooserDialog
Dialog to select files.
file_dialog = gtk.FileChooserDialog("Choose a file", None, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) if file_dialog.run() == gtk.RESPONSE_OK: print("File selected:", file_dialog.get_filename()) file_dialog.destroy()
16. gtk.ColorButton
Select a color.
color_button = gtk.ColorButton() color_button.connect("color-set", lambda widget: print("Color selected:", widget.get_color()))
17. gtk.FontButton
Select a font.
font_button = gtk.FontButton() font_button.connect("font-set", lambda widget: print("Font selected:", widget.get_font_name()))
18. gtk.Dialog
Generic dialog window.
dialog = gtk.Dialog("My Dialog", None, gtk.DIALOG_DESTROY_WITH_PARENT, (gtk.STOCK_OK, gtk.RESPONSE_OK, gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)) dialog.vbox.pack_start(gtk.Label("This is a dialog!")) dialog.show_all() response = dialog.run() dialog.destroy()
19. gtk.ImageMenuItem
Menu item with an image icon.
image_item = gtk.ImageMenuItem(gtk.STOCK_NEW) image_item.connect("activate", lambda _: print("New menu clicked!"))
20. gtk.Statusbar
Displays status messages at the bottom of a window.
statusbar = gtk.Statusbar() context_id = statusbar.get_context_id("Example") statusbar.push(context_id, "Status message here")
A Generic Application Using PyGTK APIs
Here’s an example of a simple text editor application using PyGTK:
import gtk class TextEditorApp: def __init__(self): self.window = gtk.Window() self.window.set_title("PyGTK Text Editor") self.window.set_size_request(600, 400) self.window.connect("destroy", gtk.main_quit) # Create a VBox for layout vbox = gtk.VBox() # Create a menu bar menubar = gtk.MenuBar() file_menu_item = gtk.MenuItem("File") file_menu = gtk.Menu() open_item = gtk.MenuItem("Open") save_item = gtk.MenuItem("Save") quit_item = gtk.MenuItem("Quit") quit_item.connect("activate", gtk.main_quit) file_menu.append(open_item) file_menu.append(save_item) file_menu.append(quit_item) file_menu_item.set_submenu(file_menu) menubar.append(file_menu_item) vbox.pack_start(menubar, False, False, 0) # Create a TextView with scrolling self.textview = gtk.TextView() self.textview.set_wrap_mode(gtk.WRAP_WORD) scroll = gtk.ScrolledWindow() scroll.add(self.textview) vbox.pack_start(scroll, True, True, 0) # Create a status bar self.statusbar = gtk.Statusbar() vbox.pack_start(self.statusbar, False, False, 0) # Add vbox to the main window self.window.add(vbox) self.window.show_all() if __name__ == "__main__": app = TextEditorApp() gtk.main()
Conclusion
PyGTK is a feature-packed library that enables Python developers to create stunning and functional GUIs. With countless widget APIs, integration options, and cross-platform support, PyGTK is an excellent choice for building GUI-based applications.
Happy coding!