2

In my code, Im trying to make a calculator. So there is a 1 button which when pressed, updates the Question: text by adding 1 to it's text. So when I press 1, the text will convert from Question: to Question: 1. But its not updating. I have faced this problem before too. I think when I do the .update, it will only update the value till its the same number of letters as the text already has. If it has 2 letters and I try to .update('123'), it will only update to 12. Is there any way to get around this???

import PySimpleGUI as sg

layout = [
    [sg.Text('Question: ', key='-IN-')],
    [sg.Text('Answer will be shown here', key='-OUT-')],
    [sg.Button('1'), sg.Button('2'), sg.Button('3')],
    [sg.Button('4'), sg.Button('5'), sg.Button('6')],
    [sg.Button('7'), sg.Button('8'), sg.Button('9')],
    [sg.Button('Enter'), sg.Button('Exit')]
]

window = sg.Window('calculator', layout)

while True:
    event, values = window.read()
    if event is None or event == 'Exit':
        break
    elif event == '1':
        bleh = window['-IN-'].get()
        teh = f'{bleh}1'
        window['-IN-'].update(value=teh)

window.close()
SYCK playz
  • 102
  • 2
  • 8

1 Answers1

3

As above comment, Example like this,

import PySimpleGUI as sg

layout = [
    [sg.InputText('Question: ', readonly=True, key='-IN-')],
    [sg.Text('Answer will be shown here', key='-OUT-')],
    [sg.Button('1'), sg.Button('2'), sg.Button('3')],
    [sg.Button('4'), sg.Button('5'), sg.Button('6')],
    [sg.Button('7'), sg.Button('8'), sg.Button('9')],
    [sg.Button('Enter'), sg.Button('Exit')]
]

window = sg.Window('calculator', layout)
input = window['-IN-']
while True:
    event, values = window.read()
    if event is None or event == 'Exit':
        break
    elif event in '1234567890':
        bleh = window['-IN-'].get()
        teh = f'{bleh}{event}'
        input.update(value=teh)
        input.Widget.xview("end")   # view end if text is too long to fit element

window.close()
Jason Yang
  • 11,284
  • 2
  • 9
  • 23