python - Tkinter menubar class -
i'm trying make code more efficient have class creating menu bar , contents. no errors being reported, won't make bar. (i did have working without class, can't seem working again!)
from tkinter import * class menubar: #'player' name of tk window def __init__(self, menuname, label, drop_label, command, separator = false): self.menubar = menu(player) self.menuname = menu(self.menubar, tearoff = 0) self.menuname.add_command(label = label, command = command) if separator == true: self.menuname.add_separator() self.menubar.add_cascade(label = drop_label, menu = menuname) self.create() def create(self): player.config(menu = self.menubar) #example menu item def addmenubar(): exitmenu = menubar("filemenu", "exit", "file", onexit, true) #more menu items here, function keep tidier. def onexit(): #code here player = tk() addmenubar() player.mainloop() this draw tk window, no menu bar options, going wrong? cheers.
in __init__ function create menu bar.
create function adds menus , accept commands list of tuples:
from tkinter import * class menubar: #'player' name of tk window def __init__(self, parent): self.menubar = menu(parent) self.create() def create(self): player.config(menu = self.menubar) def add_menu(self, menuname, commands): menu = menu(self.menubar, tearoff = 0) command in commands: menu.add_command(label = command[0], command = command[1]) if command[2]: menu.add_separator() self.menubar.add_cascade(label=menuname, menu=menu) def onexit(): import sys sys.exit() def onopen(): print 'open' player = tk() menubar = menubar(player) filemenu = menubar.add_menu("file", commands = [("open", onopen, true), ("exit", onexit, false)]) player.mainloop()
Comments
Post a Comment