【问题标题】:PySimpleGui - Get Button TextPySimpleGui - 获取按钮文本
【发布时间】:2021-03-18 22:22:39
【问题描述】:

我正在使用 PySimpleGui 开发一个应用程序,它将从列表中分配按钮文本。用户搜索时,按钮上的文本会发生变化。我希望能够在按下按钮时阅读、打印和分配按钮文本。我怎么做?这是代码。我尝试了几件事无济于事。代码末尾的两条 print 语句是余数。

import PySimpleGUI as sg

selection_button_text_data = ['Like a Rolling Stone','Bob Dylan','The Sound of Silence','Simon & Garfunkel','Respect','Aretha Franklin','A Day In The Life','The Beatles']

layout =[
    [
        [sg.Button(selection_button_text_data[0])],
        [sg.Button(selection_button_text_data[1])],
        [sg.Button(selection_button_text_data[2])],
        [sg.Button(selection_button_text_data[3])]
    ]
        ]
window = sg.Window('Window Title', layout)
event, values = window.read()
window.close()
print(values)
print(sg.Button.GetText())

【问题讨论】:

  • 您想即时更新按钮?
  • 奥尔德文是的,我愿意。

标签: python user-interface pysimplegui


【解决方案1】:

在您的情况下,最好为每个按钮提供每个键,然后您将获得与单击按钮的键相同的事件。通过window[key]可以获取元素并应用get_text()方法获取按钮文本。

import PySimpleGUI as sg

selection_button_text_data = [
    'Like a Rolling Stone',
    'Bob Dylan',
    'The Sound of Silence',
    'Simon & Garfunkel',
    'Respect',
    'Aretha Franklin',
    'A Day In The Life','The Beatles',
]

layout =[
    [sg.Button(selection_button_text_data[i], key=f'BTN{i}')] for i in range(4)
]
window = sg.Window('Window Title', layout)
event, values = window.read()
if event != sg.WINDOW_CLOSED:
    print(window[event].get_text())
window.close()
The Sound of Silence

【讨论】:

  • 谢谢 Jason - 这让我开始了。就像创建多个按钮的单行代码一样。赞赏...现在让我们看看我是否可以使用它! :) ....布拉德....
猜你喜欢
  • 2020-06-13
  • 2022-12-04
  • 2022-10-16
  • 2023-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-03
  • 1970-01-01
相关资源
最近更新 更多