Tkinter GUI Application Development Cookbook - Alejandro Rodas de Paz - E-Book

Tkinter GUI Application Development Cookbook E-Book

Alejandro Rodas de Paz

0,0
31,19 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

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 using themed widgets, an extension of Tk widgets that have a more native look and feel. Finally, this book covers using the canvas and themed widgets.
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.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 202

Veröffentlichungsjahr: 2018

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



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 BanerjeeAcquisition Editor:Reshma RamanContent Development Editor: Jason PereiraTechnical Editor: Prajakta MhatreCopy Editor: Dhanya BaburajProject Coordinator:Sheejal ShahProofreader: Safis EditingIndexer: Rekha NairProduction 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 Elcintoreceived his PhD in image processing from theUniversité 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

Title Page

Copyright and Credits

Tkinter GUI Application Development Cookbook

Dedication

Packt Upsell

Why subscribe?

PacktPub.com

Contributors

About the author

About the reviewers

Packt is searching for authors like you

Preface

Who this book is for

What this book covers

To get the most out of this book

Download the example code files

Download the color images

Conventions used

Get in touch

Reviews

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...

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...

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

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

Customizing Widgets

Introduction

Working with colors

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...

How it works...

There's more...

See also

Dialogs and Menus

Introduction

Showing alert dialogs

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...

How it works...

There's more...

Passing variables between windows

Getting ready

How to do it...

How it works...

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...

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...

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...

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

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...

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

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

Other Books You May Enjoy

Leave a review - let other readers know what you think

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.

What this book covers

Chapter 1, Getting Started with Tkinter, introduces the structure of a Tkinter program and shows you how to perform the most common tasks, such as creating widgets and handling user events.

Chapter 2, Window Layout, demonstrates how to place widgets using geometry managers and improve the layout of large applications.

Chapter 3, Customizing Widgets, dives deeper into Tkinter's widget configuration and appearance customization.

Chapter 4, Dialogs and Menus, teaches you how to improve the navigation of Tkinter apps with menus and dialogs.

Chapter 5, Object-Oriented Programming and MVC, teaches you how to effectively apply design patterns in your Tkinter applications.

Chapter 6, Asynchronous Programming, covers several recipes to execute long-running actions without freezing the application—a recurring problem in GUI development.

Chapter 7, Canvas and Graphics, explores the Canvas widget and the types of items you can add to the canvas and how to manipulate them.

Chapter 8, Themed Widgets, teaches you how to extend Tkinter applications with the Tk-themed widget set.

To get the most out of this book

To get up and running, users will need to have the following technologies installed:

Python 3.x

Any operating system

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

Log in or register at

www.packtpub.com

.

Select the

SUPPORT

tab.

Click on

Code Downloads & Errata

.

Enter the name of the book in the

Search

box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

WinRAR/7-Zip for Windows

Zipeg/iZip/UnRarX for Mac

7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Tkinter-GUI-Application-Development-Cookbook. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available athttps://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here:https://www.packtpub.com/sites/default/files/downloads/TkinterGUIApplicationDevelopmentCookbook_ColorImages.pdf.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.

Getting Started with Tkinter

In this chapter, we will cover the following recipes:

Structuring a Tkinter application

Working with buttons

Creating text entries

Tracing text changes

Validating a text entry

Selecting numerical values

Creating selections with radio buttons

Implementing switches with checkboxes

Displaying a list of items

Handling mouse and keyboard events

Setting the main window's icon, title, and size

Introduction

Thanks to its clear syntax and the wide ecosystem of libraries and tools, Python has become a popular and general-purpose programming language. From web development to Natural Language Processing (NLP), you can easily find an open source library that fits the need of your application domain, and in the last instance, you can always use any of the modules included in the Python standard library.

The standard library follows the "batteries-included" philosophy, which means that it contains a large collection of utilities: regular expressions, mathematical functions, networking, and so on. The standard Graphical User Interface (GUI) package of this library is Tkinter, a thin object-oriented layer on top of Tcl/Tk.

Starting from Python 3, the Tkinter module was renamed to tkinter (with a lowercase t). It also affects to the tkinter.ttk and tkinter.tix extensions. We will dive into the tkinter.ttk module in the last chapter of this book, since the tkinter.tix module is officially deprecated.

In this chapter, we will explore several patterns for some basic classes of the tkinter module and some methods that are common to all widget subclasses.

Structuring a Tkinter application

One of the main advantages of making applications with Tkinter is that it is very easy to set up a basic GUI with a script of a few lines. As the programs get more complex, it becomes more difficult to separate logically each part, so an organized structure will help us to keep our code clean.

How it works...

First, we replaced the wildcard import with the import ... as syntax to have better control over our global namespace.

Then, we defined our App