我已经写了这段代码,不知道是什么问题。它应该是一个弹出窗口,可以使用Python中的二次公式求解二次方程ax^2+bx+c=0
from tkinter import *
root = Tk()
a= Entry(root, width=50, bg="white",
fg="black",
borderwidth=3.5,)
a.pack()
a.get()
b= Entry(root, width=50, bg="white",
fg="black",
borderwidth=3.5,)
b.pack()
b.get()
c= Entry(root, width=50, bg="white",
fg="black",
borderwidth=3.5,)
c.pack()
c.get()
Cscore=c.get()
Bscore=b.get()
Ascore=a.get()
#ax^2 + bx + c = 0
import math
def slove(Ascore,Bscore,Cscore):
d = math.sqrt((Bscore**2)- 4*Ascore*Cscore)
x1 = (-Bscore - d) / (2 * Ascore)
x2 = (-Bscore + d) / (2 * Ascore)
return x1, x2
x1, x2 = slove (Ascore ,Bscore ,Cscore)
#print("x1")
#print(x1)
#print("x2")
#print(x2)
def myClick():
myLabel3 = Label(root,
text="Megoldás " + x1 + x2, borderwidth=15)
myLabel3.pack()
myButton = Button(root, text="solve",
padx=10, pady=5, command=myClick,
fg= "black" , bg= "white" )
myButton.pack()
root.mainloop()
这是我在终端上运行后得到的错误消息:
Traceback (most recent call last):
File "c:\matek\math_sqrtupdate.py", line 43, in <module>
x1, x2 = slove (Ascore ,Bscore ,Cscore)
File "c:\matek\math_sqrtupdate.py", line 37, in slove
d = math.sqrt((Bscore**2)- 4*Ascore*Cscore)
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'
以前,它不是'str'而是'Entry',我用Ascore,Bscore,Cscore
取消了它,但它不是'Entry'而是'str'。我不知道该怎么做
您的代码有很多问题,但是
TypeError
是由这组语句引起的:因为
Entry
小部件的内容是字符串,而不是执行计算所需的浮点值。您还在错误的时间执行get()
方法-在单击solve按钮之前不应该执行,因此我已经有效地将它们移动到了myClick()
回调函数中为了帮助纠正这个问题,我添加了一个名为
get_numeric_value()
的助手函数,对作为参数传递给它的Entry
进行转换除此之外,我还修复了其他几个问题,使其能够运行注意:如果你打算使用这种语言,我强烈建议你阅读并开始跟随PEP 8 - Style Guide for Python Code
您必须确保所有Ascore、Bscore和Cscore都是数字(而不是字符串)。看起来Bscore可能是一个字符串。下面是浮动与字符串的演示:
相关问题 更多 >
编程相关推荐