我正在尝试创建一个大时钟作为 GUI 测试项目。 该窗口应该在没有任何输入的情况下运行,并且仅显示时间,同时每秒更新 10 次。 无论到目前为止我尝试了什么,我都无法让文本更新为我当前的时间。
这是我的代码:
import PySimpleGUI as gui
import time
gui.theme('Reddit')
clockFont = ('Arial', 72)
layout = [
[gui.Text('Loading...',size=(8,2), justification='center', key='CLK', font=clockFont)]
]
window = gui.Window('Big Clock', layout, size=(500, 150), finalize=True)
window.Resizable = True
while True:
event, values = window.read()
print(event)
if event in (None, gui.WIN_CLOSED):
break
if event == 'Run':
currentTime = time.strftime("%H:%M:%S")
window['CLK'].update(currentTime)
window.refresh()
time.sleep(0.1)
window.close()
此外,StackOverflow 上有人在一篇文章中表示,不应在循环环境中使用 time.sleep()。我应该用什么来代替?
最佳答案
事件'Run'
来自哪里?
尝试在方法window.read
中使用选项timeout
来生成事件sg.TIMEOUT_EVENT
。
timeout
Milliseconds to wait until the Read will return IF no other GUI events happen first
您还可以使用多线程进行 time.sleep
并调用 window.write_event_value
生成事件来更新 GUI。
import PySimpleGUI as gui
import time
gui.theme('Reddit')
clockFont = ('Arial', 72)
layout = [
[gui.Text('Loading...',size=8, justification='center', key='CLK', font=clockFont)]
]
window = gui.Window('Big Clock', layout, finalize=True)
while True:
event, values = window.read(timeout=100)
if event in (None, gui.WIN_CLOSED):
break
if event == gui.TIMEOUT_EVENT:
currentTime = time.strftime("%H:%M:%S")
window['CLK'].update(currentTime)
window.close()
关于python - pysimplegui 无按钮文本更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72036136/