PySimpleGUI / PySimpleGUI

Python GUIs for Humans! PySimpleGUI is the top-rated Python application development environment. Launched in 2018 and actively developed, maintained, and supported in 2024. Transforms tkinter, Qt, WxPython, and Remi into a simple, intuitive, and fun experience for both hobbyists and expert users.

Home Page:https://www.PySimpleGUI.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Question] In which way `_, values = window.read()` differs from getting the values from the widgets directly?

Wes0617 opened this issue · comments

commented

Type of Issue

Question


Operating System

Windows 10

PySimpleGUI Port

tkinter


Versions

Python version

3.10

PySimpleGUI Version

4.59.0

GUI Version

8.6.12


Troubleshooting

Detailed Description

In which way the values I get from _, values = window.read() are different from getting the values from the widgets themselves at the time the event is triggered? For example, the moment I activate a radio button, will values["my_radio_button"] be always the same as window["my_radio_button"].get()? Thank you in advance.

Code To Duplicate

while True:
    event, values = window.read()
    if event == "my_radio_button":
        assert(values["my_radio_button"] == window["my_radio_button"].get())

Basically, both of them should be all the same, even not the radio event.

They'll match at the moment before the read returns. After that, all bets are off.

values is a dictionary that's returned to you. If in your loop you do something to the window, it's possible for you to directly read the value and have it be different than the values dictionary.

In the docs, I discourage using the get methods on a regular basis and urge users to use the dictionary so that you get one consistent "snapshot" of the window's values all at one time. Using get is more like the traditional GUI frameworks and is unnecessary in PySimpleGUI because they're all returned at once.

commented

Before the read() returns? Not right after? Are the values changed concurrently by tkinter? How exactly?

It means after read() called and before read() return, this function read() will collect all the values and return as a dictionary.
If nothing changed by your code during the period from it collect values until you call get(), they will be the same.

If you make some calling into PySimpleGUI after window.read(), and continue to interact with the GUI, then it's possible for tkinter to change the values internally. It depends on a lot of things going on in the windowing system, the OS, etc. Calling window.refresh() for example will enable tkinter to get cycles to run and thus update the values. Even though tkinter isn't multithreaded, it doesn't mean that the windowing system under tkinter isn't still running and allowing you to interact with the window.

None of this is likely to impact normal applications.