python - pysimplegui 无按钮文本更新

标签 python python-3.x user-interface pysimplegui

我正在尝试创建一个大时钟作为 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/

相关文章:

Python C-API : Populate a Py_buffer from a C function

user-interface - Qt mac : How does the layout differs from other OSes

c++ - 用于 C++ 应用程序的 Web 界面

python - 有关 Python TKinter 动态选项菜单的更多信息

python - Feedparser.parse() 'SSL: CERTIFICATE_VERIFY_FAILED'

python - 如何在 cloudant-python 中选择特定字段进行查询?

python-3.x - 如何将python xgboost模型转换为pmml?

python - 为什么我的 numpy 矩阵在分配之前得到更新?

Python Click 在执行前确定链式命令的顺序

python - 如何解决TypeError : 'float' object is not iterable