Skip to content

Instantly share code, notes, and snippets.

@vicperotti
Last active October 7, 2019 15:57
Show Gist options
  • Save vicperotti/c22ba8fe72c13dd86cd35a37a51de73e to your computer and use it in GitHub Desktop.
Save vicperotti/c22ba8fe72c13dd86cd35a37a51de73e to your computer and use it in GitHub Desktop.
Adding Menuing to an existing Example for Tkinter
# modified from https://pythonprogramming.net/change-show-new-frame-tkinter/
# The code for changing pages was derived from: http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
# License: http://creativecommons.org/licenses/by-sa/3.0/
import tkinter as tk
LARGE_FONT = ("Verdana", 12)
class SeaofBTCapp(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(0, weight=1)
container.grid_columnconfigure(0, weight=1)
# New Menu Code Here
# create a toplevel menu
menubar = tk.Menu(self)
menubar.add_command(label="Show Frame 1", command=self.hello)
menubar.add_command(label="Show Frame 2", command=self.hello2)
menubar.add_command(label="Quit!", command=self.quit)
# display the menu
self.config(menu=menubar)
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def hello(self):
print("Showing Frame 1")
frame = self.show_frame(PageOne)
frame.tkraise()
def hello2(self):
print("Showing Frame 2")
frame = self.show_frame(PageTwo)
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Start Page", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button = tk.Button(self, text="Visit Page 1",
command=lambda: controller.show_frame(PageOne))
button.pack()
button2 = tk.Button(self, text="Visit Page 2",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text="Page Two",
command=lambda: controller.show_frame(PageTwo))
button2.pack()
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = tk.Button(self, text="Back to Home",
command=lambda: controller.show_frame(StartPage))
button1.pack()
button2 = tk.Button(self, text="Page One",
command=lambda: controller.show_frame(PageOne))
button2.pack()
app = SeaofBTCapp()
app.mainloop()
@vicperotti
Copy link
Author

I modified an existing example I found (see first line) to allow the menu items to choose among frames.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment