Python GUI科学计算器报错:'str'对象无'sqrt'属性求助
问题描述
开发科学计算器时,输入数字后点击平方根按钮,程序崩溃并抛出错误:Exception thrown 'str' object has no attribute 'sqrt'。通过调试确认num_1和f_num均为float类型且已正确赋值(例如256),尝试过强制变量类型转换、调整函数初始化方式等方法,均未解决问题。
测试代码如下:
import math from tkinter import * def sqrt(first_number): global math global f_num math="sqrt" f_num=float(first_number) def button_equal(second_number): num_1=f_num if math == "sqrt": Panel.delete(0, END) Panel.insert(0, math.sqrt(num_1)) def button_click(number): # Panel.delete(0, END) Panel.insert(0, number) root = Tk() root.title("scientific calculator") root.resizable(width=False, height = False) root.geometry("400x492+460+40") myButton = Button(root, text="1",padx=3) MainFrame = Frame( root, bd=20, pady=2, relief = RIDGE).grid() Panel = Entry(root, font=("default", 20), bd=2, justify="right") Panel.place(height=100, width=327, x=3,y=25) # The width of the panel is the value of the x axis on root.geometry - 3px on each side for margin Panel.insert(0, "0") B17 = Button(root, font=("default", 15), fg="orange", bg="#696969", text="1", command=lambda: button_click(1)) B17.place(width=75, height=50, x=8, y=330) B18 = Button(root, font=("default", 15), fg="orange", bg="#696969", text="2", command=lambda: button_click(2)) B18.place(width=75, height=50, x=87, y=330) B19 = Button(root, font=("default", 15), fg="orange", bg="#696969", text="3", command=lambda: button_click(3)) B19.place(width=75, height=50, x=167, y=330) B6 = Button(root, font=("default", 15), fg="orange", bg="#696969", text="x^", command=lambda: sqrt(Panel.get())) B6.place(width=75, height=50, x=87, y=180) B24 = Button(root, font=("default", 15), fg="orange", bg="#696969", text="=", command=lambda: button_equal(Panel.get())) B24.place(width=75, height=50, x=245, y=380) calc = Frame( MainFrame, bd=20, pady=2, relief = RIDGE).grid() root.mainloop()
问题修复
核心原因
你在sqrt函数中声明了global math,并将其赋值为字符串"sqrt",直接覆盖了导入的math模块。后续调用math.sqrt(num_1)时,math已经是字符串类型,自然不存在sqrt方法,导致报错。
修复方案
将记录当前操作的全局变量名改为不与math模块冲突的名称(比如current_operation),并调整所有相关引用:
修复后的代码:
import math from tkinter import * # 用current_operation代替math作为全局操作变量 current_operation = "" f_num = 0.0 def sqrt(first_number): global current_operation global f_num current_operation = "sqrt" f_num = float(first_number) def button_equal(second_number): num_1 = f_num if current_operation == "sqrt": Panel.delete(0, END) # 这里调用的是原本的math模块的sqrt方法 Panel.insert(0, math.sqrt(num_1)) def button_click(number): # Panel.delete(0, END) Panel.insert(0, number) root = Tk() root.title("scientific calculator") root.resizable(width=False, height = False) root.geometry("400x492+460+40") myButton = Button(root, text="1",padx=3) MainFrame = Frame( root, bd=20, pady=2, relief = RIDGE).grid() Panel = Entry(root, font=("default", 20), bd=2, justify="right") Panel.place(height=100, width=327, x=3,y=25) Panel.insert(0, "0") B17 = Button(root, font=("default", 15), fg="orange", bg="#696969", text="1", command=lambda: button_click(1)) B17.place(width=75, height=50, x=8, y=330) B18 = Button(root, font=("default", 15), fg="orange", bg="#696969", text="2", command=lambda: button_click(2)) B18.place(width=75, height=50, x=87, y=330) B19 = Button(root, font=("default", 15), fg="orange", bg="#696969", text="3", command=lambda: button_click(3)) B19.place(width=75, height=50, x=167, y=330) B6 = Button(root, font=("default", 15), fg="orange", bg="#696969", text="√", command=lambda: sqrt(Panel.get())) B6.place(width=75, height=50, x=87, y=180) B24 = Button(root, font=("default", 15), fg="orange", bg="#696969", text="=", command=lambda: button_equal(Panel.get())) B24.place(width=75, height=50, x=245, y=380) calc = Frame( MainFrame, bd=20, pady=2, relief = RIDGE).grid() root.mainloop()
额外优化:把按钮文本从x^改成√更符合平方根的语义,你也可以根据需求改回原文本。
内容的提问来源于stack exchange,提问作者Ippo
相关产品推荐
相关产品推荐

