Python GUI Programming with Tkinter
eBook - ePub

Python GUI Programming with Tkinter

Design and build functional and user-friendly GUI applications, 2nd Edition

Alan D. Moore

Share book
  1. 664 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Python GUI Programming with Tkinter

Design and build functional and user-friendly GUI applications, 2nd Edition

Alan D. Moore

Book details
Book preview
Table of contents
Citations

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

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Python GUI Programming with Tkinter an online PDF/ePUB?
Yes, you can access Python GUI Programming with Tkinter by Alan D. Moore in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Python. We have over one million books available in our catalogue for you to explore.

Information

Year
2021
ISBN
9781801818865
Edition
2

9

Improving the Look with Styles and Themes

While programs can be perfectly functional with plain text in shades of black, white, and gray, the subtle use of colors, fonts, and images can enhance the visual appeal and usability of even the most utilitarian applications. Your data entry application is no exception, and the current round of requests brought to you by your coworkers seems to require some retooling of the application's look and feel.
Specifically, you've been asked to address these points:
  • 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 addition to the user's requests, you'd like to make your application look more professional by adding some icons to your buttons and menu.
In this chapter, we're going to learn about some features of Tkinter that will help us to solve these issues:
  • 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

To solve the corporate logo issue and spruce up our application with some icons, we're going to need to understand how to work with images in Tkinter. Tkinter provides access to image files through two classes: the PhotoImage class and the BitmapImage class. Let's see how these classes can help us add graphics to our application.

Tkinter PhotoImage

Many Tkinter widgets, including 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.
Making a 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) 
Note that if we pass both an 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 ) 
In this situation, the first label would only show the 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

When using a 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() 
If you run this example, you'll notice that no image gets displayed. That's because the variable holding the 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, ...

Table of contents