PySimpleGui中的文本未更新

2024-10-01 05:00:11 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的代码中,我试图制作一个计算器。因此,有一个1按钮,当按下该按钮时,通过向其文本中添加1来更新Question: 文本。因此,当我按下1时,文本将从Question: 转换为Question: 1。但它并没有更新。我以前也遇到过这个问题。我想当我做.update时,它只会更新值,直到它的字母数与文本的字母数相同为止。如果它有两个字母,我尝试.update('123'),它将只更新为12。有没有办法绕过这个问题

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()

Tags: keytextin文本event字母exitupdate
1条回答
网友
1楼 · 发布于 2024-10-01 05:00:11

正如上面的评论,像这样的例子

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()

相关问题 更多 >