Question

I'm working with Tkinter and I'm trying to create an attribute called wordlist for a main object that belongs to the Main1 class.

This is the Main1 class:

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)

I hope I don't need to post more of the program as this is already a lot of code. Anyway, I get an error message saying that my Main1 object has no wordlist attribute. Why? I created it in the init method!

Grateful for all help.

Sahand

EDIT: The error is traced back to the last line, where I try to change the value of self.wordlist. The error message is:

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
Was it helpful?

Solution

The reason here is that this:

super(Main1,self).__init__(master)

will in turn call this:

def create_wdgts(self):

which will in turn do this:

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

This does not assign the function self.pointhndlr to self.pointbttn["command"], instead it calls self.pointhndlr, and assigns the result to self.pointbttn["command"].

The solution: remove the parenthesis:

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

OTHER TIPS

The way you call super.init is wrong.

You used:

super(Main1,self).__init__(master)

You should use:

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

The way you called it, the object you are creating is not initialized as an Instructions instance. Instead, the master object gets re-initialized or re-cast as an Instructions instance.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top