我是Kivy的新手,尽管阅读了几本书,大量的Kivy文档,并查看了许多示例,但在制定程序结构方面仍然遇到很大的麻烦。我正在尝试使filechooser对话框正常工作。
我想从一个带有单个按钮的简单BoxLayout界面开始。按下此按钮后,我要显示“文件选择器”对话框。我从其中一本书中摘取了很多这样的代码。我的问题是如何调用LoadDialog小部件/类。我知道我的按钮不应该引用root.show_load_list(),但是我不确定应该如何引用它。我非常感谢朝着正确方向前进。
# File name: main.py
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.lang import Builder
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
def show_load_list(self):
content = LoadDialog(load=self.load_list, cancel=self.dismiss_popup)
self._popup = Popup(title="Load a file list", content=content, size_hint=(1, 1))
self._popup.open()
def load_list(self, path, filename):
pass
def dismiss_popup(self):
self._popup.dismiss()
class LoadDialogApp(App):
pass
if __name__ == '__main__':
LoadDialogApp().run()
我的kv文件定义为
# File name: loaddialog.kv
BoxLayout:
Button:
text: "Click me"
on_release: root.show_load_list()
<LoadDialog>:
BoxLayout:
size: root.size
pos: root.pos
orientation: "vertical"
FileChooserListView:
id: filechooser
path: './'
BoxLayout:
size_hint_y: None
height: 30
Button:
text: "Cancel"
on_release: root.cancel()
Button:
text: "Load"
on_release: root.load(filechooser.path, filechooser.selection)
答案 0 :(得分:1)
from kivy.uix.popup import Popup
LoadDialog
重命名为Root
from kivy.uix.popup import Popup
class LoadDialog(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
class Root(FloatLayout):
load = ObjectProperty(None)
cancel = ObjectProperty(None)
def show_load_list(self):
Root:
前添加根规则BoxLayout:
Root:
BoxLayout:
Button:
text: "Click me"
on_release: root.show_load_list()
<LoadDialog>:
BoxLayout: