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 kivy.uix.boxlayout import BoxLayout
import time
from kivy.uix.screenmanager import Screen, ScreenManager
import filestack
Builder.load_file("test.kv")
class LoginScreen(Screen):
def create_file(self):
filename = f"file_{time.strftime('%Y%m%d_%H%M%S')}.txt"
with open(filename, 'w') as file:
file.write("Some content")
class RootWidget(ScreenManager):
pass
class MainApp(App):
def build(self):
return RootWidget()
class FileSharer:
def __init__(self, filepath, api_key="abcdefg"):
self.filepath = filepath
self.api_key = api_key
def share(self):
client = filestack.Client(self.api_key)
new_filelink = client.upload(filepath=self.filepath)
return new_filelink.url
MainApp().run()
And my test.kv
file:
<LoginScreen>:
GridLayout:
cols: 1
padding: 10, 10
spacing: 10, 10
Button:
text: 'Create File'
size_hint_y: None
height: '48dp'
on_press: root.create_file()
<RootWidget>:
LoginScreen:
id: login_screen
name: "login_screen"
Here's a visual of what the program produces:
When the Create File button is pressed, the LoginScreen.create_file()
method is called by the Kivy file, and a text file with a name containing the current timestamp is created (e.g. file_20200624_173229.txt).
I want to be able to access that filename from the Python code. The reason I want to do this is I want to upload the file to the cloud using the FileSharer class. In order to do that, I need to initlalize FileSharer
like FileSharer(filepath=...)
.
I know I can get around this by using os.listdir()
to look for the generated files in the disk, but I want to do this the right way which is by accessing the value of filename
from the LoginScreen
instance.