我该如何调用分配了不同命令的按钮



我正在尝试制作一个程序,在下面显示每个管理员的名称,如下所示:

他们的名字更多信息按钮,引导用户进入他们的个人资料。但是如果我有20个管理员,那么我该如何用这个代码生成20个随机按钮呢?

代码:

i=0 
adminshower = tk.Toplevel()
how = len(admins)
scre = 50 x how
adminshower.geometry("200"+"x"+scre)
adminshower.title("Our admins")
while i != len(admins):
name = admins[i].get("name")
adminLabel = Label(adminshower, text=name).grid(row=i, column=0)
code for button here please help:
i+=1
this button should lead to something like this:
def moreinfo(name):
moreinfoshower = tk.Toplevel()
moreinfoshower.geometry("200x300")
moreinfoshower.title(adminsname)
name = "name: "+name
nameLabel = Label(adminshower, text=name).grid(row=0, column=0) 
email = accounts[name].get("email")
email = "email: "+email
emailLabel = Label(adminshower, text=email).grid(row=1, column=0)  
user = accounts[name].get("email")
user = "username: "+user
usernameLabel = Label(adminshower, text=user).grid(row=2, column=0)

请帮忙,我对任何错误感到抱歉。

阐述了ShayneLord的概念。多亏了他,我有了这个:

i=0
k=0
adminshower = tk.Toplevel()
how = len(admislist)
adminbuttons = []
adminlabels = []
scre = 25 * how
scre = str(scre)
adminshower.geometry("250"+"x"+scre)
adminshower.title("Our admins")
while i != len(admislist):
i+=1
user = admislist[i].get("username")
adminlabels.append(Label(adminshower, text=user))  # you don't have to grid after, but you need to if you want to reference the labels again.
adminlabels[k].grid(row=i, column=0)
cmd=partial(moreinfo, user)  # It is important you create the command before or use partial() from functools library
adminbuttons.append(Button(adminshower, text=user, command=cmd))
adminbuttons[k].grid(row=i, column=15)
k+=1

def moreinfo(user):
global admislist
moreinfoshower = tk.Toplevel()
moreinfoshower.geometry("300x200")
moreinfoshower.title(user)
name = accounts[user].get("name")
email = accounts[user].get("email")
user = accounts[user].get("username")
if name == "N/A" and email == "no":
status = "status: Account not configured"
pass
elif name != "N/A" and email == "no" or email !="no" and name == "N/A":
status = "status: Account is partially configured"
pass
else:
status = "status: Acount is fully configured"
pass
nameLabel = Label(moreinfoshower, text=status).grid(row=0, column=0) 
user = "username: "+user
usernameLabel = Label(moreinfoshower, text=user).grid(row=1, column=0)
if name == "N/A":
name = "Sorry the admin didn't add their name yet, Sorry"
name = "name: "+name
nameLabel = Label(moreinfoshower, text=name).grid(row=2, column=0) 
if email =="no":
email = "The admin didn't add their email.n Sorry for the unconvenience"
pass
email = "email: "+email
emailLabel = Label(moreinfoshower, text=email).grid(row=3, column=0)

最新更新