1

I'm making a travel/tourism app using Kivy as my project and I need to pass some value from a widget to another screen that'll display information of a location.

I am able to share information between different screens using this Kivy - Screen Manager - Accessing attribute in other class method, however this doesn't seem to work when I try to pass variables from a widget that isn't directly a screen.


I have a chatting interface created using RecycleView that has a button to "see more" information on the current location we're talking about, but I am unable to pass values from this Row widget to the LocationInfo widget/screen.

Here is the relevant code

Relevant Python code

class TourismRoot(ScreenManager):
    def open_location_info_page(self, locationname):
        self.current = 'LocationInfoPage'

class LocationInfo(Screen):
    cityname = StringProperty()
    def test_print(self):
        print(self.cityname)

Relevant KV code

TourismRoot:
    id: screen_manager
    FirstTimeRun:
        id: firstrunid
        name: 'FirstRun'
        manager: screen_manager

    FirstTimeRun2:
        id: firstrun2id
        name: 'FirstRun_2'
        manager: screen_manager
        nickname: firstrunid.entered_nickname #taking value from another screen, works

    LoginScreen:
        id: loginscreenid
        name: 'Login'
        manager: screen_manager

    ChatBotScreen:
        id: chatbotid
        name: 'ChatBot'
        manager: screen_manager
        nickname: firstrunid.entered_nickname

    LocationInfo:
        id: locationinfo
        name: 'LocationInfoPage'
        manager: screen_manager
        cityname: chatbotid.ids.lb_chat_row #gives error

<Row>:
    BoxLayout:
        Label:
            id: lb_chat_row
            text: 'stuff'
    GridLayout:
        Button:
            id: bt_chat_row
            text: root.button_text
            font_size: "18dp"
            text_size: self.size
            on_release: app.root.open_location_info_page(root.ids.lb_chat_row.text)

<ChatBotScreen>
    ScrollView:
        size_hint_y: 85
        RecycleView:
            id: chatbase_list
            viewclass: 'Row'
            data: []
            RecycleBoxLayout:
                orientation: 'vertical'
                padding: "15dp", "45dp", "15dp", "15dp"
                default_size_hint: 1, None
                size_hint_y: None
                height: self.minimum_height

as you can see, the Row widget is actually a viewclass of a RecycleView and thus I cannot simply make it a screen and pass values (I tried, it messed up the layout).

The current code gives the following error:

 kivy.lang.builder.BuilderException: Parser: File "<inline>", line 47:
  ...
       45:        name: 'LocationInfoPage'
       46:        manager: screen_manager
  >>   47:        cityname: chatbotid.ids.lb_chat_row
       48:
       49:<Row>:
  ...
  BuilderException: Parser: File "<inline>", line 47:
  ...
       45:        name: 'LocationInfoPage'
       46:        manager: screen_manager
  >>   47:        cityname: chatbotid.ids.lb_chat_row
       48:
       49:<Row>:
  ...
  AttributeError: 'super' object has no attribute '__getattr__'

Any help is greatly appreciated~

RhinoFreak
  • 99
  • 7

1 Answers1

0

As an alternative to .kv language in these kind of cases, you can store your values in Cache for transferring between screens if needed. Then, you can update whatever you want dynamically.

# Store value in Cache    
Cache.register('mycache')
Cache.append('mycache', 'mykey', value)

# Get value from Cache
value = Cache.get('mycache', 'mykey')
M. Yasin
  • 66
  • 6
  • I read this and the Cache documentation a couple of times now but I'm really not sure how I can implement it in my case, especially since I'm using kv language for defining all widgets. Can you please expand upon how I can use this? Thank you so much. – RhinoFreak Mar 24 '18 at 05:45
  • @RhinoFreak Sorry, my post wasn't that helpful in your case. I've edited it, let me know if it doesn't work. – M. Yasin Mar 24 '18 at 14:02
  • @"M Yasin Y" I used the chatbotid.ids.lb_chat_row.text and I get the error saying '''super' object has no attribute '__getattr__'". Thank you for your help though! – RhinoFreak Mar 27 '18 at 09:32
  • @RhinoFreak Can you add another label into ScrollView on ChatBotScreen directly and try calling it? If it works, your problem is that label is not defined on given root. In that case, you might need to use one of the default view classes or place your label right into RecycleView if possible. Additionally, the reason may be that Kivy can't find your view class. As is stated on Kivy's website, "test when views cannot be found" is still on todo list. – M. Yasin Mar 27 '18 at 13:53