![]() |
Inserting Python Buttons into KV Files - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Inserting Python Buttons into KV Files (/thread-44026.html) |
Inserting Python Buttons into KV Files - edand19941 - Feb-19-2025 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------------------------------- 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: 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.
RE: Inserting Python Buttons into KV Files - deanhystad - Feb-19-2025 Please post the entire error message, including the full traceback. I think the problem is that you bind the button to a class instead of an instance to a class. TileMain cannot tile_select. You need to create an instance of TileMain and bind the instance.tile_select to your button. RE: Inserting Python Buttons into KV Files - menator01 - Feb-19-2025 Here is an example of creating kivy buttons https://python-forum.io/thread-42326-post-179292.html#pid179292 RE: Inserting Python Buttons into KV Files - buran - Feb-19-2025 bt.on_press = TileMain.tile_select(self)here you call tile_select() method of TileMain class and bind return value (which is None ) to bt.on_pres . And None is not callable.
|