【发布时间】:2019-12-12 09:16:52
【问题描述】:
我使用 for 循环在 tkinter 应用程序中创建了一组 (6) checkbuttons。到目前为止,我刚刚创建并布置了它们,但什么也没做。我希望他们做的是告诉另一个函数如何工作,具体取决于单击了哪个 checkbutton,但是当我尝试访问 checkbuttons 时,我收到了问题底部发布的错误。
我尝试将所有按钮制作为单独的代码行,但显然这是很多重复的代码,所以我使用 for 循环制作它们并将它们存储在嵌套的 dict 中,如下所示:
for i in self.atts:
self.att_buttons[i] = {}
self.att_buttons[i]["checkbutton"] = tk.Checkbutton(
self.check_frame, text=i, font=("Courier", 15),
onvalue = 1, offvalue = 0,
).pack(side=tk.LEFT)
我不确定这是否正确,但我是新手,我正在尽力而为。
我有一个roll() 函数,我想要的是检查按钮来修改该函数的结果,所以我尝试的是
def roll(self):
"""Roll dice, add modifer and print a formatted result to the UI"""
value = random.randint(1, 6)
if self.att_buttons["Str"]["checkbutton"].get() == 1:
result = self.character.attributes["Strength"]["checkbutton].get()
self.label_var.set(f"result: {value} + {result} ")
File "main_page.py", line 149, in roll
if self.att_buttons["Str"]["checkbutton"].get() == 1:
AttributeError: 'NoneType' object has no attribute 'get'
现在我假设这是因为我错误地调用了嵌套的 dict,但我尝试移动我的代码并尝试不同的点点滴滴,但我一直收到同样的错误。
更新
根据下面雨果的回答,我将 for 循环编辑为
for i in self.atts:
self.att_buttons[i] = {}
self.att_buttons[i]["checkbutton"] = tk.Checkbutton(
self.check_frame, text=i, font=("Courier", 15),
variable = tk.BooleanVar()#this is the change
)
self.att_buttons[i]["checkbutton"].pack(side=tk.LEFT)`
如何调用 variable 来获取 roll() 函数中的特定检查按钮?
【问题讨论】:
-
pack()方法返回None,并且您将该值分配给self.att_buttons[i]["checkbutton"]。 -
谢谢,我已经编辑了循环以分别打包每个按钮。