Data Loading...
9781788622301 Flipbook PDF
No description
144 Views
50 Downloads
FLIP PDF 706.26KB
Alejandro Rodas de Paz
Tkinter GUI Application Development
Cookbook A practical solution to your GUI development problems with Python and Tkinter
FOR SALE IN INDIA ONLY
Tkinter GUI Application Development Cookbook A practical solution to your GUI development problems with Python and Tkinter
Alejandro Rodas de Paz
BIRMINGHAM - MUMBAI
Tkinter GUI Application Development Cookbook Copyright © 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Amarabha Banerjee Acquisition Editor: Reshma Raman Content Development Editor: Jason Pereira Technical Editor: Prajakta Mhatre Copy Editor: Dhanya Baburaj Project Coordinator: Sheejal Shah Proofreader: Safis Editing Indexer: Rekha Nair Production Coordinator: Deepika Naik First published: March 2018 Production reference: 1270318 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78862-230-1
www.packtpub.com
I dedicate my work to my aunt, Elena, and my cousins, Julia and Laura. This book would not have been possible without their love and support.
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content
PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors About the author Alejandro Rodas de Paz is a computer engineer from Seville, Spain. He has developed several projects with Python, from web crawlers to artificial intelligence algorithms. He has also used Tkinter for building an application for the lighting system of the city of Almere (Netherlands). Prior to this publication, Alejandro co-wrote Packt's title Python Game Development by Example, and collaborated as a technical reviewer on the book Tkinter GUI Application Development Hotshot. I would like to thank the exceptional team at Packt Publishing for their assistance during this journey, and words cannot express my gratitude to Jason and Prajakta for their understanding and immense patience.
About the reviewers Javier Becerra Elcinto received his PhD in image processing from the Université de Bordeaux in 2006. In 2010 he started working as a self-employed developer working with Python and C++, and in 2014 he cofounded Panoimagen S.L. There, he has continued to develop software for industrial and precision agriculture applications. Javier lectures regularly on scientific programming in private companies and several Spanish research institutions. Marcos Perez Gonzalez works in the treatment of multimedia document databases with Python. Previously, he had been using it as glue between projects and technologies for more than 10 years. At the beginning of his career, he used Python for embedding purposes and multiplatform GUI programming. He is a member of the Python Spain association and the Python Madrid meetup. Moreover, he has been a member of the board of directors of the Spanish Association of Computer Engineering. He holds a master's degree in computer engineering from the University of Valladolid. Bryson Tyrrell is a systems development engineer at Jamf in Minneapolis, Minnesota. In addition to the responsibilities of his role, Bryson has spoken at various IT conferences (including the Jamf Nation User Conference, Penn State MacAdmins, Mac Admins and Developers UK, and Atlassian Summit) and maintains a number of open source tools for the Mac admin community on his GitHub account.
Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Table of Contents Preface Chapter 1: Getting Started with Tkinter Introduction Structuring a Tkinter application Getting ready How to do it... How it works... There's more...
Working with buttons How to do it... How it works... There's more...
Creating text entries How to do it... How it works... There's more... See also
Tracing text changes How to do it... How it works... There's more... See also
Validating a text entry How to do it... How it works... There's more... See also
Selecting numerical values How to do it... How it works... There's more... See also
Creating selections with radio buttons How to do it... How it works... There's more...
Implementing switches with checkboxes How to do it...
1 7 7 8 8 9 10 10 11 11 12 12 13 13 14 14 15 15 15 16 17 17 17 17 18 19 20 20 20 21 21 21 22 22 23 23 24 24
Table of Contents
How it works... There's more... See also
Displaying a list of items How to do it... How it works... There's more... See also
Handling mouse and keyboard events How to do it... How it works... There's more... See also
Setting the main window's icon, title, and size How to do it... How it works... There's more...
Chapter 2: Window Layout Introduction Grouping widgets with frames Getting ready How to do it… How it works… There's more… See also
Using the Pack geometry manager Getting ready How to do it… How it works… There's more… See also
Using the Grid geometry manager Getting ready How to do it… How it works… There's more… See also
Using the Place geometry manager Getting ready How to do it… How it works… There's more… See also
Grouping inputs with the LabelFrame widget [ ii ]
25 25 25 25 26 27 28 28 28 29 30 32 32 32 32 33 33 35 35 36 36 36 38 38 39 39 39 40 41 42 42 42 43 43 44 45 45 45 46 46 47 48 48 48
Table of Contents
Getting ready How to do it… How it works…
Dynamically laying out widgets Getting ready How to do it… How it works…
Creating horizontal and vertical scrollbars Getting ready How to do it… How it works… There's more… See also
Chapter 3: Customizing Widgets Introduction Working with colors
48 49 50 50 51 51 52 53 53 54 55 57 57 59 59 60 60 61 62 63 63 63 64 65 65 66 66 66 67 67 69 69 69 70 70 71 71 72 72 73 74 75 75 76
Getting ready How to do it... How it works... There's more...
Setting widget fonts Getting ready How to do it... How it works... There's more... See also
Using the options database Getting ready How to do it... How it works... There's more... See also
Changing the cursor icon Getting ready How to do it... How it works... There's more...
Introducing the Text widget Getting ready How to do it... How it works...
Adding tags to the Text widget Getting ready How to do it...
[ iii ]
Table of Contents
How it works... There's more... See also
76 77 78
Chapter 4: Dialogs and Menus Introduction Showing alert dialogs
79 79 80 80 82 83 83 84 84 85 85 86 87 88 89 89 90 90 91 91 92 92 92 93 93 94 94 95 96 96 97 97 98 99 100 101 101 102 102 103 103
Getting ready How to do it... How it works...
Asking for user confirmation Getting ready How to do it... How it works...
Choosing files and directories Getting ready How to do it... How it works... There's more...
Saving data into a file Getting ready How to do it... How it works... There's more... See also
Creating a menu bar Getting ready How to do it... How it works...
Using variables in menus Getting ready How to do it... How it works...
Displaying context menus Getting ready How to do it... How it works... There's more...
Opening a secondary window Getting ready How to do it... How it works...
Handling window deletion Getting ready How to do it...
[ iv ]
Table of Contents
How it works... There's more...
Passing variables between windows Getting ready How to do it... How it works...
Chapter 5: Object-Oriented Programming and MVC Introduction Structuring our data with a class Getting ready How to do it... How it works... There's more...
Composing widgets to display information Getting ready How to do it... How it works...
Reading records from a CSV file Getting ready How to do it... How it works...
Persisting data into a SQLite database Getting ready How to do it... How it works... See also
Refactoring using the MVC pattern Getting ready How to do it... How it works... There's more...
Chapter 6: Asynchronous Programming Introduction Scheduling actions Getting ready How to do it... How it works... There's more... See also
Running methods on threads How to do it... How it works... There's more...
[v]
104 104 105 105 105 107 109 109 110 110 110 111 112 113 113 114 115 116 116 117 118 118 119 120 124 125 125 126 126 130 131 133 133 134 134 135 136 137 137 137 137 138 140
Table of Contents Thread methods - start, run, and join Parameterizing the target method
Performing HTTP requests Getting ready How to do it... How it works... See also
Connecting threads with a progress bar Getting ready How to do it... How it works... See also
Canceling scheduled actions Getting ready How to do it... How it works... There's more...
Handling idle tasks Getting ready How to do it... How it works...
Spawning separate processes Getting ready How to do it... How it works...
Chapter 7: Canvas and Graphics Introduction Understanding the coordinate system How to do it... How it works... There's more...
Drawing lines and arrows Getting ready How to do it... How it works...
Writing text on a canvas Getting ready How to do it... How it works... There's more...
Placing the text by its upper-left corner Setting line wrapping
Adding shapes to the canvas Getting ready
[ vi ]
140 140 141 141 142 143 144 144 145 145 146 148 148 148 149 150 150 151 151 151 152 152 153 153 154 157 157 158 158 159 160 160 160 161 162 163 164 164 165 166 166 167 167 168
Table of Contents
How to do it... How it works... See also
Finding items by their position Getting ready How to do it... How it works...
Moving canvas items How to do it... How it works... There's more... See also
Detecting collisions between items Getting ready How to do it... How it works... There's more...
Deleting items from a canvas Getting ready How to do it... How it works...
Binding events to canvas items Getting ready How to do it... How it works... There's more... See also
Rendering a canvas into a PostScript file How to do it... How it works... There's more...
Chapter 8: Themed Widgets Introduction Replacing basic widget classes Getting ready How to do it... How it works... See also
Creating an editable drop-down with Combobox Getting ready How to do it... How it works... There's more...
Using the Treeview widget [ vii ]
168 170 172 172 172 173 174 174 175 176 177 178 178 178 179 180 181 181 182 182 184 184 184 185 186 187 187 187 188 188 189 191 191 192 192 193 194 195 195 195 196 197 198 198
Table of Contents
Getting ready How to do it... How it works... There's more...
Using tags in Treeview items
See also
Populating nested items in a Treeview Getting ready How to do it... How it works...
Displaying tabbable panes with Notebook Getting ready How to do it... How it works... There's more...
Applying Ttk styling How to do it... How it works...
Creating a datepicker widget Getting ready How to do it... How it works... See also
199 199 200 202 202 203 203 204 204 206 208 208 208 209 210 211 211 212 212 213 213 216 220
Other Books You May Enjoy
221
Index
225
[ viii ]
Preface As one of the more versatile programming languages, Python is well known for its batteries-included philosophy, which includes a rich set of modules in its standard library; Tkinter is the library used to build desktop applications. Built over the Tk GUI toolkit, Tkinter is a common choice for rapid GUI development, and complex applications can benefit from the full capabilities of this library. This book covers all of your Tkinter and Python GUI development problems and solutions. Tkinter GUI Application Development Cookbook starts with an overview of Tkinter classes and at the same time provides recipes for basic topics, such as layout patterns and event handling. Next, this book covers how to develop common GUI patterns, such as entering and saving data, navigating through menus and dialogs, and performing long-running actions in the background. You can then make your apps leverage network resources effectively and perform graphical operations on a canvas and related tasks such as detecting collisions between items. Finally, this book covers using themed widgets, an extension of Tk widgets that have a more native look and feel. By the end of the book, you will have an in-depth knowledge of Tkinter classes and know how to use them to build efficient and rich GUI applications.
Who this book is for This book targets Python developers who are familiar with the basics of the language—syntax, data structures, and OOP—wants to learn effective solutions to the common challenges of GUI development, and desires to discover interesting capabilities that Tkinter can offer to build complex applications. You do not need previous experience with Tkinter or other GUI development libraries since the first part of the book will teach the basics of the library through the introductory use cases.
Tkinter GUI Application Development Cookbook As one of the more versatile programming languages, Python is well-known for its batteries-included philosophy, which includes a rich set of modules in its standard library; Tkinter is the library included for building desktop applications. Due to this, Tkinter is a common choice for rapid GUI development, and more complex applications can benefit from the full capabilities of this library. This book covers all of your Tkinter and Python GUI development problems and solutions. Tkinter GUI Application Development Cookbook starts with an overview of Tkinter classes and at the same time provides recipes for basic topics, such as layout patterns and event handling. Next, we cover how to develop common GUI patterns, such as entering and saving data, navigating through menus and dialogs, and performing long-running actions in the background. You can then make your apps leverage network resources effectively and perform graphical operations on a canvas and related tasks, such as detecting collisions between items. Finally, this book covers how to use themed widgets, an extension of Tk widgets that have a more native look and feel. By the end of the book, you will have an in-depth knowledge of Tkinter classes, and will know how to use them to build efficient and rich GUI applications.
Things you will learn:
• Add widgets and handle user events • Lay out widgets within windows using frames and the different geometry managers
• Configure widgets so that they
have customized appearances and behaviors
• Improve the navigation of your apps with menus and dialogs
• Apply object-oriented programming techniques to Tkinter applications
• Use threads to achieve
responsiveness and update the GUI
• Explore the capabilities of the Canvas
widget and the types of items that can be added to it
• Extend Tkinter applications with the TTK (themed Tkinter) module
FOR SALE IN INDIA ONLY
www.packtpub.com