我试图弄清楚如何从用户单击提交按钮时选择的所有 Checkbuttons 中获取文本值。到目前为止,我能够获取已选择但无法获取的按钮的索引号文本值。例如,如果选中了第一个、第二个和第五个按钮,它将打印 1、2、5,而不是实际的文本值。Iv 引用了其他关于使用.cget()的帖子,但没有运气。我的下一个想法是使用字典将数字和文本值存储在一起,但我计划使列表更大的唯一问题。我在下面发布了代码和图片来帮助解释。有什么建议吗?
from tkinter import *
sick = []
def getChecked():
for i in range(len(sick)):
selected = ""
if sick[i].get() >= 1:
selected += str(i)
print(selected)
root = Tk()
root.geometry('850x750')
root.title("Registration Form")
for i in range(6):
option = IntVar()
option.set(0)
sick.append(option)
# Conditions checkbutton
label_6 = Label(root, text="Have you ever had ( Please check all that apply ) :", width=50, font=
("bold", 10))
label_6.place(x=35, y=330)
Checkbutton(root, command=getChecked, text="Anemia", variable=sick[0]).place(x=130, y=350)
Checkbutton(root, command=getChecked, text="Asthma", variable=sick[1]).place(x=270, y=350)
Checkbutton(root, command=getChecked, text="Arthritis", variable=sick[2]).place(x=410, y=350)
Checkbutton(root, command=getChecked, text="Cancer", variable=sick[3]).place(x=560, y=350)
Checkbutton(root, command=getChecked, text="Gout", variable=sick[4]).place(x=130, y=380)
Checkbutton(root, command=getChecked, text="Diabetes", variable=sick[5]).place(x=270, y=380)
# submit button
Button(root, text='Submit', command=getChecked, width=20, bg='brown', fg='white').place(x=180, y=600)
root.mainloop()