A more object orientated approach would be to recognize that a Tile is a Button, and you can create a specialized Button class, and use it as you would a Button, rather than creating a Utility class.
class TileStack(GridLayout): # use a layout to hold the buttons
TARGET = 0
N_ROWS = 10
N_COLS = 15
def __init__(self, **kwargs):
super().__init__(**kwargs)
TARGET = random.randint(0, 9) + random.randint(0, 9)
print(TARGET)
for i in range(0, self.N_ROWS):
for j in range(0, self.N_COLS):
rand_int1 = random.randint(100, 254) / 255
rand_int2 = random.randint(100, 254) / 255
rand_int3 = random.randint(100, 254) / 255
rand_int4 = random.randint(0, 9)
my_color = (rand_int1, rand_int2, rand_int3, 1)
my_id = str(i) + '-' + str(j) + '-' + str(rand_int4)
self.add_widget(Tile(my_id, my_color, rand_int4)) # instance a Tile and add to the Layout
class Tile(Button): # a Tile is a Button
def __init__(self, my_id, my_color, my_value, **kwargs):
super().__init__(**kwargs)
self.text = str(my_value)
self.font_size = 32
self.bold = True # use True not a string "true"
self.color = (0, 0, 0, 1)
self.background_normal = ""
self.background_color = my_color
self.tile_id = my_id
self.bind(on_press=self.selected)
def selected(self, obj):
print(f"selected {self.tile_id}") # self refers to this instance of the button