Hooking up a GUI in Python
Written by
on .Glade 3.0 is a great GUI designer. I can't imagine creating a GUI using code when this option is available. It takes no more than a few minutes to setup a screen, name your signals, and set the default properties. It's also super easy to hook up the handler methods.
Check out this code example of showing a Glade-based GUI and hooking up some signals in Python:
#!/usr/bin/python
import gtk, gtk.glade
# Load the Form1 instance
Form1 = gtk.glade.XML("Form1.glade")
# Signal Handlers
def CloseWindow(widget):
print widget
gtk.main_quit()
def button1_clicked(widget):
print widget
print "bottom button clicked"
NewForm = Form1.get_widget("window2")
NewForm.show()
def on_hscale1_change_value(widget, userdata, value):
print value
def on_vscale1_change_value(widget, userdata, value):
print value
def on_entry1_changed(widget):
print widget.get_text()
def on_checkbutton1_toggled(widget):
print widget.get_active()
def on_button4_clicked(widget):
print "close color picker"
widget.window.destroy()
# Create a dictionary of the event names and functions
MySignals = {"on_window1_destroy" : CloseWindow,
"on_button1_clicked" : button1_clicked,
"on_hscale1_change_value" : on_hscale1_change_value,
"on_vscale1_change_value" : on_vscale1_change_value,
"on_entry1_changed" : on_entry1_changed,
"on_checkbutton1_toggled" : on_checkbutton1_toggled,
"on_button4_clicked" : on_button4_clicked}
# Connect all the signals to the Form1
Form1.signal_autoconnect(MySignals)
# Start the main loop
gtk.main()