Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Python GUI Programming Cookbook. - Third Edition

You're reading from  Python GUI Programming Cookbook. - Third Edition

Product type Book
Published in Oct 2019
Publisher
ISBN-13 9781838827540
Pages 486 pages
Edition 3rd Edition
Languages
Author (1):
Burkhard Meier Burkhard Meier
Profile icon Burkhard Meier

Table of Contents (13) Chapters

Preface Creating the GUI Form and Adding Widgets Layout Management Look and Feel Customization Data and Classes Matplotlib Charts Threads and Networking Storing Data in Our MySQL Database via Our GUI Internationalization and Testing Extending Our GUI with the wxPython Library Building GUIs with PyQt5 Best Practices Other Books You May Enjoy

Data and Classes

In this chapter, we will save our GUI data into tkinter variables. We will also start using object-oriented programming (OOP), writing our own classes in Python. This will lead us to creating reusable OOP components. By the end of this chapter, you will know how to save data from the GUI into local tkinter variables. You will also learn how to display tooltips over widgets, which give the user additional information. Knowing how to do this makes our GUI more functional and easier to use.

Here is an overview of the Python modules for this chapter:

In this chapter, we will use data and OOP classes using Python 3.7 and above. We will cover the following recipes:

  • How to use StringVar()
  • How to get data from a widget
  • Using module-level global variables
  • How coding in classes can improve the GUI
  • Writing callback functions
  • Creating reusable GUI components
...

How to use StringVar()

There are built-in programming types in tkinter that differ slightly from the Python types we are used to programming with. StringVar() is one such tkinter type. This recipe will show you how to use the StringVar() type.

Getting ready

In this recipe, you will learn how to save data from the tkinter GUI into variables so we can use that data. We can set and get their values, which is very similar to how you would use the Java getter/setter methods.

Here are some of the types of code in tkinter:

strVar = StringVar() Holds a string; the default value is an empty string ("")
intVar = IntVar() Holds an integer; the default value is 0
dbVar = DoubleVar() Holds a float; the default value is...

How to get data from a widget

When the user enters data, we want to do something with it in our code. This recipe shows how to capture data in a variable. In the previous recipe, we created several tkinter class variables. They were standalone. Now, we are connecting them to our GUI, using the data we get from the GUI, and storing them in Python variables.

Getting ready

We will continue using the Python GUI we were building in Chapter 3, Look and Feel Customization. We'll reuse and enhance the code from GUI_progressbar.py from that chapter.

How to do it...

We will...

Using module-level global variables

Encapsulation is a major strength in any programming language, enabling us to program using OOP. Python is both OOP-friendly as well as procedural. We can create global variables that are localized to the module they reside in. They are global only to this module, which is one form of encapsulation. Why do we want this? Because as we add more and more functionality to our GUI, we want to avoid naming conflicts that could result in bugs in our code.

We do not want naming clashes creating bugs in our code! Namespaces are one way to avoid these bugs, and in Python, we can do this by using Python modules (which are unofficial namespaces).

Getting ready

We can declare module-level globals in...

How coding in classes can improve the GUI

So far, we have been coding in a procedural style. This is a quick scripting method we can do in Python. When our code gets larger and larger, we need to advance to coding in OOP.

Why?

Because, among many other benefits, OOP allows us to move code around by using methods. Once we use classes, we no longer have to physically place the code above the code that calls it. This gives us great flexibility in organizing our code. We can write the related code next to the other code and no longer have to worry that the code will not run because the code does not sit above the code that calls it. We can take that to some rather fancy extremes by coding up modules that refer to methods that are not being created within that module. They rely on the runtime state having created those methods during the time the code runs.

If the methods we call have...

Writing callback functions

At first, callback functions can seem to be a little bit intimidating. You call the function, passing it some arguments, and then the function tells you that it is really very busy and it will call you back!

You wonder: will this function ever call me back? And how long do I have to wait? In Python, even callback functions are easy and, yes, they usually do call you back. They just have to complete their assigned task first (hey, it was you who coded them in the first place…).

Let's learn a little bit more about what happens when we code callbacks into our GUI. Our GUI is event-driven. After it has been created and displayed onscreen, it typically sits there waiting for an event to happen. It is basically waiting for an event to be sent to it. We can send an event to our GUI by clicking one of its buttons. This creates an event and, in a...

Creating reusable GUI components

We will create reusable GUI components using Python. In this recipe, we will keep it simple by moving our ToolTip class into its own module. Then, we will import and use it to display tooltips over several widgets of our GUI.

Getting ready

We are building our code from Chapter 3, Look and Feel Customization: GUI_tooltip.py. We will start by breaking out our ToolTip class into a separate Python module.

How to do it...

We will create a new Python module and place the ToolTip class code into it and then import this module into our primary...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Python GUI Programming Cookbook. - Third Edition
Published in: Oct 2019 Publisher: ISBN-13: 9781838827540
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}