Pavol Bujna
Pavol Bujna

Reputation: 179

Tkinter overlaying text

I'm learning Tkinter and I'm a bit confused. The code below is showing the white text from the face_recogniton() function.

import tkinter as tk
from PIL import Image, ImageTk
from facerec_on_raspberry_pi import face_function #face_function() return recognized person name

root = tk.Tk()

#Fullscreen
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes('-fullscreen',True)
root.configure(background='black')

def my_mainloop():
    print (face_function())
    instructions = tk.Label(root, text=str(face_function()), font=('Raleway', 55), fg='white', bg='black')
    instructions.place(x=160, y=60)
    root.after(1, my_mainloop)

root.after(1, my_mainloop)

root.mainloop()

But the text is overlaying. How can I clean it before showing new text?

Here are the images of what's happening:

Here is Unknown Person and Barack Obama overlaying - Barack Obama

Here is None and Unknown Person overlaying. None

Upvotes: 0

Views: 150

Answers (1)

STerliakov
STerliakov

Reputation: 7963

You don't need to re-create your label every time you need to change text. The text of existing label can be changed after creation. Also note the two lines I commented: you don't need both.

import tkinter as tk
from PIL import Image, ImageTk
from facerec_on_raspberry_pi import face_function #face_function() return recognized person name

root = tk.Tk()

#Fullscreen
root.overrideredirect(True) # Use only one of this two lines: 
root.overrideredirect(False) # you just set a flag and then change it
root.attributes('-fullscreen',True)
root.configure(background='black')

instructions = tk.Label(root, text='', font=('Raleway', 55), fg='white', bg='black')
instructions.place(x=160, y=60)

def my_mainloop():
    print (face_function())
    instructions['text'] = str(face_function())    
    root.after(1, my_mainloop)

root.after(1, my_mainloop)

root.mainloop()

Upvotes: 2

Related Questions