samedi 12 mai 2018

How to remove the text from label when checkbutton is not being checked?

i'm working on this code.i want to make it display the text from the checkbutton on the label, and also remove the text when the checkbutton is not being checked.the first question is i can't remove it since the system responses

TypeError: list indices must be integers or slices, not str

the other question is i want to make a shuffle button which can shuffle the text in the label.i tried random.shuffle() but it seems doesn't work. thanks in advance!!

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()




Aucun commentaire:

Enregistrer un commentaire