[英]How to update text in pysimplegui
I'm very new to python so I'm just experimenting with new GUIs and other things.我是 python 的新手,所以我只是在尝试新的 GUI 和其他东西。 I was wondering if you could open up a window using pysimplegui and have a piece of text which says "0", and when you click a button the text changes to the previous number +1.我想知道您是否可以使用 pysimplegui 打开一个 window 并有一段文字显示“0”,当您单击一个按钮时,该文字会变为之前的数字 +1。 For EG: at first the text says "0", but when you click a button of some sort, the text changes to a "1".对于 EG:起初文本显示为“0”,但是当您单击某种按钮时,文本变为“1”。 Sorry if my explanation of my problem is bad but I have not used stack overflow before.对不起,如果我对我的问题的解释不好,但我以前没有使用过堆栈溢出。
import PySimpleGUI as sg
num = 0
layout = [
[sg.Text(num)],
[sg.Button("hi")]
]
window = sg.Window("the box", layout)
while True:
event, values = window.read()
if event == "hi":
num = num+1
layout = [
[sg.Text(num)],
[sg.Button("hi")]
]
window = sg.Window("the box", layout)
I tried this code but it did not work.我试过这段代码,但没有用。 As you can probably see I am very new to all of this so kindly do not type an answer with complicated/advanced code.正如您可能看到的那样,我对所有这些都很陌生,所以请不要输入复杂/高级代码的答案。
Thanks to tim who gave me an answer but i do not understand it, here is the new(still faulty) code:感谢蒂姆给了我一个答案,但我不明白,这是新的(仍然有问题的)代码:
import PySimpleGUI as sg
num = 0
layout = [
[sg.Text(key='xxx')],
[sg.Button("hi")]
]
window = sg.Window("the box", layout)
while True:
event, values['xxx'] = window.read()
if event == "hi":
xxx = num
num = num + 1
Should call method window[element_key].update(value=new_value)
to update the element window[element_key]
with new value new_value
.应调用方法window[element_key].update(value=new_value)
以使用新值new_value
更新元素window[element_key]
。
New layout and new window are not required.不需要新布局和新 window。
import PySimpleGUI as sg
num = 0
layout = [
[sg.Text(num, key='xxx')],
[sg.Button("hi")],
]
window = sg.Window("the box", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED:
break
elif event == "hi":
num += 1
window['xxx'].update(num)
window.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.