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 buttons and changing their text attributes

In this recipe, we will add a button widget, and we will use this button to change an attribute of another widget that is a part of our GUI. This introduces us to callback functions and event handling in a Python GUI environment.

Getting ready

How to do it...

In this recipe, we will update the label we added in the previous recipe as well as the text attribute of the button. The steps to add a button that performs an action when clicked are as follows:

  1. Start with the GUI_add_label.py module and save it as GUI_create_button_change_property.py.
  2. Define a function and name it click_me():
def click_me()
  1. Use ttk to create a button and give it a text attribute:
action.configure(text="** I have been Clicked! **")
a_label.configure (foreground='red')
a_label.configure(text='A Red Label')
  1. Bind the function to the button:
action = ttk.Button(win, text="Click Me!", command=click_me)
  1. Use the grid layout to position the button:
 action.grid(column=1, row=0)

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

  1. Run the code and observe the output.

The following screenshot shows how our GUI looks before clicking the button:

After clicking the button, the color of the label changed and so did the text of the button, which can be seen in the following screenshot:

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

How it works...

In line 19, we assign the label to a variable, a_label, and in line 20, we use this variable to position the label within the form. We need this variable in order to change its attributes in the click_me() function. By default, this is a module-level variable, so as long as we declare the variable above the function that calls it, we can access it inside the function.

Line 23 is the event handler that is invoked once the button gets clicked.

In line 29, we create the button and bind the command to the click_me() function.

GUIs are event-driven. Clicking the button creates an event. We bind what happens when this event occurs in the callback function using the command attribute of the ttk.Button widget. Notice how we do not use parentheses, only the name click_me.

Lines 20 and 30 both use the grid layout manager, which will be discussed in Chapter 2, Layout Management, in the Using the grid layout manager recipe. This aligns both the label and the button. We also change the text of the label to include the word red to make it more obvious that the color has been changed. We will continue to add more and more widgets to our GUI, and we will make use of many built-in attributes in the other recipes of this book.

We've successfully learned how to create buttons and change their text attributes. Now, let's move on to the next recipe.