Reputation: 47
i want button2 to trigger the pressing of button1 automatically with 0.2 second intervals between each press however when i try and put the section which i have surrounded with stars below into any kind of loop or try repeating them in any way it seems to only do the top line, i know it is not just completing them all and changing the button to the same colour each time as i have changed the code to give the reult of printing out the names of the colours instead of changing the button to that colour and each time it prints out randomly generated names.
any help is 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()
Upvotes: 0
Views: 55
Reputation: 54193
Use after
. This schedules another callback to run after some time. root.after(x, f)
runs f()
after x
ms (where root
is the controlling Tk instance)
def button2_function():
Button.invoke(button1)
root.after(200, button2_function)
N.B. that this code could use some re-factoring to stop using globals, but I'm assuming it's just sample code! It'd look way better in a class, though.
Upvotes: 2