如何访问Kivy代码启动的类实例? [英] How to access class instance initiated by the Kivy code?
问题描述
我被困住了.我无法确定如何访问在Kivy文件中启动的对象?
I am stuck. I can't wrap my mind around how to access an object that is initiated in the Kivy file?
这是我的代码:
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()
和我的 test.kv
文件:
<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:
当按下 Create File 按钮时,Kivy文件将调用 LoginScreen.create_file()
方法,而文本文件的名称将包含当前时间戳记已创建(例如file_20200624_173229.txt).
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).
我希望能够从Python代码访问该文件名.我要这样做的原因是我想使用FileSharer类将文件上传到云中.为此,我需要像 FileSharer(filepath = ...)
一样初始化 FileSharer
.
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=...)
.
我知道我可以通过使用 os.listdir()
在磁盘中查找生成的文件来解决此问题,但是我想通过访问的值来正确地做到这一点. LoginScreen
实例中的 filename
.
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.
推荐答案
几种方法可以实现此目的.这是一个.您可以在 LoginScreen
中创建一个 StringProperty
来包含创建的文件名:
Several ways to accomplish this. Here is one. You can create a StringProperty
in the LoginScreen
to contain the created filename:
class LoginScreen(Screen):
filename = StringProperty('')
def create_file(self):
self.filename = f"file_{time.strftime('%Y%m%d_%H%M%S')}.txt"
with open(self.filename, 'w') as file:
file.write("Some content")
create_file()
方法将文件名保存到 filename
属性.
And the create_file()
method saves the file name to the filename
property.
然后,您可以通过几种方式访问文件名,例如 login_screen_instance.filename
,或者利用它现在是 Property
的事实,可以在您的文件名中使用它. kv
:
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_screen
name: "login_screen"
on_filename: root.get_file(self.filename)
并向 RootWidget
中添加 get_file()
方法:
class RootWidget(ScreenManager):
def get_file(self, filename):
print('file is', filename)
现在,每当通过 create_file()
方法创建新文件时,就会调用 get_file()
方法.
Now, whenever a new file is created by the create_file()
method, the get_file()
method is called.
这篇关于如何访问Kivy代码启动的类实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!