【发布时间】:2019-11-20 16:30:43
【问题描述】:
我正在尝试创建一个程序,允许用户选择任意数量的复选框并点击按钮以从这些复选框返回随机结果。由于我的列表基于 Smash bros Ultimate 的名单,我试图避免创建 70 多个变量只是为了放置复选框。但是,我无法弄清楚如何迭代它。在我弄清楚之前,为行设置的各种值只是占位符。我还想在顶部有一个重置按钮,允许用户自动取消选中每个框。这段代码是我到目前为止所拥有的。任何帮助将不胜感激。
#!/usr/bin/python3
from tkinter import *
window = Tk()
#window name and header
window.title("Custom Random SSBU")
lbl = Label(window, text="Select the fighters you would like to include:")
lbl.grid(column=1, row=0)
f = [] #check boxes
ft = open("Fighters.txt").readlines() #list of all the character names
fv=[0]*78 #list for tracking what boxes are checked
ff=[] #list to place final character strings
def reset():
for i in fv:
fv[i]=0
rst = Button(window, text="Reset", command=reset)
rst.grid(column=0, row=3)
for y in range (0,77):
f[y] = Checkbutton(window, text = ft[y], variable = fv[y])
f[y].grid(column=0, row=4+y)
def done():
for j in fv:
if fv[j] == 1:
ff.append(fv[j])
result = random.choice(ff)
r=Label(window, text=result)
d = Button(window, text="Done", command=done)
d.grid(column=0, row = 80)
window.mainloop()
【问题讨论】: