我正在尝试制作一个简单的计算器,用Python2.7.10和GUI来提供平方根和平方根,但是它不起作用,我不知道问题出在哪里。我收到这个错误:
Tkinter回调跟踪(最近一次调用)中的异常: File >“C:\Python27 27\lib\tk\Tkinter.py”,第1536行,在call root > self.func(*args)文件"C:/Users/Ali/Desktop/simplecalc.py“中,第5行,in > do_sqrt root= x ** 0.5 TypeError:不支持的操作数和类型(S)用于**或> pow():'str‘和’do_sqrt‘
import Tkinter
import tkMessageBox
def do_sqrt():
root = x**0.5
tkMessageBox.showinfo("Square Root = ", x)
def do_square():
square = x**2
tkMessageBox.showinfo("Square = ", x)
main_window = Tkinter.Tk()
main_window.title("Simple Calc")
number_input = (Tkinter.Entry(main_window))
x = number_input.get()
button_sqrt = Tkinter.Button(main_window, text = "Square Root", command = do_sqrt)
button_sqrt.pack()
button_square = Tkinter.Button(main_window, text = "Square", command = do_square)
button_square.pack()
number_input.pack()
main_window.mainloop()
发布于 2016-01-29 22:14:26
您正在从条目中读取字符串,并尝试执行一些数学运算。而且您没有使用根和平方变量。
import Tkinter
import tkMessageBox
def do_sqrt():
root = float(number_input.get())**0.5
tkMessageBox.showinfo("Square Root = ", root)
def do_square():
square = float(number_input.get())**2
tkMessageBox.showinfo("Square = ", square)
main_window = Tkinter.Tk()
main_window.title("Simple Calc")
number_input = Tkinter.Entry(main_window)
button_sqrt = Tkinter.Button(main_window, text="Square Root", command=do_sqrt)
button_sqrt.pack()
button_square = Tkinter.Button(main_window, text="Square", command=do_square)
button_square.pack()
number_input.pack()
main_window.mainloop()
发布于 2016-01-29 21:49:10
行x = number_input.get()
将返回一个字符串,但您正在尝试使用它作为一个数字。使用行x = float(number_input.get())
代替。如果包含错误打印输出,这一点就很清楚了:
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'
此外,最好避免使用全局变量,但这是另一个问题。
https://stackoverflow.com/questions/35094583
复制相似问题