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

Setting the focus to a widget and disabling widgets

While our GUI is nicely improving, it would be more convenient and useful to have the cursor appear in the Entry widget as soon as the GUI appears.

In this recipe, we learn how to make the cursor appear in the Entry box for immediate text Entry rather than the need for the user to click into the Entry widget to give it the focus method before typing into the entry widget.

Getting ready

This recipe extends the previous recipe, Creating textbox widgets. Python is truly great. All we have to do to set the focus to a specific control when the GUI appears is call the focus() method on an instance of a tkinter widget we previously created. In our current GUI example, we assigned the ttk.Entry class instance to a variable named name_entered. Now, we can give it the focus.

How to do it...

Place the following code just above the previous code, which is located at the bottom of the module, and which starts the main window's event loop, like we did in the previous recipes:

  1. Start with the GUI_textbox_widget.py module and save it as GUI_set_focus.py.
  2. Use the name_entered variable we assigned the ttk Entry widget instance to and call the focus() method on this variable:
name_entered.focus()

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

  1. Run the code and observe the output.

If you get some errors, make sure you are placing calls to variables below the code where they are declared. We are not using OOP as of now, so this is still necessary. Later, it will no longer be necessary to do this.

On a Mac, you might have to set the focus to the GUI window first before being able to set the focus to the Entry widget in this window.

Adding line 38 of the Python code places the cursor in our text Entry widget, giving the text Entry widget the focus. As soon as the GUI appears, we can type into this textbox without having to click it first. The resulting GUI now looks like this, with the cursor inside the Entry widget:

Note how the cursor now defaults to residing inside the text entry box.

We can also disable widgets. Here, we are disabling the button to show the principle. In larger GUI applications, the ability to disable widgets gives you control when you want to make things read only. Most likely, those would be combobox widgets and Entry widgets, but as we have not yet gotten to those widgets yet, we will use our button.

To disable widgets, we will set an attribute on the widget. We can make the button disabled by adding the following code below line 37 of the Python code to create the button:

  1. Use the GUI_set_focus.py module and save it as GUI_disable_button_widget.py.
  2. Use the action button variable to call the configure method and set the state attribute to disabled:
action.configure(state='disabled')
  1. Call the focus() method on the name_entered variable:
name_entered.focus()

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

  1. Run the code. After adding the preceding line of Python code, clicking the button no longer creates an action:

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

How it works...

This code is self-explanatory. In line 39, we set the focus to one control, and in line 37, we disable another widget. Good naming in programming languages helps to eliminate lengthy explanations. Later in this book, there will be some advanced tips on how to do this while programming at work or practicing our programming skills at home.

We've successfully learned how to set the focus to a widget and disable widgets. 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