gpt4 book ai didi

python - python计算器程序中的AttributeError或ImportError

转载 作者:太空宇宙 更新时间:2023-11-03 17:14:55 25 4
gpt4 key购买 nike

我正在用 python 编写一个计算器程序,但我不断收到错误消息AttributeError:“应用程序”对象没有属性“response_txt”

from tkinter import *




class Application(Frame):
""" GUI application calculator. """
def __init__(self, master):
""" Initialize the frame. """
super(Application, self).__init__(master)

# Adds the grid layout manager
self.grid()

# The result string
self.response_str = ""

#sets up the controls in the frame
self.create_widgets()

def create_widgets(self):
""" Create and sequence """

#Text Sequence
self.sequence_txt = Text(self, height = 1, width = 30, wrap=WORD)
self.sequence_txt.grid(row = 0, column = 0, columnspan = 2)

# Configure the Text to center
self.sequence_txt.tag_config("center_align", justify='center')

# Configure the Text to center
self.response_txt.tag_config("center_align", justify='center')

###buttons
# Button 1
Button(self,
bg='1',
command = self.one_btn_clicked,
height = 2
).grid(row = 2, column = 0, sticky = NSEW)

#buttons clicked
def one_btn_clicked(self):
"""This method is run when the one button gets clicked"""
#append a 1
self.response_str+="1"
#update text
self.response_txt.delete(0.0, END)
self.response_txt.insert(0.0, self.response_str, "center_align")
#add number
self.compare_sequences();
#main
root = Tk()
root.title("Calculator")
app = Application(root)
root.mainloop()

当我通过模块运行它时,它给了我这个错误:

AttributeError: 'Application' object has no attribute 'response_txt'

我尝试导入这样的子模块:

`import self.response_txt`

然后它给了我这个错误消息:

ImportError: No module named 'self'

我真的需要这个来工作,明天就要交作业了。任何想法都表示赞赏,我对编程非常陌生。我也知道该程序还没有真正完成,但在我进行任何其他步骤之前,我需要确保我在这里所做的事情首先能够发挥作用。谢谢。

最佳答案

您忘记初始化 self.response_txt。此外,“1”不是代码中 bg 的有效参数:

Button(self,
bg='1',
command = self.one_btn_clicked,
height = 2
).grid(row = 2, column = 0, sticky = NSEW)

更正的代码:

from tkinter import *




class Application(Frame):
""" GUI application calculator. """
def __init__(self, master):
""" Initialize the frame. """
super(Application, self).__init__(master)

# Adds the grid layout manager
self.grid()

# The result string
self.response_str = ""

#sets up the controls in the frame
self.create_widgets()

def create_widgets(self):
""" Create and sequence """

#Text Sequence
self.sequence_txt = Text(self, height = 1, width = 30, wrap=WORD)
self.sequence_txt.grid(row = 0, column = 0, columnspan = 2)
self.response_txt = Text(self, height = 1, width = 30, wrap=WORD)
self.response_txt.grid(row = 0, column = 0, columnspan = 2)
# Configure the Text to center
self.sequence_txt.tag_config("center_align", justify='center')

# Configure the Text to center
self.response_txt.tag_config("center_align", justify='center')

###buttons
# Button 1
Button(self,
bg='white',
command = self.one_btn_clicked,
height = 2
).grid(row = 2, column = 0, sticky = NSEW)

#buttons clicked
def one_btn_clicked(self):
"""This method is run when the one button gets clicked"""
#append a 1
self.response_str+="1"
#update text
self.response_txt.delete(0.0, END)
self.response_txt.insert(0.0, self.response_str, "center_align")
#add number
self.compare_sequences();
#main
root = Tk()
root.title("Calculator")
app = Application(root)
root.mainloop()

此外,您还没有创建compare_sequences函数。

关于python - python计算器程序中的AttributeError或ImportError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33728414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com