0

On a drop-down/ option menu everytime an option is selected a pop-up should be displayed on the screen. This pop-up is another window.

#function to create pop-up
def alert_popup():
    
    root = Tk()
    root.title("title")
    
    sw = root.winfo_screenwidth()
    sh = root.winfo_screenheight()
    root.geometry('%dx%d+%d+%d' % (sw/2, sh/2, sh/2, sw/2))
    w = Label(root, text=" ", width=120, height=10)
    w.grid()
    b = Button(root, text="OK", command=root.destroy, width=10)
    b.grid()


#code to select the option and display the pop-up

drop = OptionMenu( root , clicked , *options )
drop.grid(pady= 10)
print(clicked.get())
def changed():
    print (clicked.get())
clicked.trace("w", changed)

# each time i select this particular option the pop-up should open
if(clicked.get() == "Particular Option"): 
    alert_popup()

Ishaan Joshi
  • 49
  • 1
  • 4
  • Move the if statement inside your function. – TheLizzard May 22 '21 at 19:35
  • You missed the `root.mainloop()` inside the pop-up function. – Martin Wettstein May 22 '21 at 20:11
  • @MartinWettstein Assuming that OP has `.mainloop()` at the end of their code (in the global scope), there is need for `.mainloop()` inside the function. For more info look at [this](https://stackoverflow.com/questions/67007447/new-root-mainloop-doesnt-make-main-window-unresponsive) question that I asked – TheLizzard May 22 '21 at 20:27
  • It's not just a new toplevel, it's a completely different Tk instance. I don't think the mainloop of the first window covers that. Worse still, both instances are called `root`. – Martin Wettstein May 22 '21 at 20:34
  • @MartinWettstein Well surprisingly a simple `.mainloop()` covers all `Tk()` windows including new ones that are made. Did you look at the link that I sent you? I think you are going to find it interesting to run the code and see it for yourself – TheLizzard May 22 '21 at 20:36
  • Yes, thanks. I just read it and I am surprised. I would never open a second Tk instance, but it seems that they behave counter-intuitively. – Martin Wettstein May 22 '21 at 20:39

0 Answers0