![](/img/trans.png)
[英]I want to firstly create a database and then update it as per the values in mongodb
[英]Firstly I want to place label1 and after 3 seconds i want to configure background but it doesnt work
import tkinter as tk
import time
root=tk.Tk()
label1=tk.Label(text='hello',font='Calibri 25')
label1.pack()
time.sleep(3)
label1.configure(bg='green')
root.mainloop()
我一次又一次地说我只想放置 label1,3 秒后我想更改背景颜色,但它不起作用。 你能解释一下主要问题吗?我该怎么办?
不应在主 tkinter 循环中使用sleep
,因为这会导致界面冻结。 最好在after
使用。
import tkinter as tk
COLOR = 'green'
def lbl_clear():
label1.configure(text="", bg=root['bg'])
def lbl_bg(color):
label1.configure(bg=color)
root.after(3000, lbl_clear)
root = tk.Tk()
label1 = tk.Label(text='hello', font='Calibri 25')
label1.pack()
root.after(3000, lbl_bg, COLOR)
root.mainloop()
所以,这里没有什么要澄清的。
在调用root.mainLoop()
之前,元素并未放置在屏幕上,事实上 - 此时没有屏幕。 例如,如果你执行sleep(1000)
,1000 秒内什么都不会发生,然后屏幕将显示绿色标签。
您的第一行创建程序化对象(包含有关如何在屏幕上绘制的数据,但它们什么也不做)。
只有在调用rootMainLoop
之后, tk
才会创建屏幕。
事件是一种在发生某些事情时执行代码的方式(单击按钮、键入文本或您的示例 -创建屏幕)
一种实现你想要的东西的方法:
创建屏幕
编写一个更改标签的函数
将函数绑定到屏幕上显示标签时触发的事件(即将函数设置为仅在显示标签时运行)
def change_color(): global label sleep(3) label.config(bg="green") root = tk.Tk() label = tk.Label(...) label.bind('<Map>',change_color) root.mainloop()
注意:这个例子是伪代码。 没有按原样运行
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.