Tkinter menu toplevel or frame

M

Matimus

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.makeMenuBar()

def makeMenuBar(self):
self.menubar = Menu(self.master)
self.master.config(menu=self.menubar)

pulldown = Menu(self.menubar, tearoff=0)
pulldown.add_command(label='New', command=self.new)
pulldown.add_command(label='Open', command=self.onOpen)
pulldown.add_command(label='Save', command=self.save)
pulldown.add_command(label='Save As', command=self.saveas)
pulldown.add_separator()
pulldown.add_command(label='Exit', command=self.onExit)
self.menubar.add_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__(self, master, **kwargs):
self.master = master

# This is equivalent to self packing, do it outiside of the
widget
# self.master.config(menu=self.menubar)

pulldown = Menu(self, tearoff=0)
pulldown.add_command(label='New', command=self.new)
pulldown.add_command(label='Open', command=self.onOpen)
pulldown.add_command(label='Save', command=self.save)
pulldown.add_command(label='Save As', command=self.saveas)
pulldown.add_separator()
pulldown.add_command(label='Exit', command=self.onExit)
self.add_cascade(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.master)
self.master.config(menu=self.menubar)

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

Gigs_

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(width=200, height=200)
self.pack(fill=BOTH, expand=YES)
self.makeMenuBar()

def makeMenuBar(self):
self.menubar = Menu(self.master)
self.master.config(menu=self.menubar)
pulldown = Menu(self.menubar, tearoff=0)
pulldown.add_command(label='New', command=self.new)
pulldown.add_command(label='Open', command=self.onOpen)
pulldown.add_command(label='Save', command=self.save)
pulldown.add_command(label='Save As', command=self.saveas)
pulldown.add_separator()
pulldown.add_command(label='Exit', command=self.onExit)
self.menubar.add_cascade(label='File', underline=0, menu=pulldown)


Please tell me is here anything that I should change.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top