AttributeError: type object 'Player2' has no attribute 'x'
Говорит что нет атрибута x, а он есть. Не знаю скорее всего ошибка очень глупая, но я реально не могу понять в чём дело.
Вот ошибка:
Traceback (most recent call last):
File "C:\Users\Artyom\PycharmProjects\pythonProject4\main.py", line 51, in <module>
print(Player2.x)
^^^^^^^^^
AttributeError: type object 'Player2' has no attribute 'x'
А вот мой код:
from tkinter import *
game = Tk()
game.title('Simple multiplayer game, it was made that show how to create multiplayer or communication this server')
game.geometry('1000x600')
game.resizable(width=False, height=False)
canvas = Canvas(game, width=1000, height=600)
canvas.place(in_=game, x=0, y=0)
class Player1:
def __init__(self):
self.x = 250
self.y = 300
self.v = 0
self.photo = PhotoImage(file='knight.png')
def up(self, event):
self.v = -3
def down(self, event):
self.v = 3
def stop(self, event):
self.v = 0
class Player2:
def __init__(self):
self.x = 750
self.y = 300
self.v = 0
self.photo = PhotoImage(file='knight.png')
def up(self, event):
self.v = -3
def down(self, event):
self.v = 3
def stop(self, event):
self.v = 0
def start():
canvas.delete('all')
canvas.create_image(Player1.x, Player1.y, image=Player1.photo)
canvas.create_image(Player2.x, Player2.y, image=Player2.photo)
# start()
print(Player2.x)
game.mainloop()
Источник: Stack Overflow на русском