Python GUI Programming with Tkinter
Design and build functional and user-friendly GUI applications, 2nd Edition
Alan D. Moore
- 664 pages
- English
- ePUB (mobile friendly)
- Available on iOS & Android
Python GUI Programming with Tkinter
Design and build functional and user-friendly GUI applications, 2nd Edition
Alan D. Moore
About This Book
Transform your evolving user requirements into feature-rich Tkinter applications
Key Features
- Extensively revised with new content on RESTful networking, classes in Tkinter, and the Notebook widget
- Take advantage of Tkinter's lightweight, portable, and easy-to-use features
- Build better-organized code and learn to manage an evolving codebase
Book Description
Tkinter is widely used to build GUIs in Python due to its simplicity. In this book, you'll discover Tkinter's strengths and overcome its challenges as you learn to develop fully featured GUI applications.
Python GUI Programming with Tkinter, Second Edition, will not only provide you with a working knowledge of the Tkinter GUI library, but also a valuable set of skills that will enable you to plan, implement, and maintain larger applications. You'll build a full-blown data entry application from scratch, learning how to grow and improve your code in response to continually changing user and business needs.
You'll develop a practical understanding of tools and techniques used to manage this evolving codebase and go beyond the default Tkinter widget capabilities. You'll implement version control and unit testing, separation of concerns through the MVC design pattern, and object-oriented programming to organize your code more cleanly.
You'll also gain experience with technologies often used in workplace applications, such as SQL databases, network services, and data visualization libraries. Finally, you'll package your application for wider distribution and tackle the challenge of maintaining cross-platform compatibility.
What you will learn
- Produce well-organized, functional, and responsive GUI applications
- Extend the functionality of existing widgets using classes and OOP
- Plan wisely for the expansion of your app using MVC and version control
- Make sure your app works as intended through widget validation and unit testing
- Use tools and processes to analyze and respond to user requests
- Become familiar with technologies used in workplace applications, including SQL, HTTP, Matplotlib, threading, and CSV
- Use PostgreSQL authentication to ensure data security for your application
Who this book is for
This book is for programmers who understand the syntax of Python, but do not yet have the skills, techniques, and knowledge to design and implement a complete software application. A fair grasp of basic Python syntax is required.
Frequently asked questions
Information
9
Improving the Look with Styles and Themes
- Your manager has informed you that ABQ's corporate policy requires the company logo to be displayed on all in-house software. You've been provided with a corporate logo image to include in the application.
- The data entry staff have some readability issues with the form. They want more visual distinction between the sections of the form and more visibility for error messages.
- The data entry staff have also requested that you highlight records they've added or updated during a session to help them keep track of their work.
- In Working with images in Tkinter, we'll learn how to add pictures and icons to our Tkinter GUI.
- In Styling Tkinter widgets, we'll learn how to adjust the colors and visual style of Tkinter widgets, both directly and using tags.
- In Working with fonts in Tkinter, we'll learn the ins and outs of using fonts in Tkinter.
- In Styling Ttk widgets, we'll learn how to adjust the look of Ttk widgets using styles and themes.
Working with images in Tkinter
PhotoImage
class and the BitmapImage
class. Let's see how these classes can help us add graphics to our application.Tkinter PhotoImage
Label
and Button
, accept an image
argument that allows us to display an image on the widget. This argument requires that we create and pass in a PhotoImage
(or BitmapImage
) object.PhotoImage
object is fairly simple:myimage = tk.PhotoImage(file='my_image.png')
PhotoImage
is typically called with the keyword argument file
, which is pointed to a file path. Alternatively, you can use the data
argument to point to a bytes
object containing image data. In either case, the resulting object can now be used wherever an image
argument is accepted, such as in a Label
widget:mylabel = tk.Label(root, image=myimage)
image
and text
argument to the Label
initializer, only the image will be displayed by default. To display both, we need to also provide a value for the compound
argument, which determines how the image and text will be arranged with respect to one another. For example:mylabel_1 = tk.Label(root, text='Banana', image=myimage) mylabel_2 = tk.Label( root, text='Plantain', image=myimage, compound=tk.LEFT )
image
; the text
will not be displayed. In the second, since we have specified a compound
value of tk.LEFT
, the image
will be displayed to the left of the text
. compound
can be any of LEFT
, RIGHT
, BOTTOM
, or TOP
(either lowercase strings or the Tkinter constants), and indicates where the image will be placed in relation to the text.PhotoImage and variable scope
PhotoImage
object, it is critical to remember that your application must retain a reference to the object that will stay in scope for as long as the image is shown; otherwise, the image will not appear. To understand what this means, consider the following example:# image_scope_demo.py import tkinter as tk class App(tk.Tk): def __init__(self): super().__init__() smile = tk.PhotoImage(file='smile.gif') tk.Label(self, image=smile).pack() App().mainloop()
PhotoImage
object, smile
, is a local variable, and therefore destroyed as soon as the initializer returns. With no reference remaining to the PhotoImage
object, it is discarded and the image vanishes, ...