python - 如何检查 Checkbutton 的值?
问题描述
我有几个复选框 - 使用 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()
解决方案
不是很清楚你想要什么,也许这段代码可以帮助
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()
推荐阅读
- sql - 遇到异常时插入的plsql
- qt - 在 Spyder:我在哪里可以找到 Qt 和 PyQt 版本?
- javascript - 需要帮助让 JSON.parse() 工作(Discord Bot)
- javascript - JavaScript - 为什么使用 RegEx 进行拆分会产生空白数组?
- c# - 常规生命周期范围内的 Autofac IAsyncDisposable 支持
- sql - 遇到除以零错误。当联合代码
- ios - NSNumberFormatter 没有按预期工作
- gtk3 - 如何获取已知GDK窗口所属的WNCK窗口?
- java - 生菜使用带有非字符串、字符串编解码器的 RedisAsyncCommands
- javascript - 有没有办法使用 chess.js 启用促销不足?