Reader small image

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

Product typeBook
Published inOct 2019
Reading LevelIntermediate
Publisher
ISBN-139781838827540
Edition3rd Edition
Languages
Right arrow
Author (1)
Burkhard Meier
Burkhard Meier
author image
Burkhard Meier

Burkhard Meier is a professional software test automation designer, developer, and analyst. He has more than 17 years' professional experience working for several software companies in California, USA. He is the author of Python GUI Programming Cookbook, First and Second Edition. This book is also available as a Packt video course. He is also the author of the Python Projects Packt video course. In his professional career, he developed advanced in-house testing frameworks written in Python 3. He also developed advanced test automation GUIs in Python, which highly increased the productivity of the software development testing team. When not dreaming in Python code, he reads programming books about design, likes to go for long walks, and reads classical poetry.
Read more about Burkhard Meier

Right arrow

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.

Previous PageNext Page
You have been reading a chapter from
Python GUI Programming Cookbook. - Third Edition
Published in: Oct 2019Publisher: 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.
undefined
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

Author (1)

author image
Burkhard Meier

Burkhard Meier is a professional software test automation designer, developer, and analyst. He has more than 17 years' professional experience working for several software companies in California, USA. He is the author of Python GUI Programming Cookbook, First and Second Edition. This book is also available as a Packt video course. He is also the author of the Python Projects Packt video course. In his professional career, he developed advanced in-house testing frameworks written in Python 3. He also developed advanced test automation GUIs in Python, which highly increased the productivity of the software development testing team. When not dreaming in Python code, he reads programming books about design, likes to go for long walks, and reads classical poetry.
Read more about Burkhard Meier