KIVY - I Can't place my Custom Widget in a Gridview cell

196 Views Asked by At

I want to create an animal survey app using KIVY/Python. As an 82 year old my programming has gone rusty and I am a newby to KIVY/Python. I have created a Custom Widget 'AniPic' for quick action selection. When I try to place an AniPic widget of a Cheetah into the GridLayout between Buttons B1 and B3 it does not go into the allotted cell space. My AniPic widgets seem to be stacked at the end of the GridLayout and the AniPic of the Cheetah does not appear between Buttons B1 and B3.

Questions:

  1. what am I missing/doing wrong with my 'AmiPic' widget? A KISS answer with code will be appreciated
  2. How to I set property at coding time for on_release action.

Thanks a million

My KV File:

WindowManager:
    TestWindow:


<ActButton@Button>:
    # make it a transparent button
    background_color: [0, 0, 0, 0]

    on_press:
        self.background_color = [0.8, 0.8, 1, 0.5]
    on_release:
        # make button transparent again
        self.background_color = [0, 0, 0, 0]

<CntButton@ActButton>:
    text:'CNT'

<RptButton@ActButton>:
    text:'RPT'

<CUSWID@Widget>:
    pos: 0, 0
    color: (1, 1, 1, 1)
    size: root.width, 120

<AniPic@CUSWID>
    labelText: 'None'
    imageSource: ''

    BoxLayout:
        orientation: 'horizontal'
        width: root.width
        pos: 0,0

        canvas.before:
            RoundedRectangle:
                pos: 5, 5
                # the next two lines determine the position of the rectangle for the image
                size: root.width-10,  root.height-10
                source: root.imageSource
                radius:[10]

        CntButton:
            id: _cnt
            # how do I set action for event below at coding time
            on_release: root.cnt_action(_label.text)

        Label:
            id: _label
            text: root.labelText
            width: root.width

        RptButton:
            id: _rpt
            on_press: root.rpt_action(_label.text)

<TestWindow>:
    GridLayout:
        cols: 1

        Button:
            text: 'B1'

        AniPic
            imageSource: 'images/cheetah_pic.png'
            labelText: 'CHEETAH'

        Button:
            text:'B3'

        AniPic:
            imageSource: 'images/lion_pic.jpg'
            labelText: 'LION'`enter code here`

My python file:

i

mport kivy
kivy.require('2.0.0')

from kivy.app import App
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen


Window.size = (300, 560)


class AniPic:
    labelText: ObjectProperty(None)

    def cnt_action(self, labelText):
        print(labelText)
        animal = labelText
        print(animal + "pin")

    def rpt_action(self, lebelText):
        print(lebelText)
        animal = lebelText
        print('map' + animal)


class TestWindow(Screen):
    pass

class WindowManager(ScreenManager):
    pass

animal : ''
kv = Builder.load_file("mytest.kv")

class SpotmapApp(App):
    def build(self):
        return kv

if __name__ == "__main__":
    SpotmapApp().run()
1

There are 1 best solutions below

1
John Anderson On

I believe the problem is that you are using Widget (as in CUSWID@Widget) as a container for other widgets. That can work, but you must take responsibility for positioning the child widgets, and things like size_hint and pos_hint will not work. I suggest that you use a Layout class as the base for your CUSWID. Try changing:

<CUSWID@Widget>:

to:

<CUSWID@RelativeLayout>:

in your kv file.