-3

The function is used to set a user and return it to the program for further use.

def setuser(int1=int(values)):
    i = 0
    if len(aNutzerID) == 0:
        # here if I have no users in the program, it returns "no User"
        laySetUser = [
            [sg.Text(text="Benutzer wählen", font=("Arial", 24, "underline", "bold"))],
            [sg.Text(text="Keine Benutzer!")]
        ]
    else:
        """
        here the user of the program can chose, which user is active right now.
        Thats, what the "sg.Input" is for. But I can't return it properly to the
        rest of the program.
        """
        laySetUser = [
            [sg.Text(text="Benutzer wählen", font=("Arial", 24, "underline", "bold"))],
            [sg.Input(default_text="ID eingeben!", key="-USER-"),
             sg.Button("Benutzer speichern", size=(20, 1), key="-SAVESETUSER-")],
        ]
        while i < len(aNutzerID):
            laySetUser.extend([
                [sg.Text(text=aNutzerID[i], size=(7,1), justification="right"),
                sg.Text(text=aNutzerName[i], size=(20,1), justification="left"),
                sg.Text(text=aNutzerFunktion[i], size=(40,1), justification="left")]
            ])
            i = i + 1

    wsetuser = sg.Window("Benutzer wählen", laySetUser, size=(640, 360), element_padding=(3, 3), margins=(10, 10),
                   element_justification="center")

    while True:
        event, values = wsetuser.read()
        if event == sg.WIN_CLOSED:
            break
        if event == "-SAVESETUSER-":
            global user
            # here i would like to overwrite my user int with the input from above
            break
    tasks()
    wsetuser.close()

Now i wnat to know, how i can give to inputvalue from "-USER-" to my int user?

Jason Yang
  • 11,284
  • 2
  • 9
  • 23
  • Found the answer! Just had to use this bit of code. user = int(wsetuser['-USER-'].get().strip()) Found on https://stackoverflow.com/questions/68159015/how-to-set-a-input-text-as-required-pysimplegui – Riitzli May 28 '22 at 23:39
  • first edit question and format code. You can select it and use `Ctrl+K` for this. – furas May 29 '22 at 02:23

1 Answers1

0

Get the value of element with key by values[key] after window.read(), and return it to end the function. Call function by id_ = setuser().

    while True:

        event, values = wsetuser.read()

        if event == sg.WIN_CLOSED:
            result = None       # None for No user
            break
        if event == "-SAVESETUSER-":
            id_ = values["-USER-"].strip()
            if id_ in aNutzerID:
                result = id_
                break

    tasks()
    wsetuser.close()
    return result
Jason Yang
  • 11,284
  • 2
  • 9
  • 23