使用Python/Tkin顺序计时事件

2024-04-27 02:34:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要用Python为Stroop测试开发一个简单的接口,但是我不知道如何用Python启动它。你知道吗

界面如下:

10秒在一个空白屏幕上,10秒在屏幕中央有一个彩色的字。这将重复10次,然后完成应用程序。你知道吗

问题是:我不知道是必须使用Tkinter+anothelib(timed events),还是Tkinter提供了使用timed events的方法。你知道吗

有人能照亮我吗?你知道吗

非常感谢!你知道吗


Tags: 方法应用程序界面屏幕tkinterevents空白彩色
1条回答
网友
1楼 · 发布于 2024-04-27 02:34:58

在tkinter中可以使用root.after、使用参数的重复次数和对该参数值的测试来处理定时事件。你知道吗

下面是一个在简单窗口上交替显示文本标签的简短示例:

import tkinter as tk

def toggle_screen(idx=10):
    variable.set(text_values[idx%2])
    if idx > 0:
        root.after(1000, toggle_screen, idx-1)

root = tk.Tk()
root.geometry('300x300')
text_values = ['', 'hello']

variable = tk.StringVar()
variable.set('')
label = tk.Label(root, textvariable=variable, fg='red')
label.pack(expand=True, fill='both')
toggle_screen()

root.mainloop()

相关问题 更多 >