【发布时间】:2021-02-09 22:29:34
【问题描述】:
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) _tkinter.TclError: 未知选项“-class”
我正在尝试制作一个求解平方根的计算器(但在 tkinter 中)。一些简单的东西。应该发生的是,您在输入框中输入一个数字,按下一个按钮,然后您会看到下面的答案。但是,每当我按下按钮获取答案时,我都会得到 tkinter.TclError。我已经尝试了一切,包括使功能不同,以不同的顺序包装,无论什么看起来合乎逻辑。没有任何效果。请帮忙!代码如下。
from tkinter import *
import tkinter.font as fnt
root = Tk()
root.geometry('500x400')
res = Label(root, text='The answer will go here.')
from math import sqrt
myfont = fnt.Font(font='Helvetica',size=50)
head = Label(text='The Square root Solver!',font=myfont)
head.pack()
txtbox = Entry(root)
txtbox.pack()
def print_ans():
def solve():
from math import sqrt
return sqrt(eval(txtbox.get()))
ans = solve()
res.configure(root, text=f'The square root of {txtbox.get()} is {ans}')
submit = Button(root, text='Submit', command=print_ans)
submit.pack()
res.pack()[enter image description here][1]
root.mainloop()
【问题讨论】:
-
为什么要将
root作为参数传递给res.configure()?(我假设这就是发生错误的地方 - 请始终包含完整的回溯消息。) -
ok,继续添加回溯消息。
-
感谢您的建议!原来,我真的不擅长 tkinter。