我希望button2自动触发按钮1,每次按下间隔0.2秒,但是当我尝试将我已经被星星包围的部分放入任何类型的循环或尝试以任何方式重复它们似乎只做顶线,我知道它不仅仅是完成它们并且每次都将按钮更改为相同的颜色,因为我已经更改了代码以给出打印出颜色名称的结果,而不是将按钮更改为颜色,每次打印出随机生成的名称。
任何帮助都是apreciated
from tkinter import *
import random
import time
colour_list = ["red", "yellow", "pink",
"green", "orange", "purple", "blue"]
def button1_function():
button1.configure(bg = random.choice(colour_list))
*****def button2_function():
Button.invoke(button1)*****
def button3_function():
quit()
root = Tk()
button1 = Button(root, text = "Rainbow Button",
bg = "white", height = 5, width = 15, command = button1_function)
button1.place(relx = 0.5, rely = 0.5, anchor = CENTER)
button2 = Button(root, text = "Auto", bg = "grey", height = 2,
width = 10, command = button2_function)
button2.place(relx = 1.0, rely = 1.0, anchor = SE)
button3 = Button(root, text = "X", bg = "grey", command = button3_function)
button3.place(relx = 0.0, rely = 0.0, anchor = NW)
root.mainloop()
答案 0 :(得分:2)
使用after
。这会安排另一个回调在一段时间后运行。 root.after(x, f)
在f()
ms后运行x
(其中root
是控制Tk实例)
def button2_function():
Button.invoke(button1)
root.after(200, button2_function)
N.B。这段代码可以使用一些重新分解来停止使用全局变量,但我假设它只是示例代码!不过,它在课堂上看起来更好。