如何检查Checkbutton的值?

时间:2019-02-16 15:29:09

标签: python checkbox tkinter

我有几个复选框-使用dict和for循环创建。一切正常,直到我按下“检查”按钮。看来我不知道如何检查Checkbutton的当前值。

import random
from tkinter import *


def rolldice(dice):
    return random.randrange(1, dice, 1)


root = Tk()
root.wm_title("Dices")
dices = [4, 6, 8, 10, 12, 20]
check_box = {item: BooleanVar() for item in Dices}


def checkDices():
    if C == True:
        rolldice(item in Dices)
    else:
        print("end")


for item in Dices:
    C = Checkbutton(root, text=item, variable=check_box[item], anchor=W, height=1, width=40)
    C.pack()

B1 = Button(root, text="Check", command=checkDices)
B1.pack()


root.mainloop()

2 个答案:

答案 0 :(得分:0)

不清楚您想要什么,也许这段代码可以提供帮助

from tkinter import *
root = Tk()
root.wm_title("Dices")
Dices = [4, 6, 8, 10, 12, 20]
check_box = {it: BooleanVar() for it in Dices}

def checkDices():
    for item in check_box:
        print(item,check_box[item].get())

for item in Dices:
    C = Checkbutton(root, text=item, variable=check_box[item], anchor=W, height=1, width=40)
    C.pack()

B1 = Button(root, text="Check", command=checkDices)
B1.pack()

root.mainloop()

答案 1 :(得分:0)

import random
from tkinter import *


def rolldice(dice):
    return random.randrange(1, dice, 1)


def checkdices():
        for item in check_box:
            if check_box[item].get() == True:
                print("Dice", item, "K gave result -", rolldice(item))
            else:
                print("You didn't throw", item, "K dice")

root = Tk()
root.wm_title("Dices")
dices = [4, 6, 8, 10, 12, 20]
check_box = {item: BooleanVar() for item in dices}


for item in dices:
    C = Checkbutton(root, text=item, variable=check_box[item], anchor=W, height=1, width=40)
    C.pack()

B1 = Button(root, text="Check", command=checkdices)
B1.pack()

root.mainloop()