Python : AttributeError - python

I get an AttributeError I can't seem to work out.
I'm working with two classes.
The first class goes something like that.
class Partie:
def __init__(self):
# deleted lines
self.interface = Interface(jeu=self)
def evaluerProposition(self):
# computations
self.interface.afficherReponse()
Introducing second class (in a separate file).
class Interface:
def __init__(self, jeu):
self.jeu = jeu
self.root = tkinter.Tk()
# stuff
def onClick(self, event):
# talk
self.jeu.evaluerProposition()
def afficherReponse(self):
# stuff
I start the whole thing by
partie = Partie()
All manipulations on my widget work fine until some click event causes
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1442, in __call__
return self.func(*args)
File "C:\Users\Canard\Documents\My Dropbox\Python\AtelierPython\Mastermind\classeInterface.py", line 197, in clic
self.jeu.evaluerProposition()
File "C:\Users\Canard\Documents\My Dropbox\Python\AtelierPython\Mastermind\classeJeu.py", line 55, in evaluerProposition
self.interface.afficherReponse()
AttributeError: 'Partie' object has no attribute 'interface'
I typed in the interpretor
>>> dir(partie)
and got a long list in return with 'interface' among the attributes.
Also typed
>>> partie.interface
<classeInterface.Interface object at 0x02C39E50>
so the attribute seems to exist.
Following the advice in some former post, I checked the instance names do not coincide with module names.
I am confused.

Most likely, in some code that you're not showing us, you're doing something like this:
self.some_button = tkinter.Button(..., command=self.interface.onClick())
Note the trailing () on onClick(). This would cause the onClick method to be called at the time the button is created, which is probably before your constructor is done constructing the instance of the Partie class.

Related

Why is Label object not callable appearing

I am in the process of making an OOP Countdown GUI with the letters, numbers and conundrum games.
This is a snippet of my code so far.
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Main")
self.configure(bg=LIGHT_BLUE)
self.title=tk.Label(master=self,bg=LIGHT_BLUE,font=("Arial",12,"bold"),text="!Countdown Games!",pady=5,padx=5)
self.title.pack()
self.frame=tk.Frame(master=self,bg=LIGHT_BLUE)
self.LettersButton=tk.Button(master=self.frame,bg=LIGHT_BLUE,font=("Arial",12,"bold"),text="Letters",command=self.start_letters)
self.LettersButton.grid(row=0,column=0,sticky="NESW",pady=10,padx=2)
self.NumbersButton=tk.Button(master=self.frame,bg=LIGHT_BLUE,font=("Arial",12,"bold"),text="Numbers",command=self.start_numbers)
self.NumbersButton.grid(row=0,column=1,sticky="NESW",pady=10,padx=2)
self.LettersButton=tk.Button(master=self.frame,bg=LIGHT_BLUE,font=("Arial",12,"bold"),text="Conundrum",command=self.start_conundrum)
self.LettersButton.grid(row=0,column=2,sticky="NESW",pady=10,padx=2)
self.frame.pack()
self.mainloop()
def start_letters(self):
self.withdraw()
self.Letters=Letters()
def start_numbers(self):
self.withdraw()
self.Numbers=Numbers()
def start_conundrum(self):
self.withdraw()
self.Conundrum=Conundrum()
class Conundrum(tk.Toplevel):
def __init__(self):
super().__init__()
self.configure(bg=LIGHT_BLUE)
self.geometry("400x150")
self.title("Conundrum")
self.frameA=tk.Frame(master=self,bg=LIGHT_BLUE)
self.letterList=[]
self.answer=r.choice(ninewords)
self.anagram=anagram(self.answer)
for x in range(9):
self.letterList.append(ConundrumDisplay(self.frameA,x))
self.frameA.pack()
self.frameB=tk.Frame(master=self,bg=LIGHT_BLUE)
self.letterEntryList=[]
for y in range(9):
self.letterEntryList.append(ConundrumEntry(self.frameB,y))
self.frameB.pack()
self.timer=tk.Label(master=self,bg=LIGHT_BLUE,font=("Arial",12,"bold"),text="31")
self.update_timer()
self.timer.pack()
self.mainloop()
m.showinfo("Startgame","The game will start when you press OK")
for x in range(9):
self.letterlist[x].add_letter(x,self.anagram)
self.bind("<Key>",self.process_key)
self.bind("Return",self.process_guess)
self.bind("BackSpace",self.process_back)
The upper bit of code is a method from an App class which inherits from tk.Tk
The error is in the super()._ _ init _ _() line in the conundrum class after you press the Conundrum button in the main wn
This conundrum class is obviously the bit of code just below which inherits from tk.Toplevel.
I have used tk.Toplevel before but it hasn't shown the error before.
I have tried to reduce the amount of code that I am posting but if any more of it is necessary to figure out the error, then I can amend the question.
Error Message:
Traceback (most recent call last):
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
File "main.py", line 50, in start_conundrum
self.Conundrum=Conundrum()
File "main.py", line 55, in __init__
super().__init__()
File "/nix/store/2vm88xw7513h9pyjyafw32cps51b0ia1-python3-3.8.12/lib/python3.8/tkinter/__init__.py", line 2624, in __init__
self.title(root.title())
TypeError: 'Label' object is not callable
The error is telling you exactly what is wrong: you can't call a Label as if it was a function.
You are doing this: self.title(root.title()). root.title is an instance of Label but you are trying to call it as if it was a function. You get the same error if you do something like this:
foo = Label(...)
foo()
Since foo is a Label rather than a function, you can't call it.
If you want to call the title function of the root window, you should not name the label self.title. Name it something else so that you can use the title method of the base class.

Why it says that super() takes atleast 1 argument

class Member():
def __init__(self, MemberName, MemberID, SubcribtionPaid):
self.__MemberName = MemberName
self.__MemberID = MemberID
self.__SubcribtionPaid = False
def SetMemberName(self):
print(MemberName,self.__MemberName)
def SetMemberID (self):
print(MemberID,self.__MemberID)
def SetSubcriptionPaid(self):
print(SubcribtionPaid,self.__SubcribtionPaid)
class Jmember(Member):
def__init__(self,MemberName,MemberID,SubcribtionPaid,DateofJoining):
super().__init__(MemberName,MemberID,SubcribtionPaid)
self.__DateofJoining = DateofJoining
def SetDateofBirth(self):
print(DateofBirth,self.__DateofJoining)
NewMember = Jmember("Armeen","1245","True","12/3/2015")
NewMember.SetMemberName()
NewMember.SetMemberID()
NewMember.SetSubcriptionPaid()
NewMember.SetDateofJoining()
I basically copy pasted the code.I used pycharm. I still dont get how to use code block. I dont know what to do.
Sorry if i caused a problem.
I wrote a code in python,where one class inheritates the attributes of another class. When i run the code , error occurs on line 26 saying that super() takes atleast 1 argument .Even though i wrote the arguments. Did I make a mistake there. The code, the traceback
I want to see the output of the new member that includes member name, id , subcribtion paid and date of joining. But error is shown.
Updated my post to reflect your posted code:
You said that you are using PyCharm. pay attention to the red squiggly lines under sections as they are pointing out errors.
First error: you did not have a space between def and init
def__init__(self, MemberName, MemberID, SubcribtionPaid):
Second Error: found in your class function calls:
You do not have to call the class that is essentially what (self) is doing
def SetMemberName(self):
print(MemberName,self.__MemberName)
Third error: was found in your Traceback
You were trying to call a function that you forgot to create
Traceback (most recent call last):
File "/Users/sumbody/Projects/Python/MemberTest2/main.py", line 29, in <module>
NewMember.SetDateofJoining()
AttributeError: 'Jmember' object has no attribute 'SetDateofJoining'
Here is the working code:
class Member():
def __init__(self, MemberName, MemberID, SubcribtionPaid):
self.__MemberName = MemberName
self.__MemberID = MemberID
self.__SubcribtionPaid = False
def SetMemberName(self):
print(self.__MemberName)
def SetMemberID (self):
print(self.__MemberID)
def SetSubcriptionPaid(self):
print(self.__SubcribtionPaid)
class Jmember(Member):
def __init__(self,MemberName,MemberID,SubcribtionPaid,DateofJoining):
super().__init__(MemberName,MemberID,SubcribtionPaid)
self.__DateofJoining = DateofJoining
def SetDateofBirth(self):
print(self.__DateofJoining)
def SetDateofJoining(self):
print(self.__DateofJoining)
NewMember = Jmember("Armeen","1245","True","12/3/2015")
NewMember.SetMemberName()
NewMember.SetMemberID()
NewMember.SetSubcriptionPaid()
NewMember.SetDateofJoining()
Some advice, when learning to code and debug your own code, it is not a bad thing to take some code that runs and then break it. It sounds weird, but in doing so you will learn to recognize errors when seen in the editor and at runtime. Happy coding!

user defined class serialization and deserialization in python

I am very new to python : I want to serialize and deserialize my custom object in python. Please guide me on the same. I have a sample class :
import pickle
import json
class MyClass():
variable = "blah"
num = 10
def function(self):
print("this is a message inside the class.")
def get_variable():
return variable
def get_num():
return num
def main():
myObj = MyClass()
with open('/opt/infi/deeMyObj.txt', 'w') as output:
pickle.dump(myObj, output,pickle.HIGHEST_PROTOCOL)
with open('/opt/infi/deeMyObj.txt', 'r') as input:
myObjread = pickle.load(input)
print myObjread.get_variable()
print myObjread.get_num()
main()
I am getting following error :
Traceback (most recent call last):
File "sample.py", line 30, in
main()
File "sample.py", line 27, in main
print myObjread.get_variable()
TypeError: get_variable() takes no arguments (1 given)
Main intention is to read the object back.
To expand on jasonharper's comment, your get_variable and get_num methods aren't referring to the class's member variables. They should take the object as their first argument, e.g.
class MyClass:
...
def get_variable(self):
return self.variable
I think your serialization code is OK, but I might be wrong.
(Aside)
This is a bit off-topic, but another thing to note: when you define variables directly within the class block, they're defined on the class, not on objects of that class. That happens to work out in this case, since Python will look for a class-level variable of the same name if it can't find one on the object. However, if you store, say, a list in one of them and start modifying it, you'd end up sharing it between objects, which is probably not what you want. Instead you want to define them on in an __init__ method:
class MyClass:
def __init__(self):
self.variable = "blah"

Python Subclass Attribute Error

During a lecture today we began to do work with subclasses within Python. As an example, we were given code resembling a very basic social network which is as follows:
class socialNetwork:
class node:
def __init__(self, name, friendList):
self.name=name
self.friendList=friendList
def __init__(self):
self.nodeList=[]
def addPerson(self, name, friendList):
person=self.node(name,friendList)
self.nodeList.append(person)
s = socialNetwork()
s.addPerson("John",["Alice","Bob"])
s.addPerson("Alice",["John","Bob","Jeff"])
s.addPerson("Bob",["John","Alice","Jeff","Ken"])
s.addPerson("Jeff",["Alice","Bob","Barbra"])
s.addPerson("Ken",["Bob","Barbra"])
s.addPerson("Barbra",["Jeff","Ken"])
for person in s.nodeList:
print("name: ",person.name, "\n\t friends: ",person.friendList)
However, whenever I attempt to run this, I receive the following message:
Traceback (most recent call last):
** IDLE Internal Exception:
File "C:\Users\Mike\AppData\Local\Programs\Python\Python36-
32\lib\idlelib\run.py", line 460, in runcode
exec(code, self.locals)
File "C:/Users/Mike/AppData/Local/Programs/Python/Python36-32/run.py",
line 15, in <module>
s.addPerson("John",["Alice","Bob"])
AttributeError: 'socialNetwork' object has no attribute 'addPerson'
Simply put, I have no idea why I am encountering this error, especially after the professor ran the same code just fine. Am I missing something here, and if so could someone please point it out?
Your class doesn't have addPerson method, because your class is indented wrong way. It should look like this:
class socialNetwork:
class node:
def __init__(self, name, friendList):
self.name=name
self.friendList=friendList
def __init__(self):
self.nodeList=[]
def addPerson(self, name, friendList):
person=self.node(name,friendList)
self.nodeList.append(person)
Indentation does matter in python. A clue that something is wrong would be you having two __init__ methods at the same level.
You haven't defined any subclasses. Inheritance is specified in Python by putting the parent class(es) in parenthesis, e.g.:
class Node:
pass
class Leaf(Node):
# Leaf is a subclass of Node
pass
"Network" and "Node" don't really make sense to be subclasses, but one should be composed of the other.
What you've done is define a class socialNetwork with one attribute, a class called node. That's why you get an AttributeError, because there is no addPerson attribute in socialNetwork.
Firstly, node is not a subclass of socialNetwork but a class nested within the latter.
Secondly, socialNetwork actually has no attribute addPerson, but socialNetwork.node does.

How to use pyglet within a class

In all the tutorials on the web I've seen with Pyglet, it doesnt seems that any of them have used classes to contain the pyglet.window.Window instance. For example, most tutorials seem to go something like
import pyglet
game_window = pyglet.window.Window()
#game_window.event
def on_draw():
#dostuff
while __name__ == "__main__":
pyglet.app.run()
I'm having trouble restructuring this code into a class. My code which is intended to do so, is here:
import pyglet
from pyglet.gl import *
from Board import Board
class Frontend:
def __init__(self,xs, ys):
self.GameInstance = Board(xs,ys)
self.GameWindow = pyglet.window.Window(width=512, height=512,visible=False)
#GameWindow.event
def on_draw(self):
self.GameWindow.clear()
f = Frontend()
When I run this code, I get the following error:
Traceback (most recent call last):
File "C:/Users/PycharmProjects/Nothing/2048/Frontend.py", line 7, in <module>
class Frontend:
File "C:/Users/PycharmProjects/Nothing/2048/Frontend.py", line 13, in Frontend
#GameWindow.event
NameError: name 'GameWindow' is not defined
When I replace #GameWindow.event with #self.GameWindow.event in an attempt to resolve the NameError I get:
Traceback (most recent call last):
File "C:/Users/PycharmProjects/Nothing/2048/Frontend.py", line 7, in <module>
class Frontend:
File "C:/Users/PycharmProjects/Nothing/2048/Frontend.py", line 13, in Frontend
#self.GameWindow.event
NameError: name 'self' is not defined
Which i expect. However, I'm not sure why this code isnt working - can someone explain why and how to fix it?
You can inherit from Window.
This should work:
class Frontend(pyglet.window.Window):
def __init__(self, xs, ys):
self.GameInstance = Board(xs,ys)
super().__init__(width=512, height=512,visible=False)
def on_draw(self):
self.clear()
Your code is not working because you can not reference the instance of the Frontend class outside the methods. If you will have only one instance of the Frontend class you can do something like:
class Frontend:
window = pyglet.window.Window()
def __init__(self):
...
#window.event
def on_draw():
...
As I've commented on ragezor's answer, the pyglet docs recommend inheritance.
But another option may be to separate the event handling logic in its own class, as an EventDispatcher:
http://pyglet.org/doc-current/programming_guide/events.html#creating-your-own-event-dispatcher
Personally, if I knew would only have one Frontend instance, I would question the necessity of having a class. But that's a whole can of worms. Thought I'd give you another nice option, at least, you can't go wrong inheriting from Window though, especially if all your events are Window-related.
A third idea (fourth I guess since ragezor gave you two options):
class Frontend:
def __init__(self,xs, ys):
self.GameInstance = Board(xs,ys)
self.GameWindow = pyglet.window.Window(width=512, height=512,visible=False)
self.on_draw = self.GameWindow.event(self.on_draw)
def on_draw(self):
self.GameWindow.clear()
In other words, apply the #GameWindow.event decorator manually.
One last thing, don't use camelcase for attributes, it goes against PEP8 convention and confused me for a second while editing this code. Call it game_window instead.
My guess is that you need to do
#self.Gamewindow.event

Categories