Skip to content Skip to sidebar Skip to footer

How To Access Class Instance Initiated By The Kivy Code?

I am stuck. I can't wrap my mind around how to access an object that is initiated in the Kivy file? This is my code: from kivy.app import App from kivy.lang import Builder from kiv

Solution 1:

Several ways to accomplish this. Here is one. You can create a StringProperty in the LoginScreen to contain the created filename:

classLoginScreen(Screen):
    filename = StringProperty('')

    defcreate_file(self):
        self.filename = f"file_{time.strftime('%Y%m%d_%H%M%S')}.txt"withopen(self.filename, 'w') as file:
            file.write("Some content")

And the create_file() method saves the file name to the filename property.

Then you can access the filename several ways, like login_screen_instance.filename, or taking advantage of the fact that it is now a Property, you can use it in your kv:

<RootWidget>:LoginScreen:id:login_screenname:"login_screen"on_filename:root.get_file(self.filename)

And adding a get_file() method to RootWidget:

classRootWidget(ScreenManager):
    defget_file(self, filename):
        print('file is', filename)

Now, whenever a new file is created by the create_file() method, the get_file() method is called.

Post a Comment for "How To Access Class Instance Initiated By The Kivy Code?"