使用 python gui tkinter 的简单计算器
Simple calculator using python gui tkinter
我正在尝试制作一个简单的计算器,使用 Python 2.7.10 和 GUI 给出平方和平方根,但它不起作用我无法弄清楚问题是什么。我收到此错误:
> Exception in Tkinter callback Traceback (most recent call last): File
> "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in call return
> self.func(*args) File "C:/Users/Ali/Desktop/simplecalc.py", line 5, in
> do_sqrt root = x**0.5 TypeError: unsupported operand type(s) for ** or
> pow(): 'str' and 'float'
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()
第 x = number_input.get()
行将 return 一个字符串,但您正试图将其用作数字。请改用行 x = float(number_input.get())
。如果您包括错误打印输出,这将很清楚:
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'
此外,最好避免使用全局变量,但那是另一回事了。
您正在从条目中读取字符串并尝试执行一些数学运算。
而且您没有使用根变量和平方变量。
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()
我正在尝试制作一个简单的计算器,使用 Python 2.7.10 和 GUI 给出平方和平方根,但它不起作用我无法弄清楚问题是什么。我收到此错误:
> Exception in Tkinter callback Traceback (most recent call last): File > "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in call return > self.func(*args) File "C:/Users/Ali/Desktop/simplecalc.py", line 5, in > do_sqrt root = x**0.5 TypeError: unsupported operand type(s) for ** or > pow(): 'str' and 'float'
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()
第 x = number_input.get()
行将 return 一个字符串,但您正试图将其用作数字。请改用行 x = float(number_input.get())
。如果您包括错误打印输出,这将很清楚:
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'
此外,最好避免使用全局变量,但那是另一回事了。
您正在从条目中读取字符串并尝试执行一些数学运算。 而且您没有使用根变量和平方变量。
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()