0
votes

I'm trying to incorporate a 'back button' in my Tkinter app in my 'second_home' to allow the user to return to the previous page - from second_home to home. I've incorporated a hide_frames() function to allow all the frames to disappear when called. I have this done on the second_home but I've commented out the function on the home function - the app won't run with the function called. I also have the home function called when launching the app.

When clicking on the back button titled "Previous Page" I receive the error 'TclError: bad window path name ".!labelframe.!canvas'

from tkinter import *
from tkinter import ttk
from tkinter.font import BOLD

win = Tk()

# Creating Frames
wrapper1 = LabelFrame(win, width = 900, height = 950)
wrapper2 = LabelFrame(win, width = 900, height = 950)
wrapper3 = LabelFrame(win, width = 900, height = 950)
wrapper4 = LabelFrame(win, width = 900, height = 950)

mycanvas = Canvas(wrapper1)
mycanvas.pack(side = LEFT, fill = "both", expand ="yes")


# Creating a Hide Frame Function
def hide_frames():
    # Destroying the widgets in each frame
    for widget in wrapper1.winfo_children():
        widget.destroy()
    for widget in wrapper2.winfo_children():
        widget.destroy()
    for widget in wrapper3.winfo_children():
        widget.destroy()
    for widget in wrapper4.winfo_children():
        widget.destroy()
    
    # Hiding all frames
    wrapper1.pack_forget()
    wrapper2.pack_forget()
    wrapper3.pack_forget()
    wrapper4.pack_forget()

def home():
    #hide_menu_frames()

    global myframe
    myframe = Frame(mycanvas)
    mycanvas.create_window((0,0), window = myframe, anchor = "nw")

    wrapper1.pack(fill = "both", expand = "yes", padx = 10, pady = 10)

    myframe.pack(fill = "both", expand = 1)
    start_label = Label(myframe, text = "Choose Length of Loan Model", font = ("Helvetica", 19)).pack(pady = 100)

    # Creating buttons
    home_button1 = Button(myframe, text = "12 Month Model", bg='#ffffff', activeforeground='#4444ff', font = ("Helvetica", 16, BOLD), command = second_home).pack(pady = 10)

def second_home():
    hide_frames()

    mycanvas = Canvas(wrapper2)
    
    mycanvas.pack(side = LEFT, fill = "both", expand ="yes")

    yscrollbar = ttk.Scrollbar(wrapper2, orient = VERTICAL, command = mycanvas.yview)
    
    yscrollbar.pack(side = RIGHT, fill = "y")
    
    mycanvas.configure(yscrollcommand = yscrollbar.set)
    
    mycanvas.bind('<Configure>',lambda e: mycanvas.configure(scrollregion = mycanvas.bbox('all')))

    wrapper2.pack(fill = "both", expand = "yes", padx = 10, pady = 10)

    myframe = Frame(mycanvas)
    mycanvas.create_window((0,0), window = myframe, anchor = "nw")

    second_label = Label(myframe, text = "Please Input Loan Parameters", font = ("Helvetica", 18), pady = 10, padx = 325).pack()

    # Creating a Back Button
    back_button = Button(myframe, text = "Previous Page", command = home)
    back_button.pack(pady = 10)



# Defining a Main Menu
my_menu = Menu(win)
win.config(menu = my_menu)

# Creating Menu Items
app_menu = Menu(my_menu)
my_menu.add_cascade(label = "Options", menu = app_menu)
app_menu.add_command(label = "Home", command = home)
app_menu.add_separator()
app_menu.add_command(label = "Exit", command = win.quit)

win.geometry("900x950")
#win.resizable(False, False)
win.title("Loan Cash Flow Model Creator")

home()

win.mainloop()
TclError: bad window path name ".!labelframe.!canvas' means that you're trying to use a widget after it has been destroyed. Once you destroy a widget, it is no longer usable. - Bryan Oakley