我正在用python编写计算器程序,并且我不断收到错误消息
AttributeError: 'Application' object has no attribute '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'
我真的需要这个工作,明天到期的学校作业。任何想法都很受欢迎,我对编程很新。我也知道该程序并不是那么接近完成,但在我可以采取任何其他步骤之前,我需要确保我在这里所做的事情将首先起作用。谢谢。
答案 0 :(得分:1)
你忘记了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函数。