Web Analytics Made Easy - Statcounter
Skip to content

Widget Access

Most of the user extensions / enhancements are at the "Element" level. You want some Element to do a trick that you cannot do using the existing PySimpleGUI APIs. It's just not possible. What to do?

What you need is access to the underlying GUI framework's "Widget". The good news is that you HAVE that access ready and waiting for you, for all of the ports of PySimpleGUI, not just the tkinter one.

Element.widget is The GUI Widget

The class variable Widget contains the tkinter, Qt, WxPython, or Remi widget. With that variable you can modify that widget directly.

You must first Read or Finalize the window before accessing the Widget class variable

The reason for the Finalize requirement is that until a Window is Read or is Finalized it is not actually created and populated with GUI Widgets. The GUI Widgets are created when you do these 2 operations.

Side note - Calling .Finalize() or .finalize() added onto your window creation is not recommended. Instead use the finalize parameter in the Window call.

OLD WAY:

window = sg.Window('Window Title', layout).Finalize()

THE NEW WAY:

window = sg.Window('Window Title', layout, finalize=True)

It's cleaner and less confusing for beginners who aren't necessarily trained in how chaining calls work. PySimpleGUI.

Example Use of Element.widget

So far there have been 2 uses of this capability. One already mentioned is adding a new capability. The other way it's been used has been to fix a bug or make a workaround for a quirky behavior.

A recent Issue posted was that focus was always being set on a button in a tab when you switch tabs in tkinter. The user didn't want this to happen as it was putting an ugly black line around their nicely made graphical button.

There is no current way in PySimpleGUI to "disable focus" on an Element. That's essentially what was needed, the ability to tell tkinter that this widget should never get focus.

There is a way to tell tkinter that a widget should not get focus. The downside is that if you use your tab key to navigate, that element will never get focus. So, it's not only blocking focus for this automatic problem, but blocking it for all uses. Of course you can still click on the button.

The way through for this user was to modify the tkinter widget directly and tell it not to get focus. This was done in a single line of code:

window[button_key].widget.config(takefocus=0)

The absolute beauty to this solution is that tkinter does NOT need to be imported into the user's program for this statement to run. Python already know what kind of object .widget is and can thus show you the various methods and class variables for that object. Most all tkinter options are strings so you don't need to import tkinter to get any enums.

Finding Your Element's Widget Type

Of course, in order to call the methods or access the object's class variables, you need to know the type of the underlying Widget being used. This document could list them all, but the downside is the widget could change types (not a good thing for people using the .widget already!). It also saves space and time in getting this documentation published and available to you.

So, here's the way to get your element's widget's type:

    print(type(window[your_element_key].widget))

In the case of the button example above, what is printed is:

<class 'tkinter.Button'>

I don't think that could be any clearer. Your job at this point is to look at the tkinter documentation to see what the methods are for the tkinter Button widget.

Window Level Access

For this one you'll need some specific variables for the time being as there is no Window class variable that holds the window's representation in the GUI library being used.

For tkinter, at the moment, the window's root object is this:

sg.Window.TKroot

The type will vary in PySimpleGUI. It will either be: tkinter.Tk() tkinter.Toplevel()

Either way you'll access it using the same Window variable sg.Window.TKroot

Watch this space in the future for the more standardized variable name for this object. It may be something like Window.widget as the Elements use or something like Window.GUIWindow.