Tkinter GUI Application
Development l+ TSHOT
Develop exciting and engaging GUI applications in Python
and Tkinter by working on 10 real-world applications
Bhaskar Chaudhary
[ PUBLISHING
]
BIRMINGHAM-MUMBAI
Table of Contents
Preface 1
Project 1: Meet Tkinter 7
Mission Briefing 7
The root window -
your drawing board 10
Widgets -
building blocks for your GUI program 13
Getting to know core Tkinter widgets 16
Geometry management 19
Events and callbacks -
adding life to programs 28
Doing it in
style 36
Mission Accomplished 41
A Hotshot Challenge 42
Project 2: Making a Text Editor like Notepad 43
Mission Briefing 43
Setting up the widgets 45
Leveraging the power of built-in Text widget options 51
Indexing and tagging 52
Working with forms and dialogs 59
Working with message boxes 64
The icon toolbar and View menu functions 67
Event handling and the context menu 74
Mission Accomplished 76
A Hotshot Challenge 76
Table of Contents
Project 3: Programmable Drum Machine 77
Mission Briefing 77
Setting up the GUI in OOP 79
Completing the pattern editor 85
Loading drum samples 88
Playing the drum machine 91
Tkinter and threading 94
More beat patterns 97
Object persistence 102
ttk-themed widgets 106
Mission Accomplished 112
A Hotshot Challenge 113
Project 4: Game of Chess 115
Mission Briefing 115
Structuring our program 117
Structuring chessboard-and-pieces-related data 123
Adding pieces on the board 130
Enforcing rules for pieces' movement 133
The chessboard logic 138
Making the chess functional 140
Adding menu and an info frame 150
Mission Accomplished 153
A Hotshot Challenge 153
Project 5: Audio Player 155
Mission Briefing 155
Getting the audio to play 158
Adding a playlist 163
Adding more controls to the player 169
Adding the top display console 173
Looping over tracks 179
Adding the contextual menu 182
Adding a tooltip and finalizing our player 184
Mission Accomplished 187
A Hotshot Challenge 188
ii
Table of Contents
Project 6: Drawing an Application 189
Mission Briefing 189
Developing a bare bone GUI framework 191
Structuring drawing program
our 196
Handling mouse events 202
Drawing items on the canvas 205
Setting the options toolbar at the top 209
Adding some more features 214
Mission Accomplished 220
A Hotshot Challenge 221
Project 7: Some Fun Project Ideas 223
Mission Briefing 223
Building a screen saver 224
Building a Snake game 229
Creating a WeatherReporter 239
Creating a phonebook application 245
Graphing with Tkinter 252
Mission Accomplished 257
A Hotshot Challenge 257
Appendix A: Miscellaneous Tips 259
Mission Briefing 259
Tracing Tkinter variables 260
Widget traversal 262
Validating user input 263
Formatting widget data 269
More on fonts 271
Working with Unicode characters 275
Tkinter class's hierarchy 278
Custom-made mixins 281
Tips for code cleanup and program optimization 283
Distributing the Tkinter application 287
Limitations of Tkinter 289
Alternatives to Tkinter 290
Getting interactive help 292
Tkinter in Python 3.x 293
Conclusion 294
iii —
Table of Contents
Appendix B: Quick Reference Sheets 297
Options common to widgets 297
Widget-specific options 300
The pack manager 301
The grid manager 302
The place manager 304
The event types 305
The event modifiers 306
The event details 306
Other event-related methods 308
List of available cursors 310
The basic widget methods 311
ttk widgets 320
The Toplevel window methods 325
Index 331
-DD