Gavin
Gavin

Reputation: 13

grid_forget() still leads to error when using pack later on

I have a program I'm working on where, to keep it in the same window, I have my widgets in a frame. When I want to change the window, then I use either frame.pack_forget() or frame.grid_forget() and then frame.destroy() before simply adding a new frame to the new window. However, even having used grid_forget, if I use .pack(), I get an error saying I can't use pack when something is already managed by grid. Does anyone know how to circumvent this while still keeping everything within the same window?

.pack_forget seems to be working fine as I can transition from a frame using pack to a frame using grid without issue.

Here is a reproduction of the problem:

from tkinter import *

root = Tk()


def main_Menu (root):
    frame = Frame(root)
    frame.pack()

    button = Button(frame, text="button ", command=lambda:[frame.pack_forget(), frame.destroy, function(root)])
    button.pack()

def function(root):
    frame = Frame(root)
    frame.grid(row=0)

    back_Button = Button(root, text="Back", command=lambda:[frame.grid_forget(), frame.destroy(), return_To_Menu(root)])
    back_Button.grid(row=0)

def return_To_Menu(root):
    main_Menu(root)

main_Menu (root)

Upvotes: 0

Views: 38

Answers (1)

Thomas H.
Thomas H.

Reputation: 49

Your packed Button is attached to the frame, while the grided button is attached to the root.

After changing

    back_Button = Button(root, text="Back", command=lambda:[frame.grid_forget(), frame.destroy(), return_To_Menu(root)])
    back_Button.grid(row=0)

to

    back_Button = Button(frame, text="Back", command=lambda:[frame.grid_forget(), frame.destroy(), return_To_Menu(root)])
    back_Button.grid(row=0)

it worked for me.

Upvotes: 1

Related Questions