我正在尝试制作一个简单的计算器,通过使用带有GUI的Python 2.7.10来提供正方形和平方根但是它不起作用我无法弄清楚问题是什么。我收到此错误:
enum Gchord{G=1,B,D,}; int main(){ printf( "What interval is G in the G chord triad \nEnter 1 2 or 3\n" ); int note; scanf("%i",¬e); if (note = 1 ){ printf ("Yes G is %ist note in the G-chord\n",G )}; else( printf("no, wrong"); return(0): };
> 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'
答案 0 :(得分:1)
行x = number_input.get()
将返回一个字符串,但您尝试将其用作数字。请改用第x = float(number_input.get())
行。如果您包含错误打印输出,那就很清楚了:
TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'float'
此外,避免全局变量会好得多,但这是另一天的另一个问题。
答案 1 :(得分:1)
您正在从条目中读取字符串并尝试执行一些数学运算。 而且你没有使用根和方变量。
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()