如何获取按钮名称作为python中的值

时间:2019-04-01 11:56:55

标签: python-3.x tkinter

使用tkinter我从列表中创建了按钮,而我想要的是单击按钮以添加添加HDD(按钮名称)的标签

如果我单击按钮/ dev / sda

以获取解释

我应该添加HDD / dev / sda

但是问题是我总是得到列表中的最后一个列表值

如果我单击名称为/ dev / sda的按钮,则会得到HDD / dev / sdb为

已添加

高级感谢

HDD=[/dev/sda,/dev/sdb,/dev/sdc]
top = tkinter.Tk()
top.geometry("500x500")

def hdd():
    hdd = tkinter.Tk()
    hdd.geometry("500x500")
    len(HDD)

    for i in range(0 , len(HDD)):
        i = HDD[i]

        def addtolist():
            hlist =[]
            hlist.append(i)

            lable = Label(hdd, text="HDD {} is added to the 
            zpool".format(i))
            lable.pack()

        print(i)
        bb = Button(hdd, text=str(i), command=addtolist)
        bb.grid(row=1, column=1)
        bb.pack()


e = Button(top, text = "HDD", command = hdd)
e.grid(row=0, column=5)

1 个答案:

答案 0 :(得分:0)

我不确定“我从列表中创建了按钮”是什么意思

from tkinter import *
top = Tk()
top.geometry("500x500")

button_names = ["dev/sda", "dev/sdb"]
button_list = [] # for later needs

#my understanding of creating buttons from a list!?
for i in button_names:
    button = Button(top, text=i, command=lambda x=i:lable_name(x))
    button.pack()
    button_list.append(button) 

#Labels are packed bellow because I don't know where you want to pack it    
def lable_name(name):
    label = Label(top, text="HDD {} is added to the zpool".format(name))
    label.pack()

top.mainloop()   

同样奇怪的是那行* hdd = tkinter.Tk()“,然后您尝试在其上使用len()。