I have a gui application where the navbar has buttons to switch between frames. On the startpage there is an entry widget to get a file name and a listbox to display all the saved filenames. When a filename is selected from the listbox another page opens which displays the selected filename.
I believe that the answer here creates frame objects in the beginning, stack them and raise whichever frame is called. But I think that my window needs to update every time it is called so that it can display the different filename. Also it is placed at exactly the same position like the other frames so that I can still use to navbar buttons for switching to other home and other windows. I found something similar to this here but I cannot understand it.
(my acutal application is a bit more complex than this but i think that this is a small and precise representation of my problem)
Edit:
import tkinter as tk
class MainApplication(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(1, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames={}
for F in (StartPage,PageOne):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=1, column=0, sticky="nsew")
self.show_frame("StartPage")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
self.list_box = tk.Listbox(self)
self.list_box.pack()
for item in ["file 1", "file 2", "file 3"]:
self.list_box.insert(tk.END, item)
button1 = tk.Button(self, text='Go to next Page', command=lambda: self.getvalue(controller))
button1.pack()
def getvalue(self, controller):
clicked_item=self.list_box.curselection()
selected_file=self.list_box.get(clicked_item)
print(selected_file)
controller.show_frame('PageOne')
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
# ....selected file name to be displayed here....
button1 = tk.Button(self, text="Back to Home", command=lambda: controller.show_frame('StartPage'))
button1.pack()
app = MainApplication()
app.geometry("400x400")
app.mainloop()