0

so i am confused on how to add menu options to my windows... i clearly can make a menubar and know how to do that as you can see in my code. however, the way i have the windows change from one to another is affecting things because i have only 1 Tk.tk window. the rest of the program operates inside that window. I would like to be able to change the title and the menu for each window.

class start(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)

        self.frames = {}

        tk.Tk.title(self, "Group Registration")

        menubar = tk.Menu(container)
        tk.Tk.config(self, menu=menubar)
        fileMenu = tk.Menu(menubar, tearoff=0)
        menubar.add_cascade(label="File", menu=fileMenu)
        fileMenu.add_command(label="Exit", command=quit)

        for F in (begin, admin_main, members):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(begin)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()
        frame.event_generate("<<showframe>>")


class begin(tk.Frame):
    pass

class admin_main(tk.Frame):

    def __init__(self, parent, controller):

        self.bind("<<showframe>>", self.on_show_frame)

    def on_show_frame(self, event):

        menubar = Menu(self)
        fileMenu = Menu(menubar)
        menubar.add_cascade(label="File", menu=fileMenu)
        fileMenu.add_command(label="Create member list")#command=)
        fileMenu.add_command(label="Load member list")#command=)
        fileMenu.add_command(label="Email")#command=)
        fileMenu.add_separator()
        fileMenu.add_command(label="Exit")

        print"ok"


class members(tk.Frame):
    pass

app = start()
app.geometry("600x400")
app.mainloop()

the classes are my new windows. this is just short version of code to show how windows operate...

(REVISED) interpreter will print "ok" when page opens. there is no syntax with it revised again... however, i do not have my new menubar showing up.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
ricky6991
  • 53
  • 9
  • Have you read the following? https://stackoverflow.com/questions/35029188/how-would-i-make-a-method-which-is-run-every-time-a-frame-is-shown-in-tkinter – Bryan Oakley Jun 06 '17 at 22:14
  • i have now lol... i have not used event_generate before but from what i am reading on google i can do things like or things of that sort. i do not understand how to use it to add to my menu though. would i be adding code inside the event_generate() or bind it like example says and add the code inside def on_show_frame(). – ricky6991 Jun 06 '17 at 23:10
  • You don't add events to your menu. You use the even to call a function that creates the menu. You make the menu the way you're doing it in the above code, only you do it in the function that gets called when the page appears rather than in the code that creates the page. – Bryan Oakley Jun 06 '17 at 23:19
  • please don't get frustrated with me, i am learning and appreciate help. so, i revised code above in my question. i can run my code without a syntax. UNTIL, i open my class admin_main(): then i get the following syntax.... EDITED(syntax also in above) – ricky6991 Jun 07 '17 at 00:12
  • sorry to keep going, but i am working on this as i go... i revised above again... my menubar will not show new commands. only shows the manubar that is origionally done in class start():. – ricky6991 Jun 07 '17 at 00:41

1 Answers1

0

You are very close (except for all of the errors in the code in your question...)

However, assuming you can fix all of the errors, the only piece you're missing is to actually configure the main window to use the menubar that you've created.

You need to add a couple lines to tell the window about the menubar. You can get the window by calling winfo_toplevel(), and then you can configure that widget to have the menu:

def on_show_frame(self, event):
    menubar = tk.Menu(...)
    ...
    top = self.winfo_toplevel()
    top.configure(menu=menubar)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • awesome, this worked great... i am going to look up winfo_toplevel() and see what that entails. i am learning a ton as i go. i tried to do tk.configure and it wouldnt work before so i knew i was missing something. Also, the code in my question was just to give you an example of how i setup my classes, my actual code i fully functioning. Thanks, for being patient with me! – ricky6991 Jun 08 '17 at 19:04