Pygtk 2.0 Tutorial
Total Page:16
File Type:pdf, Size:1020Kb
PyGTK 2.0 Tutorial John Finlay PyGTK 2.0 Tutorial by John Finlay Published April 13, 2005 This tutorial describes the use of the Python PyGTK module. Table of Contents 1. Introduction ................................................................................. 1 1.1. Exploring PyGTK .................................................................... 2 2. Getting Started .............................................................................. 4 2.1. Hello World in PyGTK ................................................................ 6 2.2. Theory of Signals and Callbacks ....................................................... 8 2.3. Events ............................................................................. 10 2.4. Stepping Through Hello World ........................................................ 12 3. Moving On ................................................................................ 16 3.1. More on Signal Handlers ............................................................. 16 3.2. An Upgraded Hello World ............................................................ 16 4. Packing Widgets ........................................................................... 19 4.1. Theory of Packing Boxes ............................................................. 19 4.2. Details of Boxes ..................................................................... 19 4.3. Packing Demonstration Program ...................................................... 22 4.4. Packing Using Tables ................................................................ 27 4.5. Table Packing Example .............................................................. 29 5. Widget Overview ........................................................................... 32 5.1. Widget Hierarchy ................................................................... 32 5.2. Widgets Without Windows ........................................................... 35 6. The Button Widget ......................................................................... 36 6.1. Normal Buttons ..................................................................... 36 6.2. Toggle Buttons ...................................................................... 40 6.3. Check Buttons ...................................................................... 43 6.4. Radio Buttons ....................................................................... 45 7. Adjustments ............................................................................... 49 7.1. Creating an Adjustment .............................................................. 49 7.2. Using Adjustments the Easy Way ..................................................... 49 7.3. Adjustment Internals ................................................................. 50 8. Range Widgets ............................................................................. 52 8.1. Scrollbar Widgets ................................................................... 52 8.2. Scale Widgets ....................................................................... 52 8.2.1. Creating a Scale Widget ....................................................... 52 8.2.2. Methods and Signals (well, methods, at least) ................................... 53 8.3. Common Range Methods ............................................................ 53 8.3.1. Setting the Update Policy ..................................................... 53 8.3.2. Getting and Setting Adjustments ............................................... 54 8.4. Key and Mouse Bindings ............................................................. 54 8.5. Range Widget Example .............................................................. 55 9. Miscellaneous Widgets ...................................................................... 62 9.1. Labels .............................................................................. 62 9.2. Arrows ............................................................................. 66 9.3. The Tooltips Object .................................................................. 68 9.4. Progress Bars ....................................................................... 71 9.5. Dialogs ............................................................................. 76 9.6. Images ............................................................................. 76 9.6.1. Pixmaps ..................................................................... 80 9.7. Rulers .............................................................................. 87 9.8. Statusbars .......................................................................... 91 9.9. Text Entries ......................................................................... 93 9.10. Spin Buttons ....................................................................... 96 9.11. Combo Widget .................................................................... 103 9.12. Calendar ......................................................................... 105 iii PyGTK 2.0 Tutorial 9.13. Color Selection ................................................................... 113 9.14. File Selections .................................................................... 117 9.15. Font Selection Dialog .............................................................. 120 10. Container Widgets ........................................................................ 123 10.1. The EventBox .................................................................... 123 10.2. The Alignment widget ............................................................. 124 10.3. Fixed Container ................................................................... 125 10.4. Layout Container .................................................................. 127 10.5. Frames ........................................................................... 130 10.6. Aspect Frames .................................................................... 133 10.7. Paned Window Widgets ............................................................ 135 10.8. Viewports ........................................................................ 138 10.9. Scrolled Windows ................................................................. 139 10.10. Button Boxes .................................................................... 142 10.11. Toolbar ......................................................................... 146 10.12. Notebooks ...................................................................... 152 10.13. Plugs and Sockets ................................................................ 159 10.13.1. Plugs .................................................................... 159 10.13.2. Sockets .................................................................. 160 11. Menu Widget ............................................................................ 164 11.1. Manual Menu Creation ............................................................ 164 11.2. Manual Menu Example ............................................................ 167 11.3. Using ItemFactory ................................................................ 169 11.4. Item Factory Example ............................................................. 169 12. Drawing Area ............................................................................ 173 12.1. Graphics Context .................................................................. 174 12.2. Drawing Methods ................................................................. 178 13. TextView Widget ......................................................................... 187 13.1. TextView Overview ............................................................... 187 13.2. TextViews ........................................................................ 187 13.3. Text Buffers ...................................................................... 194 13.3.1. TextBuffer Status Information ............................................... 194 13.3.2. Creating TextIters .......................................................... 195 13.3.3. Text Insertion, Retrieval and Deletion ........................................ 196 13.3.4. TextMarks ................................................................. 197 13.3.5. Creating and Applying TextTags ............................................. 198 13.3.6. Inserting Images and Widgets ............................................... 199 13.4. Text Iters ......................................................................... 200 13.4.1. TextIter Attributes .......................................................... 201 13.4.2. Text Attributes at a TextIter ................................................. 201 13.4.3. Copying a TextIter ......................................................... 202 13.4.4. Retrieving Text and Objects ................................................. 202 13.4.5. Checking Conditions at a TextIter ............................................ 203 13.4.6. Checking Location in Text .................................................. 204 13.4.7. Moving Through Text ...................................................... 205 13.4.8. Moving to a Specific Location ............................................... 206 13.4.9. Searching in Text .......................................................... 206 13.5. Text Marks ....................................................................... 207 13.6. Text Tags and Tag Tables ........................................................... 208