【发布时间】:2018-02-14 19:08:07
【问题描述】:
我希望 button2 自动触发 button1 的按下,每次按下之间的间隔为 0.2 秒,但是当我尝试将下面用星星包围的部分放入任何类型的循环中或尝试以任何方式重复它们似乎只做第一行,我知道这不仅仅是完成它们并且每次都将按钮更改为相同的颜色,因为我更改了代码以给出打印出颜色名称而不是更改按钮的结果颜色和每次打印出随机生成的名称。
感谢任何帮助
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()
【问题讨论】:
-
你想要
tk.after -
您可能想使用
button.after(200, root.tk.call, '::tk::ButtonInvoke', button)来模拟按钮按下的整个过程。
标签: python user-interface button tkinter