我正在嘗試創建一個大時鐘作為 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()。我應該改用什么?
uj5u.com熱心網友回復:
事件'Run'從何而來?
嘗試timeout在方法中使用選項window.read來生成事件sg.TIMEOUT_EVENT。
超時
如果沒有其他 GUI 事件首先發生,等待讀取將回傳的毫秒數
您還可以使用多執行緒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()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/468909.html
標籤:Python python-3.x 用户界面 pysimplegui
