【问题标题】:Text not updating in PySimpleGuiPySimpleGui 中的文本未更新
【发布时间】:2020-12-13 17:38:14
【问题描述】:

在我的代码中,我试图制作一个计算器。所以有一个1 按钮,当按下该按钮时,通过在其文本中添加1 来更新Question: 文本。因此,当我按下1 时,文本将从Question: 转换为Question: 1。但它没有更新。我以前也遇到过这个问题。我认为当我执行.update 时,它只会更新该值,直到其与文本已有的字母数相同。如果它有 2 个字母并且我尝试使用.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()

【问题讨论】:

  • 我是 PySimpleGui 的初学者所以 ????
  • 这是由文本元素的长度引起的,如果没有定义选项size,它将是初始字符串的长度。所以你确实更新了,但没有显示太久而无法显示在短文本元素上。建议您使用sg.InputText 和选项readonly=True
  • @JasonYang 回答问题!!!

标签: python pysimplegui


【解决方案1】:

如上评论,这样的例子,

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-04
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多