pythontkintertextflashing

Continuous blinking text in tkinter python


I am currently creating a GUI and would like to create a continous blinking text as long as a certain condition holds. Here is a summary on how it works; 1. plot a graph and click a button to display the status of the data. If any points lie outside the limits, print a warning text that blinks continuously until you click another button to clear the status. However, I have managed to make it blink just once not continuously. see the snippet of the code Code snippet. The snippet of the buttons and the text is GUI snippet

I am new to python and tkinter as a whole so my knowledge is rusty. However, I have tried the for and while loops but the while loop froze the GUI while the for loop yielded no results. I have failed to find an answer to help. Looking forward to your help. Thank you


Solution

  • You need to call .after() inside change_color() in order to blink the text periodically.

    Below is the simplify code:

    def status(self):
    
        msg = "Warning!!!\nOut of Control, study the process and eliminate assignable cause"
        write = tk.Label(self, text=msg, font=("Montserrat", 15, "bold"), fg="red")
        write.place(x=200, y=530)
    
        def change_color():
            write.config(fg="red" if write["fg"] == "black" else "black")
            write.after(1000, change_color)
    
        write.after(1000, change_color)