Transcription of Tkinter GUI Programming by
1 Tkinter GUI Programming byExampleLearn to create modern GUIs using Tkinter by buildingreal-world projects in PythonDavid LoveBIRMINGHAM - MUMBAIT kinter GUI Programming by ExampleCopyright 2018 Packt PublishingAll rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any formor by any means, without the prior written permission of the publisher, except in the case of brief quotationsembedded in critical articles or effort has been made in the preparation of this book to ensure the accuracy of the information , the information contained in this book is sold without warranty, either express or implied.
2 Neither theauthor, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged tohave been caused directly or indirectly by this Publishing has endeavored to provide trademark information about all of the companies and productsmentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracyof this Editor: Aaron LazarAcquisition Editor: Denim PintoContent Development Editor: Anugraha ArunagiriTechnical Editor: Subhalaxmi NadarCopy Editor: Safis EditingProject Coordinator: Ulhas KambaliProofreader: Safis EditingIndexer: Aishwarya GangawaneGraphics: Tania DuttaProduction Coordinator: Arvindkumar GuptaFirst published: April 2018 Production reference.
3 1240418 Published by Packt Publishing Place35 Livery StreetBirminghamB3 2PB, is an online digital library that gives you full access to over 5,000 books and videos, aswell as industry leading tools to help you plan your personal development and advanceyour career. For more information, please visit our subscribe?Spend less time learning and more time coding with practical eBooks and Videosfrom over 4,000 industry professionalsImprove your learning with Skill Plans built especially for youGet a free eBook or video every monthMapt is fully searchableCopy and paste, print, and bookmark you know that Packt offers eBook versions of every book published, with PDF andePub files available?
4 You can upgrade to the eBook version at and as aprint book customer, you are entitled to a discount on the eBook copy. Get in touch with usat for more , you can also read a collection of free technical articles, sign up for arange of free newsletters, and receive exclusive discounts and offers on Packt books the authorDavid Love is a web developer from Kent, England. He has worked on a multitude ofdifferent systems over his career. Programming languages in his arsenal include Python,PHP, and JavaScript. He is well-trained in Linux server management and its relevanttechnologies, including MySQL, PostgreSQL, NGINX, and has written an e-book called Tkinter By Example, which is available for free under aCreative Commons licenses and maintains an ever-growing blog post named The TkinterCookbook, full of small examples on how to perform some specific the reviewerErik S.
5 Rapert is a programmer and a twin who loves Linux and video games. He lives inDallas with his wife, who is also a software engineer. Erik has a wide range of experience,which includes creating blinking LEDs using Arduino, building small desktop apps usingPython and Tkinter , web development with PHP or Ruby, and developing cutting-edgevirtual reality using C++. He has used a very broad range of Programming languages, butPython is one of his you William C. Slater for teaching me how to write software. Thank you AndrewClosson for being a teacher. Thank you Ashley N. Tharp for being is searching for authors like youIf you're interested in becoming an author for Packt, please visit andapply today.
6 We have worked with thousands of developers and tech professionals, justlike you, to help them share their insight with the global tech community. You can make ageneral application, apply for a specific hot topic that we are recruiting an author for, orsubmit your own of ContentsPreface1 Chapter 1: Meet Tkinter6 Installation7 How will the code be structured?7 Geometry managers8pack8grid9place10To pack or to grid?11 Getting going12 Adding interactivity14 Using variables16 Our first try16 Creating Tkinter -compatible variables17 Using and updating18 Fixing our application18 Showing messages19 Showing information with showinfo19 Showing warnings or errors20 Getting feedback from the user21 Getting text input22 Summary24 Chapter 2.
7 Back to the Command Line Basic Blackjack25 Python's class system26 Instances26 Inheritance28 Blackjack's classes30 The Card class30 The Deck class31 The Hand class32 The Game class and main loop33 Command line versus GUI38 Interactivity38 Familiarity39 Ease of use39 Size and portability40 Summary40 Table of Contents[ ii ]Chapter 3: Jack is Back in Style the Blackjack GUI41 Moving from the command line to a graphical interface41 The Canvas widget43 Creating a graphical blackjack game46 Card, Deck, and Hand48 The GameState class49 The GameScreen class53 Playing our game62 Summary64 Chapter 4.
8 The Finishing Touches Sound and Animation65 Python's module system66 The blackjack packages71 The casino package71 The casino_sounds package74 Setting up a virtual environment75 Creating the package76 The file77 Initializing the GameWindow class78 The GameScreen class80 The GameState class90 Choosing to hit91 Choosing to stick95 Running out of money96 Finishing off97 Summary98 Chapter 5: Creating a Highly Customizable Python Editor99 The ttk submodule100 Styling a tk widget101 Styling a ttk widget103 Ttk style inheritance105 Beginning our text editor109 Tkinter 's event system112 Binding an event112 Overwriting default events115 Generating events116 Events in our text editor118A second top-level window123 Summary125 Chapter 6: Color Me Impressed!
9 Adding Syntax Highlighting127 Tkinter 's indexing system127 Getting the cursor's position128 Named indexes130 Table of Contents[ iii ]Special strings130 Line endings130 Horizontal movement130 Vertical movement131 Line beginning and end131 Word beginning and end131 Expanding our demo132 Using tags134 Searching text137 Adding syntax highlighting to our text editor140 The Highlighter class140 Using our Highlighter class150 The LineNumbers class151 Using our LineNumbers class153 Integrating our FindWindow class153 Using our FindWindow class158 Summary159 Chapter 7: Not Just for Restaurants All About Menus160 The Menu widget160A menu bar161A floating menu164 Adding a menu bar to our text editor165 Adding a context menu to our text editor170 Handling files172 Changing the syntax highlighting177 Changing the editor's font180 The Listbox widget181 The Spinbox widget183 Saving the user's choices183 Changing the editor's color scheme186 Summary198 Chapter 8.
10 Talk Python to Me a Chat Application200 Creating a scrollable frame200 Creating our FriendsList class206 Creating our ChatWindow class210 Creating our SmilieSelect class218 Summary226 Chapter 9: Connecting Getting Our Chat Client Online227 Introduction to flask227 Our first web page228 Using JSON230 The requests module232 Table of Contents[ iv ]Sending a GET request232 Sending a POST request233 The sqlite3 module235 Creating a database and table235 Adding data to a SQLite database236 Selecting data from a SQLite database238 Linking flask and sqlite239 Updating our FriendsList class243 Creating the Requester class246 Connecting our FriendsList to our web service248 Connecting our ChatWindow251 Updating our server to store conversations252 Creating the Conversation class252 Using the Conversation class in our server254 Adding the new endpoints to our Requester257 Updating our ChatWindow class to send requests to the