'Attributes not inherited via tkinter.button
I am writing the number of step-by-step participants within the framework of the classical one. In normal code in def move function I try to reset attributes from one button to unexpected but when I use get_info I get nothing. I've been sitting for 5 hours, I can not figure out how to fix it. I tried to remake all classes into 1, but even after that it didn't inherit the attributes I needed.
import random
import tkinter as tk
from PIL import Image, ImageTk
import threading
class Color(object):
EMPTY = 0
BLACK = 1
WHITE = 2
class Move:
xstart = -1
ystart = 0
xdest = 0
ydest = 0
class Button(tk.Button, object):
IMG = None
PlayStatus = None
Getstatus = 0
MovementPointsStart = 0
MovementPoints = 0
Heatl = 0
def __init__(self, color, master, x, y, *args, **kwargs):
self.color = color
super(Button, self).__init__(master, width=6, height=3, *args, **kwargs)
self.x = x
self.y = y
self.color = color
def __str__(self):
return f'Юнит {self.x} {self.y} {self.PlayStatus}'
def __repr__(self):
return f'Юнит {self.x} {self.y} {self.PlayStatus}'
def nextturn(self):
if self.MovementPointsStart > self.MovementPoints:
print(self.MovementPointsStart)
self.MovementPoints = self.MovementPointsStart
print('sosnul')
else:
pass
def get_info(self, Button, x, y):
info = 0
if self.color == Color.BLACK:
info = (type(self), self.PlayStatus, self.MovementPointsStart, str('/'), self.MovementPoints, 'Black')
else:
info = (type(self), self.PlayStatus, self.MovementPointsStart, str('/'), self.MovementPoints,'White')
return info
def get_moves(self, Button, turn):
print(turn)
i = 1
while i==1:
print('zestko')
if turn % 2 == 0 and self.color == Color.WHITE:
print('sosnul')
if self.MovementPoints > 0:
print('huiza')
if Button.get_color(Move.xstart, Move.ystart) == Color.WHITE or Button.get_color(Move.xdest, Move.ydest) == Color.EMPTY:
if Move.xdest < Move.xstart:
xend = Move.xstart - Move.xdest
else:
xend = Move.xdest - Move.xstart
if Move.ydest < Move.ystart:
yend = Move.ystart - Move.ydest
else:
yend = Move.ydest - Move.ystart
self.MovementPoints = self.MovementPoints - (xend + yend)
break
break
else:
print("У вас нет очков перемещения")
break
else:
if turn % 2 == 0:
print("Сейчас не ваш ход")
if turn % 2 == 1 and self.color == Color.BLACK:
print('sosnul')
if self.MovementPoints > 0:
print('huiza')
if Button.get_color(Move.xstart, Move.ystart) == Color.BLACK or Button.get_color(Move.xdest, Move.ydest) == Color.EMPTY:
if Move.xdest < Move.xstart:
xend = Move.xstart - Move.xdest
else:
xend = Move.xdest - Move.xstart
if Move.ydest < Move.ystart:
yend = Move.ystart - Move.ydest
else:
yend = Move.ydest - Move.ystart
self.MovementPoints = self.MovementPoints - (xend + yend)
break
else:
print("У вас нет очков перемещения")
break
else:
print("Сейчас не ваш ход")
break
class Game:
turn = 0
ROW = 10
COLUMNS = 10
win = tk.Tk()
win.title('Eve Offline')
win.geometry("900x560")
x=0
y=0
def __init__(self):
self.Button = []
for x in range(Game.ROW):
temp = []
for y in range(Game.COLUMNS):
btn = Button(Color.EMPTY, Game.win, bg='green', x=x, y=y, text='bebra')
temp.append(btn)
self.Button.append(temp)
def set_Button(self):
self.Button[1][2] = Button(Color.BLACK, Game.win, x=1, y=2, text='Suck')
self.Button[1][2].PlayStatus = 1
for x in range (Game.ROW):
for y in range (Game.COLUMNS):
self.Button[x][y].config(command=lambda button=self.Button[x][y]: self.get_click(button))
def create_window(self):
for x in range(Game.ROW):
for y in range(Game.COLUMNS):
btn = self.Button[x][y]
btn.grid(row=x, column=y)
Muvprikaz = tk.Button(bg='green', width=20, height=4, text='MOVE', activebackground='blue')
Muvprikaz.config(command=lambda button=self.Muvprikaz: self.Muvprikaz())
Muvprikaz.place(x=550, y=300)
NextturnB = tk.Button(bg='gray', width=20, height=4, text='Next Turn')
NextturnB.config(command=lambda button=self.nextturn(Game.turn): self.nextturn(button))
NextturnB.place(x=700, y=300)
def start(self):
self.set_Button()
self.create_window()
self.print_button()
self.win.mainloop()
def print_button(self):
for row_btn in self.Button:
print(row_btn)
def get_color(self, x, y):
return self.Button[y][x].color
def get_info(self, x, y):
return self.Button[y][x].get_info(self, x, y)
def move(self):
print(Move.xdest, Move.ydest)
print('sukablat')
print([Move.xdest],[Move.ydest])
self.Button[Move.xdest][Move.ydest].config(background='gray', text='SS')
self.Button[Move.xdest][Move.ydest].config(command=lambda button=self.Button[Move.xdest][Move.ydest]: self.get_click(button))
self.Button[Move.xdest][Move.ydest] = self.Button[Move.xdest][Move.ydest]
self.Button[Move.xstart][Move.ystart] = Button(Color.EMPTY, Game.win, bg='green', x=Move.xstart, y=Move.ystart, text='bebra')
for x in range (Game.ROW):
for y in range (Game.COLUMNS):
self.Button[x][y].config(command=lambda button=self.Button[x][y]: self.get_click(button))
self.create_window()
self.Button[Move.xstart][Move.ystart].Getstatus = 0
print('blatsuka')
self.Button[Move.xstart][Move.ystart].config(bg='green', text='bebra')
self.Update()
def nextturn(self, turn):
self.turn +=1
print(self.turn)
for x in range(Game.ROW):
for y in range(Game.COLUMNS):
if self.Button[x][y] == self.Button[x][y]:
self.Button[x][y].nextturn()
print("hui")
return turn
def get_click(self, clicked_button):
if Button.Getstatus == 0:
print(Button.Getstatus)
x = (clicked_button.x)
y = (clicked_button.y)
Button.Getstatus = Button.Getstatus+1
clicked_button.config(bg='red')
self.print_button()
print(self.get_info(x, y))
print(Button.Getstatus)
Move.xstart = x
Move.ystart = y
Button.Getstatus == 0
elif Button.Getstatus == 1:
Button.Getstatus = 0
print(Button.Getstatus)
clicked_button.config(bg='green')
def dest_click(self, clicked_button):
x = (clicked_button.x)
y = (clicked_button.y)
clicked_button.config(bg='blue')
Move.xdest = x
Move.ydest = y
print(Move.ydest, Move.xdest)
print('eblan')
self.Move()
def Move(self):
self.Button[Move.xstart][Move.ystart].get_moves(self, self.turn)
self.move()
def Muvprikaz(self):
if Move.xstart>-1:
self.print_button()
print(Move.xstart, Move.ystart)
for x in range (Game.ROW):
for y in range (Game.COLUMNS):
self.Button[x][y].config(command=lambda button=self.Button[x][y]: self.dest_click(button))
else:
pass
def Update(self):
for x in range (Game.ROW):
for y in range (Game.COLUMNS):
self.Button[x][y].config(command=lambda button=self.Button[x][y]: self.get_click(button))
Move.xstart = -1
Move.ystart = 0
Move.xdest = 0
Move.ydest = 0
g = 1
game = Game()
game.start()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
