Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Make flashing text box in python tkinter

One of the useful features of my recently-announced tkstackrpms python library is a function that flashes an entry widget (text box).

files/2024/listings/demo-flashing-entry.py (Source)

#!/usr/bin/env python3
# Startdate: 2024-04-08-2 13:44
# Purpose: demo of the cool features of tkstackrpms
# Reference:
#    [python - How to make a flashing text box in tkinter? - Stack Overflow](https://stackoverflow.com/questions/27533244/how-to-make-a-flashing-text-box-in-tkinter/27533595#27533595)
# demo: flashing entry widget
import tkinter as tk
import tkstackrpms as stk
class App(tk.Frame):
   def __init__(self, master):
      super().__init__(master)
      self.master.title("Demo of tkstackrpms features")
      imgicon = stk.get_scaled_icon("battery",24,"default", "","apps")
      self.master.tk.call("wm","iconphoto",self.master._w,imgicon)
      self.grid()
      # Variables
      self.name1 = tk.StringVar()
      self.name2 = tk.StringVar()
      self.name3 = tk.StringVar()
      # Widgets
      tk.Label(self.master,text="If an A is in a text box when you tab out or press Enter,\nit will flash to indicate there is a problem.").grid(row=0,column=0,columnspan=3)
      self.ent_name1 = stk.Entry(self.master,textvariable=self.name1,func=self.validate_inputs)
      self.ent_name1.grid(row=1,column=0)
      self.ent_name2 = stk.Entry(self.master,textvariable=self.name2,func=self.validate_inputs)
      self.ent_name2.grid(row=1,column=1)
      self.ent_name3 = stk.Entry(self.master,textvariable=self.name3,func=self.validate_inputs)
      self.ent_name3.grid(row=1,column=2)
   # Functions
   def validate_inputs(self, arg1 = None, arg2 = None, arg3 = None):
      if "a" in self.name1.get():
         stk.flash_entry(self.ent_name1,["red","white","blue","white","red","white","red","white"],333)
      if "a" in self.name2.get():
         stk.flash_entry(self.ent_name2,["red","white","blue","white","red","white","red","white"],333)
      if "a" in self.name3.get():
         stk.flash_entry(self.ent_name3,["red","white","blue","white","red","white","red","white"],333)
# main
root = tk.Tk()
app_tk = App(root)
app_tk.mainloop()

I haven't bothered to learn how to make an animated GIF showcasing this, sorry. I bet this logic could be hooked up to the function called by the invalidcommand parameter of the entry widget, but I've used this where I had complex logic that evaluated a whole form so I didn't try the per-widget stuff.

Comments