0
votes

I've been diving into Python 3/Tkinter and would like to create a menubutton inside of a frame that, when clicked on, will drop down a list of options with radio buttons. Ideally, I would like to set indicatoron=0 for my radio buttons, but have ran into an issue with my code where the menubutton will not appear in the app window. All guides I've looked at demonstrated the ability to set a radio button list under the Menu Bar or creating a menubutton with a list of checkbutton options.

When you run this code, it prompts an empty window with the 'minimize - expand - close' buttons. Once you expand the app window, nothing is displayed.

from tkinter import *

class TestMenu:
        def __init__(self, master):
            self.master = master
            self.menubar = Menubutton(self.master, text='Menubutton', relief=RAISED,
                                    activebackground='#3399ff', bg='white', fg='black')
            mbmenu = Menu(self.menubar, tearoff=0)
            mb_radmenu = Menu(mbmenu)

            mb_radmenu.add_radiobutton(label='A')
            mb_radmenu.add_radiobutton(label='B')
            mb_radmenu.add_radiobutton(label='C')

            mbmenu.add_cascade(menu=mb_radmenu)
            self.top = Toplevel(menu=self.menubar, width=200, relief=RAISED, borderwidth=2)

def main():
    root = Tk()
    root.withdraw()
    app = TestMenu(root)
    root.mainloop()

if __name__ == '__main__':
    main()
1
You say you want to create a button in a frame, but then you try to add the button as the menu for a window. Which are you trying to do? Do you want a traditional menubar, or a single button? - Bryan Oakley
Thats the thing, I dont want a traditional menubar, just a single button. What I find odd is the previous code I created with a single button with checkbuttons utilized the Menu() function. I guess that is where the confusion is at. - kaztecian

1 Answers

0
votes

There are many things wrong with your code.

  • You can't use a Menubutton as the value for the menu option -- it needs to be an instance of Menu.

  • You are creating self.menubar as a child of the root window rather than the toplevel. A menubar needs to be a child of the window that it is in.

  • You must link the menu to the menubutton in addition to making the menu a child of the button (eg: self.menubar.configure(menu=mb_radmenu))

  • When you add a cascade, you need to give a label (eg: mbmenu.add_cascade(label="Rad", menu=mb_radmenu))

  • If you want to create a button rather than a traditional menubar, you don't need to configure the menu option of the window

Here's a version of your code that fixes all of those problems:

from tkinter import *

class TestMenu:
        def __init__(self, master):
            self.master = master

            self.top = Toplevel(width=200, relief=RAISED, borderwidth=2)
            self.menubar = Menubutton(self.top, text='Menubutton', relief=RAISED,
                                      activebackground='#3399ff', bg='white', fg='black')
            self.menubar.pack(side="top")

            mb_radmenu = Menu(self.menubar)
            self.menubar.configure(menu=mb_radmenu)

            mb_radmenu.add_radiobutton(label='A')
            mb_radmenu.add_radiobutton(label='B')
            mb_radmenu.add_radiobutton(label='C')

def main():
    root = Tk()
    root.withdraw()
    app = TestMenu(root)
    root.mainloop()

if __name__ == '__main__':
    main()

I didn't change it in the example, but I strongly encourage you not to name the menu button self.menubar. Your code is hard to understand when you mix them up like that.