【问题标题】:PySimpleGui: How to enter text in the text box?PySimpleGui:如何在文本框中输入文本?
【发布时间】:2020-03-23 05:02:18
【问题描述】:

我正在通过参考教程学习 PySimpleGui

Link-1Link-2

我需要在布局中添加按钮以输入值,然后在相邻的文本框中显示该值

到目前为止,我已经能够创建按钮和文本框了。

以下是我的代码:-

import PySimpleGUI as sg

layout = [[sg.Text('Enter Value:')],
          [sg.Input(do_not_clear=False), sg.Text('Value selected is:'), sg.Text(sg.InputText(""), key='_USERNAME_')],
          [sg.Button('Enter'), sg.Exit()],
          [sg.Text('List Of Values:')],
          [sg.Listbox(values=('value1', 'value2', 'value3'), size=(30, 2), key='_LISTBOX_')]]

window = sg.Window('My Application', layout)

while True:
    event, values = window.Read()
    print(event, values)
    if event is None or event == 'Exit':
        break
    if event == 'Enter':
        window.Element('_LISTBOX_').Update(values=[event, values, 'new value 3'])
        #window.Element('_USERNAME_').Update(values=[values]) #need to update the text box with value entered
window.Close()

但是,我无法在文本框中显示输入的值。 我在代码中添加了一条注释(现在给出了一个错误),我需要用输入的值更新文本框。

请帮忙!

编辑:我能够在弹出窗口中显示值,但我需要在文本框中显示

【问题讨论】:

    标签: python-3.x pysimplegui


    【解决方案1】:

    您可以通过使用窗口对象上的键引用元素来直接更新元素:

    例如根据您的更新

    window['_LISTBOX_'].Update(values=[event, values, 'new value 3'])
    window['_USERNAME_'].Update(values[0])
    

    【讨论】:

      【解决方案2】:

      我想通了,

      以下代码符合我的目的:-

      import PySimpleGUI as sg
      
      layout = [[sg.Text('Enter Value:')],
                [sg.Input(do_not_clear=False), sg.T('Not Selected ', size=(52,1), justification='left',text_color='red', background_color='white', key='_USERNAME_')],
                [sg.Button('Enter'), sg.Exit()],
                [sg.Text('List Of Values:')],
                [sg.Listbox(values=('value1', 'value2', 'value3'), size=(30, 2), key='_LISTBOX_')]]
      
      window = sg.Window('My Application', layout)
      
      while True:
          event, values = window.Read()
          print(event, values)
          if event is None or event == 'Exit':
              break
          if event == 'Enter':
              window.Element('_LISTBOX_').Update(values=[event, values, 'new value 3'])
              window.FindElement('_USERNAME_').Update(values[0])
      window.Close()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-06
        • 1970-01-01
        • 2013-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-07
        • 2021-10-11
        相关资源
        最近更新 更多