'tkinter enable/disable menu
I need some help in this. I want the user to login ... once he is logged in, only the menu corresponding to the module he is allowed to access will be enabled. So, I need to disable all the menus (except File and Help) in the beginning.. and I need to enable the corresponding menu after login...
the login code is not written yet.. I just want to see how to do that... My code is below:
import Tkinter as tk
class gv:
w = 800
h = 500
title = 'gsm ...'
MDLS = [['File','Settings','_s','Quit'],
['main menu','mm1','mm2'],
['second menu','sm1','sm2','sm3'],
['Help','About']]
class GammaSoft(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master, width=gv.w, height=gv.h)
self.master.title(gv.title)
self.master.resizable(0,0)
self.pack_propagate(0)
self.pack()
menu_bar = tk.Menu()
self.nMenu = []
mc = 0
for MDL in gv.MDLS:
self.nMenu.append(tk.Menu(menu_bar, tearoff=0))
for itm in MDL[1:]:
if itm == '_s':
self.nMenu[mc].add_separator()
else:
self.nMenu[mc].add_command(label=itm, command=lambda itm=itm: self.menu_call(itm))
menu_bar.add_cascade(label=MDL[0], menu=self.nMenu[mc])
mc += 1
self.master.config(menu=menu_bar)
frame1 = tk.Frame(self)
frame1.pack()
lblUsername = tk.Label(frame1, text='Username: ')
lblPassword = tk.Label(frame1, text='Password: ')
lblModule = tk.Label(frame1, text='Module: ')
lblSpace1 = tk.Label(frame1, text=' ')
lblSpace2 = tk.Label(frame1, text=' ')
self.Username_var = tk.StringVar()
self.txtUsername = tk.Entry(frame1, textvariable=self.Username_var, width=14)
self.Password_var = tk.StringVar()
self.txtPassword = tk.Entry(frame1, textvariable=self.Password_var, show="x", width=14)
self.Module_var = tk.StringVar()
options = [Li[0] for Li in gv.MDLS[1:len(gv.MDLS)-1]]
self.Module_var.set(options[0])
self.cmbModule = tk.OptionMenu(frame1, self.Module_var, *tuple(options))
self.cmbModule.config(takefocus=1, width=14)
self.btnLiLo = tk.Button(frame1, text='Log in', command=self.LiLo)
lblUsername.pack(side=tk.LEFT)
self.txtUsername.pack(side=tk.LEFT)
lblSpace1.pack(side=tk.LEFT)
lblPassword.pack(side=tk.LEFT)
self.txtPassword.pack(side=tk.LEFT)
lblSpace2.pack(side=tk.LEFT)
lblModule.pack(side=tk.LEFT)
self.cmbModule.pack(side=tk.LEFT)
self.btnLiLo.pack(side=tk.RIGHT)
separator = tk.Frame(self,height=2, bd=1, relief=tk.SUNKEN)
separator.pack(fill=tk.X, padx=5, pady=5)
frame2 = tk.Frame(self)
frame2_up = tk.Frame(frame2)
frame2_up.pack(fill=tk.X, side=tk.TOP)
frame2_dn = tk.Frame(frame2)
frame2_dn.pack(fill=tk.BOTH, side=tk.BOTTOM)
lblFilter = tk.Label(frame2_up, text='Filter Text: ')
lblAt = tk.Label(frame2_up, text='At: ')
lblSpace3 = tk.Label(frame2_up, text=' ')
self.Filter_var = tk.StringVar()
self.txtFilter = tk.Entry(frame2_up, textvariable=self.Filter_var, width=14)
self.At_var = tk.StringVar()
self.cmbAt = tk.OptionMenu(frame2_up, self.At_var, '')
self.cmbAt.config(takefocus=1, width=14)
self.btnGo = tk.Button(frame2_up, text='Apply')
self.btnClr = tk.Button(frame2_up, text='Clear')
lblFilter.pack(side=tk.LEFT)
self.txtFilter.pack(side=tk.LEFT)
lblSpace3.pack(side=tk.LEFT)
lblAt.pack(side=tk.LEFT)
self.cmbAt.pack(side=tk.LEFT)
self.btnClr.pack(side=tk.RIGHT)
self.btnGo.pack(side=tk.RIGHT)
frame2.pack(fill=tk.BOTH)
self.log_out
def log_out(self):
pass
def LiLo(self):
pass
def print_out(self):
print('%s, %s!' % (self.greeting_var.get().title(), self.recipient_var.get()))
def menu_call(self, data):
print "Once more, clicked / commanded %s" % data
def run(self):
self.mainloop()
app = GammaSoft(tk.Tk())
app.run()
Solution 1:[1]
You can enable or disable an entire menu by enabling or disabling the item it is connected to.
Here's a contrived example. Use the Test1
menu to enable or disable the Test2
menu.
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
self.menubar = tk.Menu()
self.test1Menu = tk.Menu()
self.test2Menu = tk.Menu()
self.menubar.add_cascade(label="Test1", menu=self.test1Menu)
self.menubar.add_cascade(label="Test2", menu=self.test2Menu)
self.test1Menu.add_command(label="Enable Test2", command=self.enable_menu)
self.test1Menu.add_command(label="Disable Test2", command=self.disable_menu)
self.test2Menu.add_command(label="One")
self.test2Menu.add_command(label="Two")
self.test2Menu.add_command(label="Three")
self.test2Menu.add_separator()
self.test2Menu.add_command(label="Four")
self.test2Menu.add_command(label="Five")
root.configure(menu=self.menubar)
def enable_menu(self):
self.menubar.entryconfig("Test2", state="normal")
def disable_menu(self):
self.menubar.entryconfig("Test2", state="disabled")
if __name__ == "__main__":
root = tk.Tk()
root.geometry("500x500")
app = Example(root)
app.pack(fill="both", expand=True)
root.mainloop()
Solution 2:[2]
could use below as well... but I want to disable the parent menu, not the items one by one...
self.nMenu[0].entrycget(0, 'label')
self.nMenu[0].entryconfigure('Quit', state=tk.DISABLED)
self.nMenu[0].entryconfigure(0)
Solution 3:[3]
To deactivate the whole menu in a menubar write:
menubar.entryconfig(1, state = DISABLED)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Bryan Oakley |
Solution 2 | Book Of Zeus |
Solution 3 | nfn |