I'm using a tkinter window as a momentary popup when certain conditions are met. On my device the tk window opens as intented and no extra blank windows are opened However, I'm currently facing a problem when i'm running the code on any other device it would open extra blank Tk windows alongside the main one. moreover, the amount of True conditions in my function reflects the amount of blank tkinter windows are spawned.
I'm currently not sure what is causing this issue as it's only occuring on 2 other devices i tried running this script on. one other thing to note that it doesnt always happen on these devices. only occasionally.
import tkinter as tk
from playsound import playsound
import threading
def popup(con1 , con2, con3, con4):
root = tk.Tk()
# labels
con1_text = tk.Label(root,text ="Bread alarm", font=("Arial Bold", 50) )
con2_text = tk.Label(root,text ="bacon alarm", font=("Arial Bold", 50) )
con3_text =tk.Label(root,text ="tomato alarm", font=("Arial Bold", 50) )
con4_text =tk.Label(root,text ="banana alarm", font=("Arial Bold", 50) )
# using setting the position of the labels with place method
if (con1):
con1_text.place(relx = 0.5,
rely = 0.4,
anchor = 'center')
if (con2):
con2_text.place(relx = 0.5,
rely = 0.5,
anchor = 'center')
if (con3):
con3_text.place(relx = 0.5,
rely = 0.6,
anchor = 'center')
if (con4):
con4_text.place(relx = 0.5,
rely = 0.7,
anchor = 'center')
if (not con1 and not con2 and not con3 and not con4):
return
# flash screen from orange to red
def change_color():
current_color = root.cget("background")
next_color = "orange" if current_color == "red" else "red"
root.config(background=next_color)
root.after(1000, change_color)
root.geometry('1000x1000')
root.configure(bg='red')
# this thread plays a sound effect during the time the tkinter window is open.
def PlaySound():
playsound("sound.mp3")
sound_th = threading.Thread(target=PlaySound)
sound_th.start()
sound_th.join
# run widget
change_color()
root.lift()
root.attributes("-topmost", True)
root.after(5000, lambda: root.destroy() ) # closes after 5 seconds
root.mainloop()
popup(False, True, True,True)