【问题标题】:How do i import these functions? Attribute Error我如何导入这些功能?属性错误
【发布时间】:2020-04-17 22:36:56
【问题描述】:

我刚开始学习 python 并使用 python 和 tkinter 创建了一个简单的计算器 GUI。按钮可以工作,但由于某种原因,当我尝试将函数移动到另一个文件并导入它们时,它不起作用并且我得到一个 AttributeError。

主文件:

from tkinter import *
from functions_calc_advanced import *

if _name_ == "_main_":

    gui = Tk()
    gui.configure(background="white")
    gui.title("LABINF Calculator WSS2018")
    gui.geometry("400x200")

    equation = StringVar()

    expression_field = Entry(gui, textvariable=equation)
    expression_field.grid(columnspan=4, ipadx=10)
    equation.set('enter your expression')

窗口打开,但当我按下任何按钮时出现错误。此按钮 1 只是一个示例,所有其他按钮的结构都相同

    button1 = Button(gui, text=' 1 ',command=lambda: press(1), height=1, width=7)
    button1.grid(row=4, column=0)

    buttonauf = Button(gui, text=' ( ', fg='black', bg='grey',command=lambda: press('('), height=1, width=7)
    buttonauf.grid(row=5, column=0)

    buttonzu = Button(gui, text=' ) ', fg='black', bg='grey',command=lambda: press(')'), height=1, width=7)
    buttonzu.grid(row=6, column=0)

    buttondot = Button(gui, text=' . ', fg='black', bg='grey',command=lambda: press('.'), height=1, width=7)
    buttondot.grid(row=5, column=2)

    buttonquit = Button(gui, text="Quit", command = gui.destroy, bg = 'black', fg = "white")
    buttonquit.grid(row = 0, column = 4)

计算按钮

    plus = Button(gui, text=' + ', fg='orange', bg='white', command=lambda: press("+"), height=1, width=2)
    plus.grid(row=2, column=3)

    minus = Button(gui, text=' - ', fg='orange', bg='white', command=lambda: press("-"), height=1, width=2)
    minus.grid(row=3, column=3)

    mult = Button(gui, text=' * ', fg='orange', bg='white', command=lambda: press("*"), height=1, width=2)
    mult.grid(row=4, column=3)

    div = Button(gui, text=' / ', fg='orange', bg='white', command=lambda: press("/"), height=1, width=2)
    div.grid(row=5, column=3)

    equal = Button(gui, text=' = ', fg='white', bg='orange', command=equalpress, height=1, width=7)
    equal.grid(row=6, column=3)

    clear = Button(gui, text='C', fg='white', bg='black', command=clear, height=1, width=7)
    clear.grid(row=0, column=3)

    gui.mainloop()

我尝试从中导入的函数文件:

expression = ""
equation = ""
def press(num):
    global expression
    expression = expression + str(num)
    equation.set(expression)

def equalpress():
    try:
        global expression
        total = str(eval(expression))
        equation.set(total)
        expression = ""

    except: 
        equation.set(" error ")
        expression = ""

def clear():
    global expression
    expression = ""
    equation.set("")

【问题讨论】:

  • 请同时显示您遇到的错误的痕迹以及行号等

标签: python user-interface tkinter calculator


【解决方案1】:

字符串没有set 属性,因此您应该将尝试导入的函数文件更改为此代码:

expression = ""
equation = ""

def press(num):
    global expression, equation
    expression = expression + str(num)
    equation = expression

def equalpress():
    global expression, equation
    try:
        total = str(eval(expression))
        equation = total
        expression = ""

    except: 
        equation = " error "
        expression = ""

def clear():
    global expression, equation
    expression = ""
    equation = ""

您应该将equation.set 更改为equation = "...",因为str 类型没有set 属性

【讨论】:

  • 感谢您的回复!我更改了我的函数文件,但我不再收到错误代码了!
猜你喜欢
  • 2022-11-19
  • 2021-09-20
  • 1970-01-01
  • 2019-10-20
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
  • 2022-01-06
相关资源
最近更新 更多