2014-05-03 11 views
8

Ho bisogno di aiuto in questo. Desidero che l'utente effettui il login ... una volta effettuato l'accesso, sarà abilitato solo il menu corrispondente al modulo a cui è consentito l'accesso. Dunque, ho bisogno di disabilitare tutti i menu (eccetto File e Guida) all'inizio ... e devo abilitare il menu corrispondente dopo il login ...tkinter abilita/disabilita menu

il codice di accesso non è stato ancora scritto .. Io solo voglio vedere come fare ... il mio codice è qui sotto:

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() 
+0

Ho notato che quando aggiungo cascate a menu_bar, se lo faccio come sotto, rende tutti i menu disabilitati ... menu_bar.add_cascade (label = MDL [0], il menu = self.nMenu [mc], state = tk.DISABLED) – mlwn

+0

Potrei usare anche qui sotto ... ma voglio disabilitare il menu genitore, non gli articoli uno per uno ... self.nMenu [0] .entrycget (0, 'label') self.nMenu [0] .entryconfigure ('Quit', stato = tk.DISABLED) self.nMenu [0] .entryconfigure (0) – mlwn

+0

Hai letto la documentazione sul comando di menu per vedere se c'è un modo per disabilitare qualcosa? Che ricerca hai fatto, o che cosa hai provato a risolvere questo problema? –

risposta

13

è possibile attivare o disattivare un intero menu attivando o disattivando la voce si è collegato.

Ecco un esempio forzato. Utilizzare il menu Test1 per abilitare o disabilitare il menu Test2.

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() 
+1

Ora capisco quanto sia facile e diretta la soluzione ... :) Ho ancora un'altra domanda .. è possibile disabilitare/abilitare un frame? o devo andare in bicicletta ai suoi figli uno per uno? – mlwn