Pysimplegui: [ Question] when swapping values in a Form(tk), InputText fields seem to get swapped only after second click, unklike Text fields

Created on 7 Sep 2020  路  2Comments  路  Source: PySimpleGUI/PySimpleGUI

Question

Linux Ubuntu 20.4

Python version 3.8

PySimpleGUI

Ports = tkinter,

PySimpleGUI Version: 4.29.0.5 Unreleased (directly from github)

tkinter version: 8.6.10

Your Experience Levels In Months or Years

10 Python programming experience
10 Programming experience overall
Have used another Python GUI Framework (tkinter, Qt, etc) previously (yes/no is fine)? YES

You have completed these steps:

  • [ x] Read instructions on how to file an Issue
  • [ x] Searched through main docs http://www.PySimpleGUI.org for your problem
  • [ x] Searched through the readme for your specific port if not PySimpleGUI (Qt, WX, Remi)
  • [ x] Looked for Demo Programs that are similar to your goal http://www.PySimpleGUI.com
  • [x ] Note that there are also Demo Programs under each port on GitHub
  • [ x] Run your program outside of your debugger (from a command line)
  • [ x] Searched through Issues (open and closed) to see if already reported
  • [ x] Try again by upgrading your PySimpleGUI.py file to use the current one on GitHub. Your problem may have already been fixed but is not yet on PyPI.

Description of Problem / Question / Details

when swapping TextFields and InputTextFields in my Form (by button press), it seems that the InputTextField does not properly swap values after the first click and is "lagging behind". can be demonstrated below by clicking "Calculate" and "Swap" buttons.
My question is if i missing some update command to update the InputText fields correctly after changing their value by code.

Code To Duplicate

import PySimpleGUI as sg

layout = [[sg.Text("clicking swap schould change line A with line B. However, input fields do not match result field...")],
          [sg.Text("Input A:"), sg.InputText("AAA", key="stringA", size=(15,1), enable_events=True   ),
            sg.Checkbox("a1", default=False, key="a1", enable_events=True),
            sg.Checkbox("a2", default=True,  key="a2", enable_events=True),
            sg.Checkbox("a3", default=False, key="a3", enable_events=True),
            sg.Text("result A:"), sg.Text("not yet calculated", size=(35,1), key="resultA", relief="raised")
            ],
          [sg.Text("Input B:"), sg.InputText("BBB", key="stringB", size=(15, 1), enable_events=True),
            sg.Checkbox("b1", default=False, key="b1", enable_events=True),
            sg.Checkbox("b2", default=False, key="b2", enable_events=True),
            sg.Checkbox("b3", default=True,  key="b3", enable_events=True),
            sg.Text("result B:"), sg.Text("not yet calculated", size=(35, 1), key="resultB", relief="raised")
             ],
           [sg.Button("Calculate"), sg.Button("Swap"), sg.Cancel()],
          ]

window = sg.Window('swapping and calculating', layout)

while True:
    event, values = window.read()
    print(event)
    if event in (sg.WIN_CLOSED, 'Cancel'):
        break
    if event in ( "Calculate", "stringA", "stringB", "a1", "a2", "a3", "b1", "b2", "b3"):
        window["resultA"].update(values["stringA"] + str(values["a1"]) + str(values["a2"]) + str(values["a3"]))
        window["resultB"].update(values["stringB"] + str(values["b1"]) + str(values["b2"]) + str(values["b3"]))
    if event == "Swap":
        # --- update result ---
        #window["resultA"].update(values["stringA"] + str(values["a1"]) + str(values["a2"]) + str(values["a3"]))
        #window["resultB"].update(values["stringB"] + str(values["b1"]) + str(values["b2"]) + str(values["b3"]))
        # --- swap ---
        a = [values["stringA"], values["a1"], values["a2"], values["a3"]]
        b = [values["stringB"], values["b1"], values["b2"], values["b3"]]
        window["stringA"].update(b[0])
        window["stringB"].update(a[0])
        window["a1"].update(b[1])
        window["b1"].update(a[1])
        window["a2"].update(b[2])
        window["b2"].update(a[2])
        window["a3"].update(b[3])
        window["b3"].update(a[3])
        # ----- somehow the update commands above are not processed ---
        #---- updating the result ------
        window["resultA"].update(values["stringA"] + str(values["a1"]) + str(values["a2"]) + str(values["a3"]))
        window["resultB"].update(values["stringB"] + str(values["b1"]) + str(values["b2"]) + str(values["b3"]))
        print("a:",values["stringA"])
        print("b:", values["stringB"])

window.close()

Most helpful comment

For "Swap", following code not correct

        window["resultA"].update(values["stringA"] + str(values["a1"]) + str(values["a2"]) + str(values["a3"]))
        window["resultB"].update(values["stringB"] + str(values["b1"]) + str(values["b2"]) + str(values["b3"]))

Should be

        window["resultA"].update(values["stringB"] + str(values["b1"]) + str(values["b2"]) + str(values["b3"]))
        window["resultB"].update(values["stringA"] + str(values["a1"]) + str(values["a2"]) + str(values["a3"]))

All 2 comments

For "Swap", following code not correct

        window["resultA"].update(values["stringA"] + str(values["a1"]) + str(values["a2"]) + str(values["a3"]))
        window["resultB"].update(values["stringB"] + str(values["b1"]) + str(values["b2"]) + str(values["b3"]))

Should be

        window["resultA"].update(values["stringB"] + str(values["b1"]) + str(values["b2"]) + str(values["b3"]))
        window["resultB"].update(values["stringA"] + str(values["a1"]) + str(values["a2"]) + str(values["a3"]))

ahhhhhhhhhh, epic facepalm
thanks! !!!!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thelou1s picture thelou1s  路  4Comments

DKatarakis picture DKatarakis  路  6Comments

mozesa picture mozesa  路  5Comments

flowerbug picture flowerbug  路  4Comments

ihouses picture ihouses  路  6Comments