Andrew Ashton
Andrew Ashton

Reputation: 21

Python Kivy recycle view prints extra button

I'm attempting to implement a recycle view widget in my kivy program. Its working fine, responding to the on press event etc but when it draws it adds an extra row that does not fit the formatting of the others. Here is the relevant part of my code, there is a lot so I only included this but I'll post more if needed. Any help is appreciated. I like kivy but this is def the strangest widget I have used in it.

Output

Here is a picture

py file

class MessageBox(Popup):

    def popup_dismiss(self):
        self.dismiss()

class SelectableButton(RecycleDataViewBehavior, Button):
    index = None

    def refresh_view_attrs(self, rv, index, data):
    """ Catch and handle the view changes """
       self.index = index
       return super(SelectableButton, self).refresh_view_attrs(rv, index, data)

    def on_press(self):
        self.parent.selected_value = 'Selected: {}'.format(self.parent.btn_info[int(self.id)])

    def on_release(self):
        MessageBox().open()

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
    selected_value = StringProperty('')
    btn_info = ListProperty(["ButtonText" for x in range(0,2)])

class MainScreen(RecycleView):
    rv_layout = ObjectProperty(None)
    def __init__(self, **kwargs):
        super(MainScreen, self).__init__(**kwargs)
        self.data = [{'text': "Button" + str(x), 'id': str(x)} for x in range(0,2)]

kv file

<MessageBox>:
    title: 'Popup Message Box'
    size_hint: None, None
    size: 400, 400

    BoxLayout:
        orientation: 'vertical'
        #Label:
            #text: app.root.rv_layout.selected_value
        Button:
            size_hint: 1, 0.2
            text: 'OK'
            on_press:
                root.dismiss()

<SelectableButton>:
    orientation: 'horizontal'
    Button:
        size_hint_x: .10
        text: '+'
        #font_size: 50
        texture_size: self.width, self.height
        size: self.texture_size
    Button:
        size_hint_x: .90
        text: root.text
        #font_size: 50
        texture_size: self.width, self.height
        size: self.texture_size

<MainScreen>:
    pos_hint: {'x': 0, 'y': .11}
    size_hint: 1, .70
    viewclass: 'SelectableButton'
    SelectableRecycleBoxLayout:
        id: layout
        default_size_hint: 1, 1
        #size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'   

Upvotes: 2

Views: 370

Answers (1)

ikolim
ikolim

Reputation: 16031

Problem - Extra Buttons

In Python file, SelectableButton is a class of Button widget but in kv file, SelectableButton is a class of BoxLayout widget with two Buttons. Button widget does not has attribute, orientation but BoxLayout has this attribute. Due to the widget inheritance mismatch, Kivy used the definition from Python script.

As for visualization, I have added background color (red, and blue) to the two Buttons defined in the kv file. From the print screen below, the extra buttons are from the kv file.

py file

class SelectableButton(RecycleDataViewBehavior, Button):

kv file

<SelectableButton>:
    orientation: 'horizontal'
    Button:
        ...
    Button:
        ...

Solution

Either change the Python file or kv file. In the example, the kv file is changed.

kv file

<SelectableButton>:
    size_hint_x: .90
    size: self.texture_size

Output - Visualization of original Kivy App

Img01 - Original

Output - Fixed Kivy App

Img02 - No extra buttons

Upvotes: 1

Related Questions