python tkinter: Custom menu while pulling down -
i'm learning creating software python , tkinter. need change menu items different conditions, not find easy way it. well, let me try explain question using example:
like shown in figure, have listbox on left , listbox on right. have menu move items around, commands "move right", "move left" , "exchange". following conditions considered:
- when items selected in left listbox, want command "move right" enabled, shown in figure.
- when items selected in right listbox, want command "move left" enabled.
- when items selected in both listboxes, want commands enabled.
- when no item selected, want commands disabled.
i know can work done binding events "listboxselect" , "button-1" functions, , use functions configure menu. complex work when have 5 listboxes in actual software. wondering whether there easy way this, overloading functions in tkinter.menu class (i tried overloading post(), grid(), pack() , place(), none of them works).
any idea welcomed.
i think want use postcommand
modify menu appropriate. if you're going have multiple listboxes, simplest solution may implement own class. here's rough idea:
class editmenu(tkinter.menu): def __init__(self, parent, listboxes, **kw): self.commandhook = kw.get('postcommand', none) kw['postcommand'] = self.postcommand super(editmenu, self).__init__(parent, **kw) self.listboxes = listboxes self.add_command(label="move right", command=self.move_to_right) self.add_command(label="move left", command=self.move_to_left) self.add_command(label="exchange", command=self.exchange) def postcommand(self): in xrange(3): # checks each entry # , set state either tkinter.disabled or tkinter.normal self.entryconfig(i, state=state) if self.commandhook not none: self.commandhook() # implement 3 functions here
if start add more items, you'll want create class each menu item. in class put in logic enable/disable , callback function implementation. comment if you'd see example.