Feb-19-2025, 04:04 PM
I am generating buttons with a loop in a py file with the code below. The TileLayout class generates a StackLayout filled with buttons with an on_press callback function.
PY FILE-------------------------------
The accompaning kv file follows.
KV FILE------------------------------------
When executed in pycharm, I get this error messsage:
PY FILE-------------------------------
from kivy.uix.stacklayout import StackLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from MathCrunchPkg import PackArray
from kivy.app import App
from kivy.config import Config
Config.set('graphics', 'resizable', True)
NUM_COLS = 15
NUM_ROWS = 10
TILE_ARRAY = []
TILE_ARRAY = PackArray.fill_arr(NUM_ROWS, NUM_COLS)
class TileMain(BoxLayout):
def on_enter(self):
print("ENTERED")
def tile_select(self):
print("SELECTED")
class TileLayout(StackLayout): #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
def __init__(self, **kwargs):
super().__init__(**kwargs)
for j in range(0,NUM_ROWS):
for i in range(0,NUM_COLS):
bt = Button()
bt.text=str(TILE_ARRAY[j][i][2])
bt.size_hint=(1/NUM_COLS,1.81/NUM_ROWS)
bt.background_color = 1,0,0,.6
bt.on_press = TileMain.tile_select(self) #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
bt.font_size = 42
self.add_widget(bt)
class MathCrushApp(App):
pass
if __name__ == "__main__":
MathCrushApp().run()-------------------------------------------The accompaning kv file follows.
KV FILE------------------------------------
TileMain: # this is the root widget, use this to set the background
orientation: "vertical"
canvas.before:
Color:
rgba: 1,1,1,1
Rectangle:
source: "gameboard.png"
size: self.size
pos: self.pos
TileLayout: #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@:
FloatLayout:
orientation: "horizontal"
Label:
text: "Attempts"
size_hint: 0.1,120/640
pos: "43dp", "0dp"
color: 0,0,0,1
font_size: 40
background_color: (1, 1, 1, 0)
canvas.before:
Color:
rgba: self.background_color
Rectangle:
size: self.size
pos: self.pos------------------------------------------------------------------------------When executed in pycharm, I get this error messsage:
Error: File "C:\Users\eanderso\PycharmProjects\PythonProject2\.venv\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down
self.dispatch('on_press')
File "kivy\\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
TypeError: 'NoneType' object is not callable The visual output (buttons, images, etc.) is as expected including active inserted buttons but the inserted buttons (from py file) do not call the callback function, but rather generate the error message. All my attempts to correct this have failed. Any suggestions or help would be appreciated.
