1

我有一个 TK Menu 小部件,并且想在单击时删除命令,在做了一些副作用之后。所有代码都在一个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()使用打印功能,我知道正在传递正确的变量,但菜单中没有发生任何变化,我是否缺少某种方法?

如果那不存在,有没有办法销毁这个子菜单并重新绘制它?

4

1 回答 1

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()
于 2020-11-24T17:58:59.520 回答