Sun Bear
Sun Bear

Reputation: 8297

How to allow continuous switching of focus in a group of tkinter widgets w/o getting stuck at the last widget?

I have several tkinter Entry and Combobox widgets that I can use the Tab key to change focus from one widget to another. However, when the focus arrives at the last widget, the focus remains at the last widget and does not continue back to the first widget when Tab is pressed. How can i get this continuation in the switching of focus?

Upvotes: 0

Views: 222

Answers (1)

Sun Bear
Sun Bear

Reputation: 8297

I discovered the reasons to my issue:

  1. I had created several FrameButton widgets before creating the Entry widgets and Combobox widget. By default, these FrameButtons had takefocus=True option defined.

  2. These Framebuttons, being a ttk type widget, were created using a custom style layout such that it only had padding and label elements. That is, it did not have a focus element.

Hence when the Tab key was pressed after the Combobox widget, focus had indeed move to the Framebutons. But because these FrameButtons did not consist a focus element, the focus on these buttons could not be seen. And because there were many such buttons and the Combobox fieldbackground was selected after focus had left it, the effects of pressing the Tab key felt as it focus had stopped at the Combobox. To visualise focus in the FrameButtons, I only had to ensure these button widgets had a focus layout element like so:

    buttonlayout =[('Button.focus', {'sticky': 'nswe', 'children':
                    [('Button.padding', {'sticky':'nswe', 'children':
                      [('Button.label', {'sticky': 'nswe'})]})]})]
    s.layout('my.TButton', buttonlayout)

Summary:

  1. By default, Tab keypress will cycle through all widgets defined with option takefocus=True in the order in which they are created.
  2. If focus appears to stop at a certain widget, do ensure the ttk widgets in your tkinter GUI has the focus element defined in its style layout. Else these widgets will not show they have focus.

Upvotes: 0

Related Questions