Как я могу проверить значение Checkbutton? - PullRequest
0 голосов
/ 16 февраля 2019

У меня есть пара флажков, созданных с использованием dict и for loop.Кажется, все работает нормально, пока я не нажму кнопку «проверить».Кажется, я не знаю, как проверить текущее значение 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 голосов
/ 16 февраля 2019
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()
0 голосов
/ 16 февраля 2019

Не очень понятно, что вы хотите, может быть, этот код может помочь

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()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...