如果没有检查checkbutton,如何从标签中删除文本?

时间:2018-05-12 17:03:20

标签: checkbox tkinter label

我正在处理这段代码。我想让它显示标签上的复选按钮中的文本,并在未检查复选键时删除文本。第一个问题是我可以'自系统响应后删除它

  

TypeError:list indices必须是整数或切片,而不是str

另一个问题是我想制作一个随机播放按钮,它可以随机播放标签中的文字。我试过random.shuffle()但它似乎不起作用。提前谢谢!!

import tkinter as tk
import random

window = tk.Tk()
checkbutton_frame = tk.Frame(window)
checkbutton_frame.grid(column=0, row=1)
contentvar = tk.StringVar()
label = tk.Label(window, textvariable=contentvar,
                 bg='white', font=('Arial', 10), width=20, height=20, wraplength=50)
label.grid(column=6, row=1, padx=20,
           pady=20, columnspan=2)

cb_list = ['ray', 'kevin', 'jacky']
cb_vars = []
checked = []  
check_list = 1

def display():
    for text, var in zip(cb_list, cb_vars):
        if var.get():
            checked.append(text)
            contentvar.set(list(set(checked)))
        else:
            for i in checked:
                del checked[i]

def shuffle():
    random.shuffle(checked)

for r, element in enumerate(cb_list):
    var = tk.BooleanVar(window, False)
    cb = tk.Checkbutton(checkbutton_frame, variable=var,
                        text=element, command=display)
    cb.grid(column=check_list, row=r, sticky='w')
    cb_vars.append(var)

shuffle_button = tk.Button(window, text='SHUFFLE', command=shuffle)
shuffle_button.grid(column=8, row=2)

window.mainloop()

0 个答案:

没有答案