0

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)
Su Rots
  • 3
  • 2
  • It is possible this is caused by multiple instances of `Tk()`. If you are going to have multiple windows you should implement `Toplevel()` instead. – Rory May 09 '22 at 19:15
  • I'm using the tk root widget to show the information.but when i call tk.destroy it doesnt destroy the small tk window. Do you assume I should use it on child widget instead? – Su Rots May 10 '22 at 09:10
  • Once you have created the parent window with an instance of Tk() and `mainloop()` every other window you create after should be created with `Toplevel()` and not another Tk(). Multiple instances of Tk() is known to cause problems. Then if you want to close your child windows you can call `destroy()` on that specific window or you can `withdraw()` the window which iconifies it and hides it from the user and can be brought back with `deiconify()`. – Rory May 10 '22 at 13:20
  • I am not able to recreate the issue you are having so I may not be addressing your question accurately. However when I run your script the `root.destroy()` does close the program after 5 seconds. What small tk window are you talking about? Is it a window you did not intend to be created? It looks like you are also missing your parenthesis after your `join()` call on your thread. – Rory May 10 '22 at 13:29
  • I was able to recreate the issue using toplevel instead and then I found out that root.destroy doesn't destroy all widgets properly, just as mentioned in this post https://stackoverflow.com/a/64078753/11885829 Using root.quit fixed the probem – Su Rots May 21 '22 at 08:54

0 Answers0