Download presentation
Presentation is loading. Please wait.
Published byHilda Conley Modified over 8 years ago
1
PyGTK 2.0 Quick Start Guide By Jeremy Bongio
2
Table of Contents “Overview” of Python Concepts of GTK Implementing pyGTK
3
“Overview” of Python ● Basic Layout – Interpreted – Space sensitive ● Classes ● Functions ● Conditionals ● Variables ● Lists ● Refer to intro_to_python.py
4
Concepts of GTK ● Widgets ● Signals and Signal Handlers ● Callbacks ● Events ● Packing Widgets ● Basic program structure
5
Widgets ● A widget is a general term for any of the components in a window, including: – Scrollbars – Buttons – Menus – Tabs – Tables – Sliders – Text – text boxes – Labels – etc.
6
Signals and Signal Handlers ● A signal is a message that is emitted when certain actions are done to a widget. For example, a widget may be “toggled”, “clicked”, “destroyed”, etc. ● A signal handler waits until a specific signal is presented. Then the handler executes a predetermined function in response.
7
Callback Functions and Events ● The function that is run by a signal handler in response to a signal is called a Callback function.
8
Events ● Not only can callback functions be executed in response to signals, but they can be attached to events. ● An event is basically a signal, but it is does not originate from a widget. Examples: button_press_event, destroy_event, scroll_event, focus_in_event, key_press_event, etc.
9
Packing Widgets into Windows ● By default, a window doesn't apply any order to the widgets it contains. To remedy this, there are packing widgets. ● Widgets are put inside of packing widgets, including other packing widgets.
10
Types of Packing Widgets ● Horizontal Box ● Vertical Box ● Tables ● And more!
11
Basic Program Structure ● Generally, building a GUI in GTK goes something like this. – Include path to python and import needed libraries – Create first window – Create a widget packing scheme – Create the necessary widgets – Connect widgets to signal handlers – Pack widgets into window – Show everything starting with small things and working up to the main window.
12
The Source Code
13
Initial Setup ● First thing's first, make sure you have #!/usr/bin/env python as the very first line of the program. ● Next, import the gtk and pygtk libraries as follows: – Import pygtk – Import gtk ● And to make things run smoother in the future you can include the line: – pygtk.require('2.0')
14
First Thing's First, A Window ● self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) ● This creates a new, invisible window completely blank, containing no widgets. ● Later you may want to close a particular window. self.window.destroy() will do the trick. ● Or else close the program entirely with gtk.main_quit()
15
Horizontal Box Packaging ● To create a horizontal box widget to pack other widgets into: h_box=gtk.HBox(homogeneous=False,spacing=0) ● Spacing is the number of pixels that will separate each widget in this package. ● If homogeneous is true, all widgets in this package will be of the same dimensions. ● To add a widget to this box: h_box.pack_start(button,expand=False,fill=False,padding=20)
16
Vertical Box Packaging ● To create a vertical box widget to pack other widgets into: v_box=gtk.VBox(homogeneous=False,spacing=0) ● To add a widget to this box: v_box.pack_start(button,expand=False,fill=False,padding=20) ● Expand allows this widget to grow as the window size changes. ● Fill allows this widget to fill any white space that is formed when window resizes. ● Padding is the number of pixels that will separate this widget from the others.
17
Table Packaging ● To create a table widget to pack other widgets into: table=gtk.Table(rows=2,columns=2,homogeneous=False) ● To add a widget to the top left of this table: table.attach(button,0,1,0,1) ● The first two numbers define the top column. The last two numbers define the row. ● The widgets in this table automatically fill whitespace and enlarge as the window is resized.
18
Common Buttons ● Normal button ● Radio button ● Checklist Button ● Toggle Button
19
Implementing a Common Buttons ● For a normal button: button=gtk.Button(“This is the label”) ● For the first radio button in a line: button=gtk.RadioButton(None,”Label”) ● Before adding more radio buttons, pack this button first. Otherwise you redefine the first button. OR just use different names for each. ● For the rest of the radio buttons: button=gtk.RadioButton(button,”Label”)
20
Common Buttons Continued ● For a toggle button: button=gtk.ToggleButton(“label”) ● For a checklist button: button=gtk.CheckButton(“label”) ● Of course, what good is a button if it doesn't do anything?
21
Signals and Signal Handlers Implemented ● First, you must create a function that will act as the callback function. Example: def button_clicked(self, widget, data): self.window1 = gtk.Window(gtk.WINDOW_TOPLEVEL) label = gtk.Label("title") label.set_text("You clicked on button "+data+"!!!") label.show() self.window1.add(label) self.window1.connect("destroy", self.kill_window) self.window1.show()
22
Signal Handlers ● To connect a button to this new callback function: button.connect(“clicked”,self.button_clicked,”data”) ● The first argument is the signal that will trigger this signal handler. ● The second argument is the name of the callback function. ● The third argument is any data that you want the callback function to know, or None if you don't want anymore information.
23
Displaying Information to User ● Label: label=gtk.Label(“title of label”) label.set_text(“the displayed text”) ● Tool tips: tooltip=gtk.Tooltips() tooltip.set_tip(button,”displayed tip”) ● Scrollable lists of dynamic data. UGH, ask me later!
24
Text Entry ● Text entry allows for user input through a text window. Examples of use: ● entry=gtk.Entry(max=0) ● entry.set_max_length(max) ● entry.set_text(text) ● entry.set_editable(True) ● To get text the user typed in box: ● text=entry.get_text()
25
At the End ● Important, remember that every window and widget that you want to show up on the screen must be specifically told to “.show()” ● Like: button.show(), window.show(), h_box.show()
26
Glade – Rapid Development Tool
27
Example of Glade Use ● self.glade_xml=gtk.glade.XML(gladepath,'MigrationWindow') ● self.glade_xml.signal_autoconnect({ “on_close_clicked” : self.close}) ● gladepath = “file.glade”
28
Where to Go for More ● http://www.moeraki.com/pygtktutorial/pygtk2tutoria l/A very thorough tutorial. Go here for any questions you have or clarifications you need. http://www.moeraki.com/pygtktutorial/pygtk2tutoria l/ ● http://www.gtk.org/tutorial/ A tutorial on how to use gtk 2.0 in C. Basically the same, just in C syntax. http://www.gtk.org/tutorial/ ● http://www.python.org/doc/tut/tut.html Another tutorial, learn all about python. http://www.python.org/doc/tut/tut.html
29
Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.