I have created a script which constantly screenshots and finds elements on the screen, and I want to use tkinter or a similar framework to overlay text on the screen. The main script runs in a while loop and checks every x seconds, and I want the tkinter window to also update ever x seconds until the script is closed.
import tkinter as tk
from pynput import keyboard
def superimpose_text(text):
color = "black" if isinstance(text, str) else 'green' if text < 10 else 'yellow' if text < 15 else 'red'
label = tk.Label(root, text=text, font=('Times New Roman', '50'), fg=color, bg='white')
label.lift()
root.geometry("+1400+600")
root.wm_attributes("-topmost", True)
root.wm_attributes("-disabled", True)
root.wm_attributes("-transparentcolor", "white")
label.pack()
listener = keyboard.Listener(on_press=lambda event: root.destroy())
listener.start()
while True:
root = tk.Tk()
text = screenshot_info()
superimpose_text(text)
root.mainloop()
Currently I'm using a listener to destroy the label at any keypress, as that's the only way I can get It to ever update the text, but this isn't working. How do i make it continuously update every time the while loop runs?