我一直在尝试通过应用程序的按钮释放创建文件选择器弹出窗口。 我分别管理了 FileChooser 和 Popups,但无法将两者一起解决,这里有人可以帮助解决问题吗? 我正在尝试用 Python 而不是 Kivy.lang 来实现 PopUp,因为这是我在弹出窗口方面的经验。我也无法让 Kivy Doc 示例正常工作。 我目前拥有的代码如下。
import kivy
from kivy.uix.filechooser import FileChooserListView
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.popup import Popup
import os
Builder.load_string(
"""
<MyWidget>:
Button
text: "open"
on_release: root.load()
""")
class MyWidget(BoxLayout):
def load(self):
load_popup = Popup(
title = "Select logo",
size_hint = (None, None),
size = (500, 500)
)
file_load= FileChooserListView(
on_selection = self.selected(file_load.selection) #selection not in My
)
content = file_load
load_popup.content = content
load_popup.open()
def open(self, path, filename):
with open(os.path.join(path, filename[0])) as f:
print(f)
def selected(self, filename):
print("selected: %s" % filename[0])
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
运行代码时出现错误(在分配之前使用
file_load
)。但你可以通过改变来纠正这个问题:
file_load= FileChooserListView(
on_selection = self.selected(file_load.selection) #selection not in My
)
至:
file_load = FileChooserListView(
on_submit=self.selected
)
然后将您的
selected()
方法重新定义为:
def selected(self, chooser, filename, touch):
print("selected: %s" % filename[0])