class DropdownList(object):
def __init__(self, master):
self.master = master
self.mainFrame = Frame(self.master, padx=1, pady=1) # if defined here, when the code reaches the 'Label' function, the 'object has no attribute mainFrame' error appears
self.mainFrame.pack()
def heightQuestion(self, rowNumber, columnNumber):
self.heightEntry = ''
# self.mainFrame = Frame(self.master, padx = 1, pady = 1) - if defined here, the attribute can be used in all other methods as well
# self.mainFrame.pack()
Label(self.mainFrame, text = 'What is your height?', font=('Roboto', 20)).grid(row = rowNumber, column = columnNumber)
Entry(self.mainFrame, textvariable = self.heightEntry, bd = 5, font = ('Roboto', 15)).grid(row = rowNumber+1, column = columnNumber)
如果我在构造函数中定义属性,则会收到“对象没有属性”错误。但是,如果我用其他方法(例如“ heightQuestion”)定义它,则可以正常工作。我希望在构造函数中定义属性。
错误: Label(self.mainFrame,text ='您的身高是多少?',font =('Roboto',20))。grid(row = rowNumber,column = columnNumber) getattr 中的文件“ C:\ Users \ Marius \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ tkinter__init __。py”,第2095行 返回getattr(self.tk,attr) AttributeError:“ _ tkinter.tkapp”对象没有属性“ mainFrame”