python - 按下按钮时如何开始闪烁我的文本标签?
问题描述
我试图在 tkinter 中创建一个文本标签,当按下“1”按钮时它应该开始闪烁。为此,我在 tkinter 文档和谷歌上的其他教程的帮助下进行了尝试,但由于我是 python 新手,最终未能成功创建逻辑,我发现处理事件对象并不难。这是我的代码。
import tkinter as Tk
flash_delay = 500 # msec between colour change
flash_colours = ('white', 'red') # Two colours to swap between
def flashColour(object, colour_index):
object.config(background = flash_colours[colour_index])
root.after(flash_delay, flashColour, object, 1 - colour_index)
root = Tk.Tk()
root.geometry("100x100")
root.label = Tk.Text(root, text="i can flash",
background = flash_colours[0])
root.label.pack()
#root.txt.insert(Tk.END,"hello")
root.button1=Tk.Button(root,text="1")
root.button1.pack()
root.button.place(x=10,y=40,command = lambda: flashColour(root.label, 0))
root.mainloop()
解决方案
place
不接受command
作为论据。您应该将它传递给Button
带有lambda
.
root.button=Tk.Button(root,text="1",command = lambda: flashColour(root.label, 0))
root.button.pack()
#root.button.place(x=10,y=40) # you should use either `pack` or `place` but not both
推荐阅读
- ios - 如何创建 api 响应对象类型
- python - Windows 任务调度程序未运行调用 python selenium 脚本的批处理文件
- parsing - 如何使用 ANTLR4 左递归管理逻辑表达式的括号?
- shopify - shopify iframe内的路由
- analytics - 是否可以重命名智能点击事件?
- excel - 努力加0完成串号到3
- php - Codeigniter 4 Pagination 正在工作,但是,我的 bootstrap 4 分页模板不是
- arrays - 如何在 React 状态数组中不包含重复值,如何防止数组中的值重复?
- javascript - 定义属性时无法读取未定义的属性“setState”
- postgresql - 并行运行两个过程 postgresql