PYTHON:__ init__方法不创建属性?

时间:2013-12-23 19:57:15

标签: python class init

我正在使用Tkinter,我正在尝试为属于Main1类的主对象创建一个名为wordlist的属性。

这是Main1类:

class Main1(Instructions):
    def __init__(self, master, wordlist):
        super(Main1,self).__init__(master)
        self.wordlist = self.readwords()
        self.textbox.insert(0.0,self.wordlist)
    def create_wdgts(self):

        mainlbl = Label(self,text="Tänk på ett ord!")
        mainlbl.grid(row=0,column=2)

        self.textbox = Text(self, width = 50, height = 5, wrap = WORD)
        self.textbox.grid(column=2,row=1)

        self.backbttn = Button(self,text="Tillbaka")
        self.backbttn["command"] = self.back
        self.backbttn.grid(column=5,row=0)

        self.pointentry = Entry(self)
        self.pointentry.grid(column=2, row=2)
        self.pointlbl = Label(self,text = "Poäng:")
        self.pointlbl.grid(column = 1, row= 2)
        self.pointbttn = Button(self, text="skicka poäng")
        self.pointbttn.grid(row= 2, column = 3)
        self.pointbttn["command"]= self.pointhndlr()

        self.crrctlbl = Label(self, text = "Rätt ord:")
        self.crrctlbl.grid(column = 1, row = 3)
        self.crrctentry = Entry(self)
        self.crrctentry.grid(column = 2, row= 3)
        self.crrctbttn = Button(self, text="skicka rätt ord")
        self.crrctbttn.grid(row= 3, column = 3)

        self.yesbttn = Button(self, text="Ja")
        self.yesbttn.grid(row = 4, column=4)

        self.nobttn = Button(self, text = "Nej")
        self.nobttn.grid(row=4, column=5)

    def readwords(self):
        """Returns list with all words in words.txt"""
        file = codecs.open("words.txt","r","utf8")
        wordlist = []
        for word in file:
            wordlist.append(word.strip())
        return wordlist

    def guess(self):
        self.guesstemp = random.choice(wordlist)
        self.textbox.insert(0.0,"Ange poäng för ordet '"+guesstemp+"': ")

    def pointhndlr(self):
        pointtemp = self.pointentry.get()
        self.pointentry.delete(0)
        self.wordlist = remvwords(self.wordlist,self.guesstemp,self.pointtemp,self.guesslist,self.pointlist)

我希望我不需要发布更多的程序,因为这已经是很多代码了。无论如何,我收到一条错误消息,说我的Main1对象没有wordlist属性。为什么?我是在 init 方法中创建的!

感谢所有人的帮助。

Sahand

编辑:错误被追溯到最后一行,我尝试更改self.wordlist的值。 错误消息是:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/tkinter/__init__.py", line 1475, in __call__
    return self.func(*args)
  File "/Users/SahandZarrinkoub/Documents/graphics.py", line 294, in main1
    main1.guess()
  File "/Users/SahandZarrinkoub/Documents/graphics.py", line 364, in guess
    self.textbox.insert(0.0,"Ange poäng för ordet '"+guesstemp+"': ")
NameError: global name 'guesstemp' is not defined

2 个答案:

答案 0 :(得分:2)

这里的原因是:

super(Main1,self).__init__(master)

将依次称之为:

def create_wdgts(self):

反过来会这样做:

self.pointbttn["command"]= self.pointhndlr()

函数 self.pointhndlr分配给self.pointbttn["command"],而是调用self.pointhndlr,并将结果分配给{ {1}}。

解决方案:删除括号:

self.pointbttn["command"]

答案 1 :(得分:-2)

你调用super.init的方式是错误的。

您使用过:

super(Main1,self).__init__(master)

您应该使用:

super(Main1,self).__init__(self, master)

您调用它的方式,您正在创建的对象不会初始化为Instructions实例。而是将master对象重新初始化或重新转换为Instructions实例。