'Unable to close popup frame

I'm currently creating a program that will open a popup window for the user to confirm their actions.

    def ConfirmActions():
        popup = tk.Toplevel(window)
        popup.geometry("500x100")
        popup.title("Confirmation")

        def DeletePopup():
            popup.pack_forget()

        def PerformAction():
            #code that performs the desired action
            DeletePopup()

        def Widgets():
            confirmation = tk.Label(popup,
                             text="Are you sure?",
                             font=("comic sans", 12))
            confirmation.place(x=250, y=20, anchor = 'center')

            confirm = tk.Button(popup,
                             text="Yes",
                             width = 10,
                             command = PerformAction,
                             font=("comic sans", 12))
            confirm.place(x=100, y = 60)

            deny = tk.Button(popup,
                             text="No",
                             width = 10,
                             command = DeletePopup,
                             font=("comic sans", 12))
            deny.place(x=300, y = 60)

        Widgets()

However, whenever the subroutine for closing the window is run I get this error message

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\AppData\Local\Programs\Python\Python39\lib\tkinter_init_.py", line 1892, in call return self.func(*args) File "C:\Filepath", line 945, in PerformAction DeletePopup() File "C:\Filepath", line 941, in DeletePopup popup.pack_forget() AttributeError: 'Toplevel' object has no attribute 'pack_forget'

I don't want to use .destroy() as this popup screen should be able to be used multiple times when the program is running and destroying the screen will make it unable to be recovered

Edit: I have decided to use .withdraw() and .deiconify() to hide and display each frame



Solution 1:[1]

Since you wish to reuse the popup window, you may find this code snippet useful. It will open and close popup window by harnessing popup.protocol and Key-F2.

I've also included a master close function using master.protocol and Key-Escape.

import tkinter as tk

master = tk.Tk()

master.wait_visibility()

popup = tk.Toplevel(master)
popup.withdraw()
popup.transient(master)


def closer(event = None):
    master.destroy()


def toggle(event = None):
    """Toggle switch for toplevel"""
    if popup.winfo_viewable( ):
        popup.withdraw( )
        master.focus_set( )
    else:
        popup.focus_set( )
        popup.deiconify()

# Bind master to closer functon via EXIT button, Alt+F4 and Escape Key
master.protocol("WM_DELETE_WINDOW", closer)
master.bind("<Escape>", closer)

# Bind popup to toggle function via EXIT button, Alt+F4 and Key-F2
popup.protocol("WM_DELETE_WINDOW", toggle)
popup.bind("<F2>", toggle) # access in popup
master.bind("<F2>", toggle) # access in master

master.mainloop()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Derek