684 Pages · 8.97 mb ·
John E. Grayson
Programming
- PythonAndTkinter (Page 1) - brief contents (Page 8) - contents (Page 10) - preface (Page 16) - special thanks (Page 18) - about the reader (Page 20) - about the author (Page 21) - conventions (Page 22) - about the cover (Page 23) - author online (Page 24) - Part 1: Basic concepts (Page 26) - Python (Page 28) - 1.1 Introduction to Python programming and a feature review (Page 28) - 1.1.1 Why Python? (Page 29) - 1.1.2 Where can Python be used? (Page 30) - 1.2 Key data types: lists, tuples and dictionaries (Page 30) - 1.2.1 Lists (Page 30) - 1.2.2 Tuples (Page 32) - 1.2.3 Dictionaries (Page 33) - 1.3 Classes (Page 34) - 1.3.1 How do classes describe objects? (Page 34) - 1.3.2 Defining classes (Page 34) - 1.3.3 Neat Python trick #10 (Page 34) - 1.3.4 Initializing an instance (Page 35) - 1.3.5 Methods (Page 35) - 1.3.6 Private and public variables and methods (Page 36) - 1.3.7 Inheritance (Page 36) - 1.3.8 Multiple inheritance (Page 36) - 1.3.9 Mixin classes (Page 36) - Tkinter (Page 37) - 2.1 The Tkinter module (Page 37) - 2.1.1 What is Tkinter? (Page 37) - 2.1.2 What about performance? (Page 38) - 2.1.3 How do I use Tkinter? (Page 38) - 2.1.4 Tkinter features (Page 39) - 2.2 Mapping Tcl/Tk to Tkinter (Page 39) - 2.3 Win32 and Unix GUIs (Page 40) - 2.4 Tkinter class hierarchy (Page 41) - 2.5 Tkinter widget appearance (Page 42) - Building an application (Page 43) - 3.1 Calculator example: key features (Page 46) - 3.2 Calculator example: source code (Page 46) - 3.3 Examining the application structure (Page 52) - 3.4 Extending the application (Page 53) - Part 2: Displays (Page 54) - Tkinter widgets (Page 56) - 4.1 Tkinter widget tour (Page 56) - 4.1.1 Toplevel (Page 57) - 4.1.2 Frame (Page 58) - 4.1.3 Label (Page 60) - 4.1.4 Button (Page 61) - 4.1.5 Entry (Page 62) - 4.1.6 Radiobutton (Page 62) - 4.1.7 Checkbutton (Page 63) - 4.1.8 Menu (Page 64) - 4.1.9 Message (Page 67) - 4.1.10 Text (Page 68) - 4.1.11 Canvas (Page 69) - 4.1.12 Scrollbar (Page 70) - 4.1.13 Listbox (Page 70) - 4.1.14 Scale (Page 71) - 4.2 Fonts and colors (Page 72) - 4.2.1 Font descriptors (Page 72) - 4.2.2 X Window System font descriptors (Page 72) - 4.2.3 Colors (Page 73) - 4.2.4 Setting application-wide default fonts and colors (Page 74) - 4.3 Pmw Megawidget tour (Page 74) - 4.3.1 AboutDialog (Page 75) - 4.3.2 Balloon (Page 75) - 4.3.3 ButtonBox (Page 76) - 4.3.4 ComboBox (Page 77) - 4.3.5 ComboBoxDialog (Page 78) - 4.3.6 Counter (Page 79) - 4.3.7 CounterDialog (Page 80) - 4.3.8 Dialog (Page 81) - 4.3.9 EntryField (Page 81) - 4.3.10 Group (Page 82) - 4.3.11 LabeledWidget (Page 83) - 4.3.12 MenuBar (Page 84) - 4.3.13 MessageBar (Page 84) - 4.3.14 MessageDialog (Page 86) - 4.3.15 NoteBookR (Page 86) - 4.3.16 NoteBookS (Page 87) - 4.3.17 NoteBook (Page 88) - 4.3.18 OptionMenu (Page 89) - 4.3.19 PanedWidget (Page 90) - 4.3.20 PromptDialog (Page 91) - 4.3.21 RadioSelect (Page 91) - 4.3.22 ScrolledCanvas (Page 92) - 4.3.23 ScrolledField (Page 93) - 4.3.24 ScrolledFrame (Page 94) - 4.3.25 ScrolledListbox (Page 95) - 4.3.26 ScrolledText (Page 95) - 4.3.27 SelectionDialog (Page 96) - 4.3.28 TextDialog (Page 97) - 4.3.29 TimeCounter (Page 98) - 4.4 Creating new megawidgets (Page 98) - 4.4.1 Description of the megawidget (Page 98) - 4.4.2 Options (Page 99) - 4.4.3 Creating the megawidget class (Page 99) - Screen layout (Page 102) - 5.1 Introduction to layout (Page 102) - 5.1.1 Geometry management (Page 103) - 5.2 Packer (Page 104) - 5.2.1 Using the expand option (Page 107) - 5.2.2 Using the fill option (Page 107) - 5.2.3 Using the padx and pady options (Page 109) - 5.2.4 Using the anchor option (Page 109) - 5.2.5 Using hierarchical packing (Page 109) - 5.3 Grid (Page 111) - 5.4 Placer (Page 115) - 5.5 Summary (Page 119) - Events, bindings and callbacks (Page 120) - 6.1 Event-driven systems: a review (Page 120) - 6.1.1 What are events? (Page 121) - 6.1.2 Event propagation (Page 122) - 6.1.3 Event types (Page 122) - 6.2 Tkinter events (Page 123) - 6.2.1 Events (Page 123) - 6.3 Callbacks (Page 127) - 6.4 Lambda expressions (Page 128) - 6.4.1 Avoiding lambdas altogether (Page 128) - 6.5 Binding events and callbacks (Page 129) - 6.5.1 Bind methods (Page 129) - 6.5.2 Handling multiple bindings (Page 131) - 6.6 Timers and background procedures (Page 132) - 6.7 Dynamic callback handlers (Page 132) - 6.8 Putting events to work (Page 133) - 6.8.1 Binding widgets to dynamic data (Page 133) - 6.8.2 Data verification (Page 136) - 6.8.3 Formatted (smart) widgets (Page 142) - 6.9 Summary (Page 144) - Using classes, composites and special widgets (Page 145) - 7.1 Creating a Light Emitting Diode class (Page 145) - 7.1.1 Let’s try that again (Page 151) - 7.1.2 What has changed? (Page 154) - 7.2 Building a class library (Page 154) - 7.2.1 Adding a hex nut to our class library (Page 156) - 7.2.2 Creating a switch class (Page 158) - 7.2.3 Building a MegaWidget (Page 161) - 7.3 Summary (Page 164) - Dialogs and forms (Page 165) - 8.1 Dialogs (Page 166) - 8.1.1 Standard dialogs (Page 166) - 8.1.2 Data entry dialogs (Page 167) - 8.1.3 Single-shot forms (Page 171) - 8.1.4 Tkinter variables (Page 177) - 8.2 A standard application framework (Page 180) - 8.3 Data dictionaries (Page 190) - 8.4 Notebooks (Page 197) - 8.5 Browsers (Page 200) - 8.6 Wizards (Page 209) - 8.7 Image maps (Page 216) - 8.8 Summary (Page 223) - Panels and machines (Page 224) - 9.1 Building a front panel (Page 224) - 9.2 Modularity (Page 226) - 9.3 Implementing the front panel (Page 226) - 9.4 GIF, BMP and overlays (Page 240) - 9.5 And now for a more complete example (Page 245) - 9.6 Virtual machines using POV-Ray (Page 257) - 9.6.1 And now for something completely different... #10 The Example (Page 258) - 9.7 Summary (Page 261) - Drawing blobs and rubber lines (Page 262) - 10.1 Drawing on a canvas (Page 263) - 10.1.1 Moving canvas objects (Page 268) - 10.2 A more complete drawing program (Page 269) - 10.3 Scrolled canvases (Page 276) - 10.4 Ruler-class tools (Page 279) - 10.5 Stretching canvas objects (Page 283) - 10.6 Some finishing touches (Page 287) - 10.7 Speed drawing (Page 296) - 10.8 Summary (Page 300) - Graphs and charts (Page 301) - 11.1 Simple graphs (Page 301) - 11.2 A graph widget (Page 304) - 11.2.1 Adding bargraphs (Page 311) - 11.2.2 Pie charts (Page 314) - 11.3 3-D graphs (Page 317) - 11.4 Strip charts (Page 321) - 11.5 Summary (Page 323) - Navigation (Page 325) - 12.1 Introduction: navigation models (Page 325) - 12.2 Mouse navigation (Page 326) - 12.3 Keyboard navigation: “mouseless navigation” (Page 326) - 12.4 Building navigation into an application (Page 327) - 12.5 Image maps (Page 330) - 12.6 Summary (Page 330) - The window manager (Page 331) - 13.1 What is a window manager? (Page 331) - 13.2 Geometry methods (Page 332) - 13.3 Visibility methods (Page 333) - 13.4 Icon methods (Page 334) - 13.5 Protocol methods (Page 334) - 13.6 Miscellaneous wm methods (Page 335) - Part 3: Putting it all together... (Page 336) - Extending Python (Page 338) - 14.1 Writing a Python extension (Page 338) - 14.2 Building Python extensions (Page 341) - 14.2.1 Linking an extension statically in Unix (Page 341) - 14.2.2 Linking an extension statically in Windows (Page 342) - 14.2.3 Building a dynamic module in Unix (Page 342) - 14.2.4 Building a dynamic module in Windows (Page 343) - 14.2.5 Installing dynamic modules (Page 344) - 14.2.6 Using dynamic modules (Page 344) - 14.3 Using the Python API in extensions (Page 344) - 14.4 Building extensions in C++ (Page 345) - 14.5 Format strings (Page 346) - 14.6 Reference counts (Page 349) - 14.7 Embedding Python (Page 350) - 14.8 Summary (Page 353) - Debugging applications (Page 354) - 15.1 Why print statements? (Page 354) - 15.2 A simple example (Page 355) - 15.3 How to debug (Page 358) - 15.4 A Tkinter explorer (Page 359) - 15.5 pdb (Page 361) - 15.6 IDLE (Page 361) - 15.7 DDD (Page 362) - Designing effective graphics applications (Page 363) - 16.1 The elements of good interface design (Page 364) - 16.2 Human factors (Page 367) - 16.2.1 Choosing fonts (Page 368) - 16.2.2 Use of color in graphical user interfaces (Page 369) - 16.2.3 Size considerations (Page 371) - 16.3 Alternative graphical user interfaces (Page 371) - 16.4 Summary (Page 372) - Programming for performance (Page 373) - 17.1 Everyday speedups (Page 373) - 17.1.1 Program organization (Page 374) - 17.1.2 Using the Python optimizer (Page 375) - 17.1.3 Examining code (Page 375) - 17.2 Tkinter performance (Page 375) - 17.2.1 Keep it short! (Page 375) - 17.2.2 Eliminate local variables (Page 376) - 17.2.3 Keep it simple (Page 376) - 17.2.4 Fast initialization (Page 377) - 17.2.5 Throttling events (Page 377) - 17.3 Python techniques (Page 377) - 17.3.1 Importing modules (Page 378) - 17.3.2 Concatenating strings (Page 378) - 17.3.3 Getting nested loops right (Page 379) - 17.3.4 Eliminate module references (Page 379) - 17.3.5 Use local variables (Page 380) - 17.3.6 Using exceptions (Page 381) - 17.3.7 Using map, filter and reduce (Page 381) - 17.4 Application profiling (Page 382) - 17.5 Python extensions (Page 384) - 17.6 Summary (Page 385) - Threads and asynchronous techniques (Page 386) - 18.1 Threading (Page 386) - 18.1.1 Non-GUI threads (Page 387) - 18.1.2 GUI threads (Page 390) - 18.2 “after” processing (Page 394) - 18.3 Summary (Page 398) - Distributing Tkinter applications (Page 399) - 19.1 General issues in distributing applications (Page 399) - 19.2 Distributing Unix applications (Page 400) - 19.3 Distributing Win32 applications (Page 401) - 19.4 Python distribution tools (Page 404) - Part 4: Appendices (Page 406) - Mapping Tk to Tkinter (Page 408) - General Tk widget information (Page 408) - The Canvas widget (Page 410) - Canvas item types (Page 413) - The Entry widget (Page 418) - The Listbox widget (Page 420) - The Menu widget (Page 422) - The Text widget (Page 425) - Text indices (Page 426) - The Button widget (Page 430) - The Checkbutton widget (Page 431) - The Frame widget (Page 432) - The Label widget (Page 433) - The Menubutton widget (Page 434) - The Message widget (Page 435) - The Radiobutton widget (Page 436) - The Scale widget (Page 437) - The Scrollbar widget (Page 439) - The Toplevel widget (Page 440) - The Image class (Page 441) - The bitmap image type (Page 441) - The PhotoImage type (Page 441) - Window information (Page 442) - The window manager (Page 444) - Binding and virtual events (Page 445) - Geometry management (Page 445) - The pack command (Page 445) - The place command (Page 445) - The grid command (Page 446) - Fonts (Page 447) - Other Tk commands (Page 448) - Tkinter reference (Page 450) - About this appendix (Page 450) - Common options (Page 450) - Options shared by most widgets (Page 451) - Options shared by many widgets (Page 453) - Inherited methods (Page 458) - Common widget methods (Page 458) - Winfo methods (Page 465) - Wm methods (Page 471) - Description (Page 471) - Bitmap class (Page 477) - Description (Page 477) - Inheritance (Page 477) - Shared options (Page 477) - Options specific to Bitmap (Page 477) - Methods (Page 478) - Button (Page 478) - Description (Page 478) - Inheritance (Page 479) - Shared options (Page 479) - Options specific to Button (Page 480) - Methods (Page 480) - Canvas (Page 481) - Description (Page 481) - Inheritance (Page 481) - Shared options (Page 481) - Options specific to Canvas (Page 482) - Methods (Page 483) - Canvas Arc (Page 493) - Description (Page 493) - Inheritance (Page 493) - Shared options (Page 493) - Options specific to Arc (Page 493) - Methods (Page 494) - Canvas bitmap (Page 495) - Description (Page 495) - Inheritance (Page 495) - Shared options (Page 495) - Options specific to Bitmap (Page 495) - Methods (Page 495) - Canvas image (Page 496) - Description (Page 496) - Inheritance (Page 496) - Shared options (Page 496) - Options specific to Image (Page 496) - Methods (Page 497) - Canvas line (Page 497) - Description (Page 497) - Inheritance (Page 497) - Shared options (Page 497) - Options specific to Line (Page 498) - Methods (Page 499) - Canvas oval (Page 499) - Description (Page 499) - Inheritance (Page 499) - Shared options (Page 500) - Options specific to Oval (Page 500) - Methods (Page 500) - Canvas polygon (Page 501) - Description (Page 501) - Inheritance (Page 501) - Shared options (Page 501) - Options specific to Polygon (Page 501) - Methods (Page 502) - Canvas rectangle (Page 502) - Description (Page 502) - Inheritance (Page 502) - Shared options (Page 502) - Options specific to Rectangle (Page 503) - Methods (Page 503) - Canvas text (Page 503) - Description (Page 503) - Inheritance (Page 504) - Shared options (Page 504) - Options specific to Text (Page 504) - Methods (Page 504) - Canvas window (Page 505) - Description (Page 505) - Inheritance (Page 505) - Shared options (Page 505) - Options specific to Window (Page 505) - Methods (Page 506) - Checkbutton (Page 506) - Description (Page 506) - Inheritance (Page 507) - Shared options (Page 507) - Options specific to Checkbutton (Page 508) - Methods (Page 509) - Entry (Page 509) - Description (Page 509) - Inheritance (Page 510) - Shared options (Page 510) - Options specific to Entry (Page 511) - Methods (Page 512) - Font class (Page 514) - Inheritance (Page 514) - Description (Page 514) - Shared options (Page 514) - Options specific to Font (Page 514) - Methods (Page 515) - Functions (Page 516) - Frame (Page 516) - Description (Page 516) - Inheritance (Page 516) - Shared options (Page 516) - Options specific to Frame (Page 517) - Methods (Page 517) - Grid geometry manager (Page 517) - Inheritance (Page 517) - Description (Page 518) - Shared options (Page 518) - Options specific to Grid (Page 518) - Methods (Page 519) - Label (Page 520) - Description (Page 520) - Inheritance (Page 521) - Shared options (Page 521) - Methods (Page 521) - Listbox (Page 522) - Description (Page 522) - Inheritance (Page 522) - Shared options (Page 522) - Options specific to Listbox (Page 523) - Methods (Page 524) - Menu (Page 526) - Description (Page 526) - Inheritance (Page 526) - Shared options (Page 526) - Options specific to Menu (Page 527) - Methods (Page 528) - Menubutton (Page 531) - Description (Page 531) - Inheritance (Page 531) - Shared options (Page 532) - Options specific to Menubutton (Page 533) - Methods (Page 533) - Message (Page 533) - Description (Page 533) - Inheritance (Page 534) - Shared options (Page 534) - Options specific to Message (Page 535) - Methods (Page 535) - OptionMenu class (Page 535) - Description (Page 535) - Inheritance (Page 535) - Shared options (Page 536) - Options specific to Widget (Page 536) - Methods (Page 536) - Pack geometry manager (Page 536) - Description (Page 536) - Inheritance (Page 536) - Shared options (Page 536) - Options specific to Pack (Page 536) - Methods (Page 537) - PhotoImage class (Page 537) - Description (Page 537) - Inheritance (Page 538) - Shared options (Page 538) - Options specific to PhotoImage (Page 538) - Methods (Page 539) - Place geometry manager (Page 541) - Description (Page 541) - Inheritance (Page 541) - Shared options (Page 541) - Options specific to Place (Page 541) - Methods (Page 543) - Radiobutton (Page 544) - Description (Page 544) - Inheritance (Page 544) - Shared options (Page 545) - Options specific to Radiobutton (Page 545) - Methods (Page 546) - Scale (Page 547) - Description (Page 547) - Inheritance (Page 547) - Shared options (Page 547) - Options specific to Scale (Page 548) - Methods (Page 549) - Scrollbar (Page 550) - Description (Page 550) - Inheritance (Page 550) - Shared options (Page 550) - Options specific to Scrollbar (Page 551) - Methods (Page 552) - Text (Page 553) - Description (Page 553) - Inheritance (Page 553) - Shared options (Page 553) - Options specific to Text (Page 554) - Methods (Page 557) - Toplevel (Page 564) - Description (Page 564) - Inheritance (Page 564) - Shared options (Page 564) - Options specific to Toplevel (Page 565) - Methods (Page 566) - Pmw reference: Python megawidgets (Page 567) - AboutDialog (Page 567) - Description (Page 567) - Inheritance (Page 567) - AboutDialog options (Page 568) - Components (Page 569) - Methods (Page 569) - Functions (Page 569) - Balloon (Page 570) - Description (Page 570) - Inheritance (Page 570) - Balloon options (Page 570) - Components (Page 570) - Methods (Page 571) - ButtonBox (Page 571) - Description (Page 571) - Inheritance (Page 571) - ButtonBox options (Page 572) - Components (Page 572) - Methods (Page 573) - ComboBox (Page 574) - Description (Page 574) - Inheritance (Page 574) - ComboBox options (Page 574) - Components (Page 575) - Methods (Page 575) - ComboBoxDialog (Page 576) - Description (Page 576) - Inheritance (Page 576) - ComboBoxDialog options (Page 576) - Components (Page 577) - Methods (Page 578) - Counter (Page 578) - Description (Page 578) - Inheritance (Page 578) - Counter options (Page 578) - Components (Page 580) - Methods (Page 581) - CounterDialog (Page 581) - Description (Page 581) - Inheritance (Page 581) - CounterDialog options (Page 581) - Components (Page 582) - Methods (Page 582) - Dialog (Page 583) - Description (Page 583) - Inheritance (Page 583) - Dialog options (Page 583) - Components (Page 584) - Methods (Page 584) - EntryField (Page 584) - Description (Page 584) - Validation function return values (Page 585) - Inheritance (Page 585) - EntryField options (Page 585) - Validators (Page 586) - Components (Page 588) - Methods (Page 588) - Group (Page 589) - Description (Page 589) - Inheritance (Page 589) - Group options (Page 589) - Components (Page 589) - Methods (Page 590) - LabeledWidget (Page 590) - Description (Page 590) - Inheritance (Page 590) - LabeledWidget options (Page 590) - Components (Page 591) - Methods (Page 591) - MegaArchetype (Page 591) - Description (Page 591) - Inheritance (Page 592) - Methods (Page 592) - MegaToplevel (Page 594) - Description (Page 594) - Inheritance (Page 595) - MegaToplevel options (Page 595) - Components (Page 595) - Methods (Page 595) - MegaWidget (Page 596) - Description (Page 596) - Inheritance (Page 596) - Components (Page 596) - Methods (Page 596) - MenuBar (Page 597) - Description (Page 597) - Inheritance (Page 597) - MenuBar options (Page 597) - Components (Page 597) - Methods (Page 597) - MessageBar (Page 599) - Description (Page 599) - Inheritance (Page 599) - MessageBar options (Page 599) - Components (Page 600) - Methods (Page 600) - MessageDialog (Page 601) - Description (Page 601) - Inheritance (Page 601) - MessageDialog options (Page 601) - Components (Page 602) - Methods (Page 602) - NoteBook (Page 603) - Description (Page 603) - Inheritance (Page 603) - NoteBook options (Page 603) - Components (Page 604) - Methods (Page 604) - NoteBookR (Page 605) - Description (Page 605) - Inheritance (Page 605) - NoteBookR options (Page 605) - Components (Page 606) - Methods (Page 606) - NoteBookS (Page 607) - Description (Page 607) - Inheritance (Page 607) - NoteBookS options (Page 607) - Components (Page 608) - Methods (Page 608) - OptionMenu (Page 609) - Description (Page 609) - Inheritance (Page 609) - OptionMenu options (Page 609) - Components (Page 610) - Methods (Page 610) - PanedWidget (Page 611) - Description (Page 611) - Inheritance (Page 611) - PanedWidget options (Page 611) - Pane options (Page 611) - Components (Page 612) - Methods (Page 612) - PromptDialog (Page 612) - Description (Page 612) - Inheritance (Page 613) - PromptDialog options (Page 613) - Components (Page 613) - Methods (Page 614) - RadioSelect (Page 614) - Description (Page 614) - Inheritance (Page 614) - RadioSelect options (Page 615) - Components (Page 615) - Methods (Page 616) - ScrolledCanvas (Page 617) - Description (Page 617) - Inheritance (Page 617) - ScrolledCanvas options (Page 617) - Components (Page 618) - Methods (Page 618) - ScrolledField (Page 619) - Description (Page 619) - Inheritance (Page 619) - ScrolledField options (Page 619) - Components (Page 619) - Methods (Page 620) - ScrolledFrame (Page 620) - Description (Page 620) - Inheritance (Page 620) - ScrolledFrame options (Page 620) - Components (Page 622) - Methods (Page 623) - ScrolledListBox (Page 623) - Description (Page 623) - Inheritance (Page 623) - ScrolledListBox options (Page 623) - Components (Page 624) - Methods (Page 625) - ScrolledText (Page 625) - Description (Page 625) - Inheritance (Page 626) - ScrolledText options (Page 626) - Components (Page 626) - Methods (Page 627) - SelectionDialog (Page 628) - Description (Page 628) - Inheritance (Page 628) - SelectionDialog options (Page 628) - Components (Page 629) - Methods (Page 629) - TextDialog (Page 630) - Description (Page 630) - Inheritance (Page 630) - TextDialog options (Page 630) - Components (Page 631) - Methods (Page 631) - TimeCounter (Page 632) - Description (Page 632) - Inheritance (Page 632) - TimeCounter options (Page 632) - Components (Page 633) - Methods (Page 633) - Building and installing Python, Tkinter (Page 635) - Building for Unix (Page 635) - Obtaining source distributions (Page 635) - Building Tcl (Page 636) - Building Tk (Page 637) - Building Python (Page 637) - Building for Windows (Page 639) - Obtaining source distributions (Page 639) - Building Tcl (Page 639) - Building Tk (Page 640) - Building Python (Page 640) - Building for MacOS (Page 641) - Events and keysyms (Page 642) - Modifiers (Page 642) - Event types (Page 643) - Qualifier (Page 644) - Keysyms (Page 644) - E.5.1 Latin-1 (Page 644) - Cursors (Page 646) - References (Page 650) - Resources (Page 650) - Siomodule (Page 650) - SWIG (Page 650) - NumPy (Page 651) - Python News Group (Page 651) - Python Imaging Library (PIL) (Page 651) - PythonWorks (Page 651) - Python books (Page 651) - X Window books (Page 652) - Tcl/Tk books (Page 652) - Human factors engineering (Page 652) - index (Page 654)