[英]Update Element Using a Function - PySimpleGUI
In my application, I'm trying to update a text element by initiating a function call when a specific button is pressed.在我的应用程序中,我试图通过在按下特定按钮时启动 function 调用来更新文本元素。 Right now, pressing the button generates an error in the console: NameError: name 'window' is not defined.现在,按下按钮会在控制台中生成错误:NameError: name 'window' is not defined。
The code I'm working with is:我正在使用的代码是:
import PySimpleGUI as sg
sg.theme('DarkAmber')
introText = """ Let's begin!"""
landing = """ Okay"""
col1 = [[sg.Button("""Let's begin!"""), sg.Button('Win3')]]
col2 = [[sg.Button('Exit')]]
def makeWin1():
layout = [[sg.Menu(menu_def)],
[sg.Text(introText, font= 'Helvetica 10', justification='center', key='text1')],
[sg.Column(col1, vertical_alignment='center', justification='center', k='C1')],
[sg.Column(col2, vertical_alignment='center', justification='center', k='C2')]]
return sg.Window('Escape From Tenopia 2', layout, finalize=True,)
def main():
window1 = makeWin1()
while True:
window, event, values = sg.read_all_windows()
if window == sg.WIN_CLOSED:
break
if event == sg.WIN_CLOSED or event == 'Exit':
window.close()
if window == window1:
window1 = None
if event == 'About...':
sg.popup(introText)
if event == """Let's begin!""":
change1()
def change1():
window["text1"].update(landing)
if __name__ == '__main__':
main()
How can I make it such that pressing the "Let's Begin?"我怎样才能让它按下“让我们开始”? button updates the main text element to show the text in 'landing'?按钮更新主要文本元素以显示“着陆”中的文本?
You need to pass a parameter to change1():您需要将参数传递给change1():
if event == "Let's begin!":
change1(window1)
def change1(window):
window["text1"].update(landing)
Or don't use any extra function for that and adjust your main() like this:或者不要为此使用任何额外的 function 并像这样调整你的 main() :
if event == "Let's begin!":
window1["text1"].update(landing)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.