0

Below is a code I modified from this link! with an addition of a screen widget and a defined viewclass of the recycleview. The notion is that when I press on a button in a recycleview item, a popup must appear with the button name on it or a data must be transferred to the popup widget. I have been able to print out the button name on the terminal but couldn't print it on the popup widget. The challenge is how to reference 'selected_value = StringProperty('')' with app.root.... method in the .kv. This has taken me days trying to figure it out as such I have couldn't continue. Please I need helping hands!!!

main.py

import kivy
kivy.require('1.10.1')
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout  
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager,Screen,FadeTransition,SlideTransition
from kivy.properties import *
from kivy.uix.behaviors import ButtonBehavior, FocusBehavior
from kivy.uix.recycleview.layout import LayoutSelectionBehavior
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.recycleboxlayout import RecycleBoxLayout
from kivy.uix.recycleview import RecycleView
from kivy.uix.popup import Popup
from kivy.core.window import Window
Window.size = (100 * 5, 90 * 8)

class ScreenOne(Screen):
    def go(self):
    self.manager.current = 'two'

class ScreenTwo(Screen):
    def __init__(self,**kwargs):
        super(ScreenTwo,self).__init__(**kwargs)

############## creating a popup MessageBox #####################################
class MessageBox(Popup):
    def popup_dismiss(self):
        self.dismiss()

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, RecycleBoxLayout):
    selected_value = StringProperty('') # How can I reference this property in my .kv file to appear on my Messagebox widget label
    info = ListProperty(['Button 1','Button 2','Button 3','Button 4','Button 5','Button 6','Button 7','Button 8', 'Button 9','Button 10','Button 11','Button 12','Button 13','Button 14','Button 15','Button 16','Button 17','Button 18','Button 19','Button 20'])

class ViewClass(RecycleDataViewBehavior, BoxLayout):
    id_num = StringProperty()
    button = StringProperty()
    index = None

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

    def on_press(self):
        self.parent.selected_value = '{}'.format(self.parent.info[int(self.id_num)])
        MessageBox().open()

    def on_release(self):
        pass

class RV(RecycleView):
    rv_layout = ObjectProperty(None)
    def __init__(self, **kwargs):
         super(RV, self).__init__(**kwargs)
         self.data = [{'id_num':str(x),'button': 'Button %s' % str(x)} for x in range(20)]

class MainApp(App):
    def build(self):
        sm = ScreenManager()
        sm.add_widget(ScreenOne(name="one"))
        sm.add_widget(ScreenTwo(name="two"))
        sm.current = 'one'
        return sm

if __name__ == '__main__':
    MainApp().run()

main.kv

#: kivy 1.10.1
#: import Label kivy.uix.button.Label
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import FallOutTransition kivy.uix.screenmanager.FallOutTransition

<ScreenOne>:
    name:'verify_no'
    BoxLayout:
        orientation: 'vertical'
        padding:100,100
        spacing: 50
        GridLayout:
            cols: 1
            Label:
                text: 'press to go to screen two'
                id: ti1
            Button:
                text: 'Go to >'
                on_press: root.go()

<MessageBox>:
    title: 'Order'
    size_hint: None, None
    size: 400, 400
    BoxLayout:
        orientation: 'vertical'
        spacing: 50
        GridLayout:
            cols: 1
            Label:
                text:'app.root.parent.rv_layout.selected_value:\nproduces Attribute error'
            Label:
                text:'app.root.parent.rv_layout.selected_value:\nalso produces NoneType error'
            Label:
                text:'app.root.children.rv_layout.selected_value:\ndid not work neither'
        BoxLayout:
             orientation:'horizontal'
             Button:
                size_hint: 1, 0.2
                text: 'ok'
                on_press:
                    root.dismiss()

 <MyButton@Button>:
    # Draw a background to indicate selection
    canvas.before:
        Color:
            rgba: (0.0, 0.9, 0.1, 0.3)
        Rectangle:
            pos: self.pos
            size: self.size

<ViewClass>:
    orientation:'vertical'
    BoxLayout:
        padding: 15
        Label:
            text: root.id_num
        MyButton:
            id:bt
            text: root.button
            background_color: 0,0,0,0
            on_press: root.on_press()

    Label:
        canvas.before:
            Color:
                rgba: (1,1,1,1)
            Rectangle:
                size: self.size
                pos: self.pos
        size_hint_y: None
        height: 1

<ScreenTwo>:
    name:'two'
    id: itm
    BoxLayout:
       id:b2
       padding:1,1
       spacing:1
       orientation: 'vertical'

       RV:
           rv_layout: layout
           scroll_type: ['bars', 'content']
           scroll_wheel_distance: dp(114)
           bar_width: dp(10)
           viewclass: 'ViewClass'
           SelectableRecycleBoxLayout:
               id: layout
               default_size: None, dp(56)
               default_size_hint: 1, None
               size_hint_y: None
               height: self.minimum_height
               orientation: 'vertical'
Anas
  • 125
  • 13

1 Answers1

0

I'm still stumbling through the whole Kivy 1.10.1 myself, but I think you're trying to reference something that doesn't exist yet. The same thread has a better (IMHO) example.

Try creating a function inside your RecycleView (RV) for the StringProperty() to reference.

class MessageBox(Popup):
    message = StringProperty()

def message(self, message)
    p = MessageBox()
    p.message = message
    p.open()

Then in your KV reference it inside your BoxLayout: with:

 Label:
        text: root.message