0

I'm trying to create a number of frames to be accessed through the use of the menu widget. When using the menu, you can click on one of the commands - it would bring up a frame and the menu widget should still be at the top, so you can easily decide where to go.

I'm trying to make use of the option menu widget within a function which is called after a login page, therefore I'm using the top level method within it. When attempting to do this option menu I encountered a few problems and I'm currently stuck as well as not understanding what's wrong with the code, so I was hoping someone would tell me what's wrong with it.

CoreContent = function named

myGUI = main root

def CoreContent():

#Building core content/structure 
   myGUI.withdraw() # This is the main root that I remove after user logs in
    CoreRoot = Toplevel(myGUI, bg="powderblue") # Toplevel 
    CoreRoot.title("titletest")
    CoreRoot.geometry('300x500')
    CoreRoot.resizable(width=False, height=False)

#Creating drop-down menu
    menu = Menu(CoreRoot)
    CoreRoot.config(menu=menu)
    filemenu = Menu(menu)
    menu.add_cascade(label="File", menu=filemenu)
    filemenu.add_command(label="test one", command=lambda: doNothing()) # Problem
    filemenu.add_command(label="soon")
    filemenu.add_separator()
    filemenu.add_command(label="Exit")

I'm confused how and where I should create the frames to add as a command to make use of within the option menu widget.

sago
  • 11
  • 5

1 Answers1

0

For a clear description of how to switch between frames in Tkinter, check this link: Switch between two frames in tkinter

To do it from a menu, you could write something like this:

import tkinter as tk

# method to raise a frame to the top
def raise_frame(frame):
    frame.tkraise()

# Create a root, and add a menu
root = tk.Tk()
menu = tk.Menu(root)
root.config(menu=menu)
filemenu = tk.Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="test one", command=lambda: raise_frame(f1))
filemenu.add_command(label="test two", command=lambda: raise_frame(f2))

# Create two frames on top of each other
f1 = tk.Frame(root)
f2 = tk.Frame(root)
for frame in (f1, f2):
    frame.grid(row=0, column=0, sticky='news')

# Add widgets to the frames
tk.Label(f1, text='FRAME 1').pack()
tk.Label(f2, text='FRAME 2').pack()

# Launch the app
root.mainloop()
Community
  • 1
  • 1
Josselin
  • 2,593
  • 2
  • 22
  • 35