Ben Sun Wai
Ben Sun Wai

Reputation: 21

Python/Kivy - Sending variables from one class to another

I'm not that new to Python so I have a basic understanding but I wouldn't say that I'm all that great either.

I've been having a tonne of problems with a brazilian jiujitsu app that I'm trying to make in Kivy. I am trying to get the text from a button that has been pressed in one window, and then use that text in a label for the next window that shows. The former window I have given the class name GuardWindow() and the latter window is called SweepSubTranPage().

I have managed to send the name of the button from my kivy file to the class GuardsWindow() easily enough, and the line self.guardlabel = instance.text works fine and retrieves the button name perfectly. My main problem however is sending that name over to the SweepSubTranPage() so that i can access it in my kivy file.

Here is the Python code:

class GuardsWindow(Screen):

    guardButtonName = ObjectProperty(None)

    def send_guard_type(self, instance, **kwargs):
        self.guardButtonName = instance.text      # retrieves the button name


    def sender(self):            # my attempt at sending it to the other class
        return self.guardButtonName


class SweepSubTranPage(Screen):

    pageTitle = ObjectProperty(None)
    gw = GuardsWindow()
    pageTitle.text = gw.sender()


kv = Builder.load_file("my.kv")


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

if __name__ == "__main__":
    MyMainApp().run()

And here is the Kivy file code:

<GuardsWindow>
    name: "GuardsHomepage"

    ScrollView:
        bar_width: 10
        do_scroll_x: False
        do_scroll_y: True
        scroll_type: ["bars", "content"]

        BoxLayout:
            orientation: "vertical"
            size_hint: 1, 2
            padding: 10
            spacing: 10

            Button:                  # these are the buttons that i am getting the text from
                text: "Full Guard"
                on_press:
                    root.send_guard_type(self)    # this sends the button
                on_release:
                    app.root.current = "SweepSubTranPage"
                    root.manager.transition.direction  = "left"


            Button:
                text: "Half Guard"
                on_press:
                    root.send_guard_type(self)    # this sends the button
                on_release:
                    app.root.current = "SweepSubTranPage"
                    root.manager.transition.direction  = "left"


<SweepSubTranPage>
    name: "SweepSubTranPage"
    pageTitle: title   

    BoxLayout:
        orientation: "horizontal"
        size_hint: 1, 0.1

        Label:              # this is the label that I need the text for
            id: title
            font_size: 40
            size_hint: 1, 1

When I run the code above, I get the error:

   File "C:/Users/bensw/PycharmProjects/BJJ Tracker/main.py", line 36, in <module>
     class SweepSubTranPage(Screen):
   File "C:/Users/bensw/PycharmProjects/BJJ Tracker/main.py", line 40, in SweepSubTranPage
     pageTitle.text = gw.sender()
 AttributeError: 'kivy.properties.ObjectProperty' object has no attribute 'text'

I hope I have been clear enough in explaining my issue. If you have any questions please ask! Thank you so much!!

Upvotes: 2

Views: 441

Answers (1)

John Anderson
John Anderson

Reputation: 38837

In your send_guard_type() method you can access another Screen using the manager property of any Screen, and the get_screen() method of the ScreenManager. Something like this:

def send_guard_type(self, instance, **kwargs):
    self.guardButtonName = instance.text      # retrieves the button name
    self.manager.get_screen("SweepSubTranPage").ids.title.text = instance.text

Upvotes: 1

Related Questions