Tkinter menu toplevel or frame

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gigs_

    #1

    Tkinter menu toplevel or frame

    what is the best way to write tkinter menus? As toplevels or as frame
    with Menubutton?

    im doing like this
    class MyWidget(Frame) :
    def __init__(self, master=None): """ should this master be
    parent? Because my first tought was that this is toplevel master than i
    found that its not"""
    Frame.__init__( self, master) # and this too than
    self.config(wid th=200, height=200)
    self.pack(fill= BOTH, expand=YES)
    self.makeMenuBa r()

    def makeMenuBar(sel f):
    self.menubar = Menu(self.maste r)
    self.master.con fig(menu=self.m enubar)
    pulldown = Menu(self.menub ar, tearoff=0)
    pulldown.add_co mmand(label='Ne w', command=self.ne w)
    pulldown.add_co mmand(label='Op en', command=self.on Open)
    pulldown.add_co mmand(label='Sa ve', command=self.sa ve)
    pulldown.add_co mmand(label='Sa ve As', command=self.sa veas)
    pulldown.add_se parator()
    pulldown.add_co mmand(label='Ex it', command=self.on Exit)
    self.menubar.ad d_cascade(label ='File', underline=0, menu=pulldown)


    Please tell me is here anything that I should change.
  • Matimus

    #2
    Re: Tkinter menu toplevel or frame

    Please tell me is here anything that I should change.

    The way you have written it, master _must_ be a Toplevel object. So,
    maybe parent is the correct name, but it doesn't really matter.

    As a side note, there is no reason for this class to inherit Frame.
    Aside from packing and sizing the frame, you appear to do nothing else
    with it. You could just as easily inherit from object. You should
    never make a custom widget that packs itself anyway. A frame object
    should act like a frame. What you have made looks more like what I
    would call an application class. If you are trying to make a Menu, I
    would inherit from that instead.

    As an application class (you will still need the new,save... methods
    to be defined):

    class MyApp(object):
    def __init__(self, master=None):
    if master:
    self.master = master
    else:
    self.master = Tk()

    frame = Frame(master, width=200, height=200)
    frame.pack(fill =BOTH, expand=YES)

    self.makeMenuBa r()

    def makeMenuBar(sel f):
    self.menubar = Menu(self.maste r)
    self.master.con fig(menu=self.m enubar)

    pulldown = Menu(self.menub ar, tearoff=0)
    pulldown.add_co mmand(label='Ne w', command=self.ne w)
    pulldown.add_co mmand(label='Op en', command=self.on Open)
    pulldown.add_co mmand(label='Sa ve', command=self.sa ve)
    pulldown.add_co mmand(label='Sa ve As', command=self.sa veas)
    pulldown.add_se parator()
    pulldown.add_co mmand(label='Ex it', command=self.on Exit)
    self.menubar.ad d_cascade(label ='File', underline=0,
    menu=pulldown)

    A menu class (this is untested, but it is close to how I would do it):

    class MyMenu(Menu):
    def __init__(self, master=None, **kwargs):
    Menu.__init__(s elf, master, **kwargs):
    self.master = master

    # This is equivalent to self packing, do it outiside of the
    widget
    # self.master.con fig(menu=self.m enubar)

    pulldown = Menu(self, tearoff=0)
    pulldown.add_co mmand(label='Ne w', command=self.ne w)
    pulldown.add_co mmand(label='Op en', command=self.on Open)
    pulldown.add_co mmand(label='Sa ve', command=self.sa ve)
    pulldown.add_co mmand(label='Sa ve As', command=self.sa veas)
    pulldown.add_se parator()
    pulldown.add_co mmand(label='Ex it', command=self.on Exit)
    self.add_cascad e(label='File', underline=0, menu=pulldown)

    You can use this in the application class:

    class MyApp(object):
    def __init__(self, master=None):
    if master:
    self.master = master
    else:
    self.master = Tk()

    self.menubar = MyMenu(self.mas ter)
    self.master.con fig(menu=self.m enubar)

    frame = Frame(master, width=200, height=200)
    frame.pack(fill =BOTH, expand=YES)




    Comment

    Working...