Book Image

Python GUI Programming Cookbook - Third Edition

By : Burkhard Meier
Book Image

Python GUI Programming Cookbook - Third Edition

By: Burkhard Meier

Overview of this book

Python is a multi-domain, interpreted programming language that is easy to learn and implement. With its wide support for frameworks to develop GUIs, you can build interactive and beautiful GUI-based applications easily using Python. This third edition of Python GUI Programming Cookbook follows a task-based approach to help you create effective GUIs with the smallest amount of code. Every recipe in this book builds upon the last to create an entire, real-life GUI application. These recipes also help you solve problems that you might encounter while developing GUIs. This book mainly focuses on using Python’s built-in tkinter GUI framework. You'll learn how to create GUIs in Python using simple programming styles and object-oriented programming (OOP). As you add more widgets and expand your GUI, you will learn how to connect to networks, databases, and graphical libraries that greatly enhance the functionality of your GUI. You’ll also learn how to use threading to ensure that your GUI doesn't become unresponsive. Toward the end, you’ll learn about the versatile PyQt GUI framework, which comes along with its own visual editor that allows you to design GUIs using drag and drop features. By the end of the book, you’ll be an expert in designing Python GUIs and be able to develop a variety of GUI applications with ease.
Table of Contents (13 chapters)

Creating textbox widgets

In tkinter, a typical one-line textbox widget is called Entry. In this recipe, we will add such an Entry widget to our GUI. We will make our label more useful by describing what the Entry widget is doing for the user.

Getting ready

This recipe builds upon the Creating buttons and changing their text attributes recipe, so download it from the repository and start working on it.

How to do it...

Follow these steps to create textbox widgets:

  1. Start with the GUI_create_button_change_property.py module and save it as GUI_textbox_widget.py.
  2. Use the tk alias of tkinter to create a StringVar variable:
name = tk.StringVar()
  1. Create a ttk.Entry widget and assign it to another variable:
name_entered = ttk.Entry(win, width=12, textvariable=name)
  1. Use this variable to position the Entry widget:
name_entered.grid(column=0, row=1)

The preceding instructions produce the following code (GUI_textbox_widget.py):

  1. Run the code and observe the output; our GUI looks like this:
  1. Enter some text and click the button; we will see that there is a change in the GUI, which is as follows:

Let's go behind the scenes to understand the code better.

How it works...

In step 1 we are creating a new Python module, and in step 2 we are adding a StringVar type of tkinter and saving it in the name variable. We use this variable when we are creating an Entry widget and assigning it to the textvariable attribute of the Entry widget. Whenever we type some text into the Entry widget, this text will be saved in the name variable.

In step 4, we position the Entry widget and the preceding screenshot shows the entire code.

In line 24, as shown in the screenshot, we get the value of the Entry widget using name.get().

When we created our button, we saved a reference to it in the action variable. We use the action variable to call the configure method of the button, which then updates the text of our button.

We have not used OOP yet, so how come we can access the value of a variable that was not even declared yet? Without using OOP classes, in Python procedural coding, we have to physically place a name above a statement that tries to use that name. So, how does this work (it does)? The answer to this is that the button click event is a callback function, and by the time the button is clicked by a user, the variables referenced in this function are known and do exist.

Line 27 gives our label a more meaningful name; for now, it describes the textbox below it. We moved the button down next to the label to visually associate the two. We are still using the grid layout manager, which will be explained in more detail in Chapter 2, Layout Management.

Line 30 creates a variable, name. This variable is bound to the Entry widget and, in our click_me() function, we are able to retrieve the value of the Entry widget by calling get() on this variable. This works like a charm.

Now we observe that while the button displays the entire text we entered (and more), the textbox Entry widget did not expand. The reason for this is that we hardcoded it to a width of 12 in line 31.

Python is a dynamically typed language and infers the type from the assignment. What this means is that if we assign a string to the name variable, it will be of the string type, and if we assign an integer to name, its type will be an integer.

Using tkinter, we have to declare the name variable as the tk.StringVar() type before we can use it successfully. The reason is that tkinter is not Python. We can use it with Python, but it is not the same language. See https://wiki.python.org/moin/TkInter for more information.

We've successfully learned how to create textbox widgets. Now, let's move on to the next recipe.