Tải bản đầy đủ (.pdf) (353 trang)

Tkinter GUI programming by example learn to create modern GUIs using tkinter by building real world projects in python

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (4.7 MB, 353 trang )


Tkinter GUI Programming by Example

Learn to create modern GUIs using Tkinter by building
real-world projects in Python

David Love


BIRMINGHAM - MUMBAI



Tkinter GUI Programming by Example
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: Aaron Lazar
Acquisition Editor: Denim Pinto
Content Development Editor: Anugraha Arunagiri
Technical Editor: Subhalaxmi Nadar
Copy Editor: Safis Editing
Project Coordinator: Ulhas Kambali
Proofreader: Safis Editing
Indexer: Aishwarya Gangawane
Graphics: Tania Dutta


Production Coordinator: Arvindkumar Gupta
First published: April 2018
Production reference: 1240418
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78862-748-1
www.packtpub.com


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 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
David Love is a web developer from Kent, England. He has worked on a multitude of different
systems over his career. Programming languages in his arsenal include Python, PHP, and JavaScript.
He is well-trained in Linux server management and its relevant technologies, including MySQL,
PostgreSQL, NGINX, and supervisor.
David has written an e-book called Tkinter By Example, which is available for free under a Creative
Commons licenses and maintains an ever-growing blog post named The Tkinter Cookbook, full of
small examples on how to perform some specific tasks.


About the reviewer
Erik S. Rapert is a programmer and a twin who loves Linux and video games. He lives in Dallas
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 using Python and Tkinter, web
development with PHP or Ruby, and developing cutting-edge virtual reality using C++. He has used a
very broad range of programming languages, but Python is one of his favorites.
Thank you William C. Slater for teaching me how to write software. Thank you Andrew Closson for being a teacher. Thank you
Ashley N. Tharp for being you.



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 Programming by Example
Packt Upsell
Why subscribe?
PacktPub.com
Contributors
About the author
About the reviewer
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


1.


Meet Tkinter
Installation
How will the code be structured?
Geometry managers
pack
grid
place
To pack or to grid?
Getting going
Adding interactivity
Using variables
Our first try
Creating Tkinter-compatible variables
Using and updating
Fixing our application
Showing messages
Showing information with showinfo
Showing warnings or errors
Getting feedback from the user
Getting text input
Summary


2.

Back to the Command Line – Basic Blackjack
Python's class system
Instances
Inheritance

Blackjack's classes
The Card class
The Deck class
The Hand class
The Game class and main loop
Command line versus GUI
Interactivity
Familiarity
Ease of use
Size and portability
Summary


3.

Jack is Back in Style – the Blackjack GUI
Moving from the command line to a graphical interface
The Canvas widget
Creating a graphical blackjack game
Card, Deck, and Hand
The GameState class
The GameScreen class
Playing our game
Summary


4.

The Finishing Touches – Sound and Animation
Python's module system

The blackjack packages
The casino package
The casino_sounds package
Setting up a virtual environment
Creating the package
The blackjack.py file
Initializing the GameWindow class
The GameScreen class
The GameState class
Choosing to hit
Choosing to stick
Running out of money
Finishing off
Summary


5.

Creating a Highly Customizable Python Editor
The ttk submodule
Styling a tk widget
Styling a ttk widget
Ttk style inheritance
Beginning our text editor
Tkinter's event system
Binding an event
Overwriting default events
Generating events
Events in our text editor
A second top-level window

Summary


6.

Color Me Impressed! – Adding Syntax Highlighting
Tkinter's indexing system
Getting the cursor's position
Named indexes
Special strings
Line endings
Horizontal movement
Vertical movement
Line beginning and end
Word beginning and end
Expanding our demo
Using tags
Searching text
Adding syntax highlighting to our text editor
The Highlighter class
Using our Highlighter class
The LineNumbers class
Using our LineNumbers class
Integrating our FindWindow class
Using our FindWindow class
Summary


7.


Not Just for Restaurants – All About Menus
The Menu widget
A menu bar
A floating menu
Adding a menu bar to our text editor
Adding a context menu to our text editor
Handling files
Changing the syntax highlighting
Changing the editor's font
The Listbox widget
The Spinbox widget
Saving the user's choices
Changing the editor's color scheme
Summary


8.

Talk Python to Me – a Chat Application
Creating a scrollable frame
Creating our FriendsList class
Creating our ChatWindow class
Creating our SmilieSelect class
Summary


9.

Connecting – Getting Our Chat Client Online
Introduction to flask

Our first web page
Using JSON
The requests module
Sending a GET request
Sending a POST request
The sqlite3 module
Creating a database and table
Adding data to a SQLite database
Selecting data from a SQLite database
Linking flask and sqlite
Updating our FriendsList class
Creating the Requester class
Connecting our FriendsList to our web service
Connecting our ChatWindow
Updating our server to store conversations
Creating the Conversation class
Using the Conversation class in our server
Adding the new endpoints to our Requester
Updating our ChatWindow class to send requests to the server
Summary


10.

Making Friends – Finishing Our Chat Application
Using threads
Why use a thread with a GUI application?
Using a thread
Adding a Thread to our ChatWindow
Creating new endpoints

The ListeningThread class
Implementing the ListeningThread class in our ChatWindow
Allowing users to upload avatars
The AvatarWindow class
Adjusting the database
Adding server endpoints
Updating the FriendsList class
Manipulating images with PIL
Adding and blocking other users
New database table
Creating the server endpoints
Tying it all together
Summary


11.

Wrapping Up – Packaging Our Applications to Share
Unexplored widgets
The LabelFrame widget
The Checkbutton and Radiobutton widgets
The OptionMenu and Combobox widgets
The Notebook widget
Packaging applications
Adjusting our text editor for portability
Preparing to package with setup.py
Installing our text editor
Cross-platform using Pip
Windows
Linux

macOS
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think


Preface
Welcome to Tkinter GUI Programming by Example. We will be exploring how to use the Tkinter
library, which is included alongside most Python installs. This framework is very simple to use, and
has a powerful event-handling system and an open license, perfect for anyone who wishes to quickly
write and share graphical applications.
Throughout this book, we will be creating three powerful desktop applications, learning about the
variety of widgets available in Tkinter. After we have these applications polished, we will then learn
how to share them with other people by packaging them up for sharing. When you have finished this
book, you will have in-depth knowledge of Tkinter, its widgets, GUI-design principles, and
packaging Python projects.


×