Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Inserting Buttons (created in PY file) ito KV file

23 views
Skip to first unread message

Edward Anderson

unread,
Mar 26, 2025, 1:30:23 PMMar 26
to Kivy users support
I am creating buttons (eventually in a loop) in a PY file in the TileStack claas below.

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.config import Config

Config.set('graphics', 'resizable', True)


class Main(GridLayout):
    pass

class TileStack(Widget): ##################################################
    def __init_subclass__(self, **kwargs):
        super().__init__(**kwargs)
        bt = Button(text="z")
        self.add_widget(bt)


class MainApp(App):
    def build(self):
    return Main()

if __name__ == "__main__":
       app = MainApp()
       app.run()

I then then want to insert these using the TileStack class name into the KY design file below.
Main:
    <TestLabel@Label>:
        font_size: 32
        text: "0"
        background_color: 1, 1, 1, 1
        background_normal: ""
        size_hint: [0.093,0.15]
        canvas.before:
            Color:
                rgba: 1, 1, 1, 1
            RoundedRectangle:
                size: self.size
                pos: self.pos

    <TestButton@Button>:
        font_size: 32
        background_color: 0, 0, 0, 0
        color: 0,0,0,1
        background_normal: ""
        canvas.before:
            Color:
                rgba: 1, 237/255, 89/255, 1
            Rectangle:
                size: self.size
                pos: self.pos


    <Main>:
        rows: 2
        cols: 1

        canvas.before:
            Rectangle:
                source: "graphics/gameboard.png"
                size: self.size
                pos: self.pos
    TileStack: ################################################
    FloatLayout:
        TestLabel:
            c
olor: 0,0,0,1
            pos_hint: {'x':0.056, 'y':0.011}
        TestLabel:
            color: 0,0,0,1
            pos_hint: {'x':0.456, 'y':0.011}
        TestButton:
            text: "Submit"
            size_hint: 0.090,0.047
            pos_hint: {'x':0.562, 'y':0.026}
        TestLabel:
            color: 0,0,0,1
            pos_hint: {'x':0.854, 'y':0.011}
I have two problems.
1. The FloatLayout is inserted into the GridLayout as if there are two buttons:  one blank one followed the one as built in the KV file.
2. The TileStack button does not appear at all.
I have reviewed many videos and visited many google searches but have had no success. Any suggestions as how to eliminate these problems is appreciated.

Edward Anderson

unread,
Mar 26, 2025, 3:15:56 PMMar 26
to Kivy users support
Thanks all. I found the answer in a previous discussion.

ELLIOT GARBUS

unread,
Mar 26, 2025, 7:37:19 PMMar 26
to kivy-...@googlegroups.com
It is not entirely clear to me what you are trying to achieve.  I've made some changes below, based on my best guesses.

A few comments:
  • Layouts are tools for sizing and positioning widgets.  Spend sometime learning how the layouts work.  Your use of size_hints and pos_hints are kind of anti-pattern.  You want the layouts to do this work for you.
  • In TileStack you are adding widgets to widgets.  You only want to add widgets to Layouts.  A Widget does not know how to size or position it's children.
  • You have instanced Main in your python code, and instanced Main in your kv code.  This is redundant and will cause problems.
  • The KV rules need to start in the far left column

In the code below, I replaced the FloatLayout with a BoxLayout to position the buttons along the bottom of the Window.  I have set the height of the BoxLayout, this will set the height of the children.  I also added some spacing, to create space between the children (the TestLabel and TestButton widgets.

I've assumed the TileStack should be a GridLayout of buttons.  I added a loop and created 20 buttons.  

In your kv code I changed TestButton, from being a Button, to adding ButtonBehavior to a Label.  You are not using the capabilities of the Button, so this seems like the better choice.  Read: Button Behavior — Kivy 2.3.1 documentation

Here is an image of the app:

Inline image

Python code:

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout


class Main(GridLayout):
pass


class TileStack(GridLayout): # use a layout to hold the buttons
def __init__(self, **kwargs):
super().__init__(**kwargs)
for i in range(1, 21):
bt = Button(text=f'{i}')
self.add_widget(bt)


class MainApp(App):
pass # you have declared the root widget in kv


if __name__ == "__main__":
app = MainApp()
app.run()
KV Code:

<TestLabel@Label>:  # Kivy rules must start in the far left col
font_size: 32
text: "0"
    canvas.before:
Color:
rgba: 1, 1, 1, 1
RoundedRectangle:
size: self.size
pos: self.pos

<TestButton@ButtonBehavior+Label>:
font_size: 32
# background_color: 0, 0, 0, 0
color: 0,0,0,1
# background_normal: ""
    canvas.before:
Color:
rgba: 1, 237/255, 89/255, 1
Rectangle:
size: self.size
pos: self.pos

<TileStack>:
cols: 5
rows: 4


<Main>:
rows: 2
cols: 1
canvas.before:
        Color:
rgb: .8, .8, .8 # I don't have your image so I used a color
Rectangle:
#source: "graphics/gameboard.png"
size: self.size
pos: self.pos



Main: # this is the root widget
TileStack:
BoxLayout:
spacing: dp(20)
size_hint_y: None
height: dp(48) # sets the height for the children
TestLabel:
color: 0,0,0,1
TestLabel:
color: 0,0,0,1
TestButton:
text: "Submit"
on_release: print('submit')
TestLabel:
color: 0,0,0,1
That was a lot of info.  I'm happy to answer any questions.

Good Luck!


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/kivy-users/f529a1d3-af9b-473a-a8bc-41837d84e64an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages