我有一个TK菜单小部件,在做一些副作用之后,想要在单击时删除该命令。所有代码都在DocumentView
类中,因此self.varnames
self.root= Tk()
self.root.title("Document Screen")
self.root.geometry(str(DocHeight)+"x"+str(DocHeight))
self.mainMenu = Menu(self.root)
self.allMembersMenu = Menu(self.updateMembersMenu)
for member in self.allUsers:
self.allMembersMenu.add_command(label=member,command=lambda i= member:
self.removeUser(i))
这将为系统中的每个用户发出一条命令。删除用户的定义如下
def removeUser(self,uname):
print("Remove User Function , uname: {}".format(uname))
x=0
mem = self.currentDoc.getMembers()
delmem=""
for i in range(0,len(mem)):
if (mem[i]==uname):
x=i
delmem = mem[i]
break
self.allMembersMenu.delete(x)
self.allUserMenu.destroy
self.currentDoc.removeMember(delmem)
使用打印功能,我知道传递了正确的变量,但是菜单中没有任何变化,我是否缺少某种Menu.reDraw()
方法?
如果不存在,有没有办法销毁此子菜单并重新绘制它?
答案 0 :(得分:0)
import tkinter as tk
root = tk.Tk()
root.geometry("400x600")
root.option_add("*tearOff", False)
menuBar = tk.Menu(root)
root.config(menu=menuBar)
usersMenu = tk.Menu(menuBar)
delUserMenu = tk.Menu(menuBar)
menuBar.add_cascade(menu=usersMenu, label="Users")
usersMenu.add_cascade(menu=delUserMenu, label="Delete")
usernamelist = ["Ali", "Ahmed", "Muhamed"]
def del_user(username):
delUserMenu.delete(delUserMenu.index(username))
print(username, "deleted!")
def command(username): return lambda :del_user(username)
for username in usernamelist:
delUserMenu.add_command(label=username, command=command(username))
root.mainloop()