Adee
Adee

Reputation: 71

How to start blinking of my text label when a button pressed?

I tried to create a text label in tkinter which should start blinking when "1" button is pressed . For that i tried with help of tkinter documentation and other tutorial on google,but eventually failed to create logic successfully as i am new to python,i found little hard to handle event object. Here is my code.

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()

Upvotes: 0

Views: 228

Answers (1)

Henry Yik
Henry Yik

Reputation: 22493

place does not accept command as argument. You should pass it to Button widget with 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

Upvotes: 1

Related Questions