Python GUI Programming with Tkinter
eBook - ePub

Python GUI Programming with Tkinter

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

  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

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

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
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.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
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.
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.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere โ€” even offline. Perfect for commutes or when youโ€™re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
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 & Application Development. We have over one million books available in our catalogue for you to explore.

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

  1. Preface
  2. Introduction to Tkinter
  3. Designing GUI Applications
  4. Creating Basic Forms with Tkinter and Ttk Widgets
  5. Organizing Our Code with Classes
  6. Reducing User Error with Validation and Automation
  7. Planning for the Expansion of Our Application
  8. Creating Menus with Menu and Tkinter Dialogs
  9. Navigating Records with Treeview and Notebook
  10. Improving the Look with Styles and Themes
  11. Maintaining Cross-Platform Compatibility
  12. Creating Automated Tests with unittest
  13. Improving Data Storage with SQL
  14. Connecting to the Cloud
  15. Asynchronous Programming with Thread and Queue
  16. Visualizing Data Using the Canvas Widget
  17. Packaging with setuptools and cxFreeze
  18. Appendices
  19. A: A Quick Primer on reStructuredText
  20. B: A Quick SQL Tutorial
  21. Other Books You May Enjoy
  22. Index