如何加载文件选择器对话框
How to load a filechooser dialog
我是 Kivy 的新手,尽管读了几本书,大量的 Kivy 文档,并查看了无数示例,但我仍然很难弄清楚如何构建我的程序。我正在尝试让文件选择器对话框正常工作。
我想从一个只有一个按钮的简单 BoxLayout 界面开始。按下此按钮后,我想显示文件选择器对话框。我从其中一本书中获取了很多代码。我的问题是如何调用 LoadDialog widget/class。我知道我的按钮不应该引用 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)
Python代码
- 添加缺失的导入语句,
from kivy.uix.popup import Popup
- 将 class
LoadDialog
重命名为 Root
- 添加一个 class LoadDialog 只有两个 ObjectProperty
片段
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):
kv文件
- 添加根规则,
Root:
在BoxLayout:
之前
片段
Root:
BoxLayout:
Button:
text: "Click me"
on_release: root.show_load_list()
<LoadDialog>:
BoxLayout:
输出
我是 Kivy 的新手,尽管读了几本书,大量的 Kivy 文档,并查看了无数示例,但我仍然很难弄清楚如何构建我的程序。我正在尝试让文件选择器对话框正常工作。
我想从一个只有一个按钮的简单 BoxLayout 界面开始。按下此按钮后,我想显示文件选择器对话框。我从其中一本书中获取了很多代码。我的问题是如何调用 LoadDialog widget/class。我知道我的按钮不应该引用 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)
Python代码
- 添加缺失的导入语句,
from kivy.uix.popup import Popup
- 将 class
LoadDialog
重命名为Root
- 添加一个 class LoadDialog 只有两个 ObjectProperty
片段
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):
kv文件
- 添加根规则,
Root:
在BoxLayout:
之前
片段
Root:
BoxLayout:
Button:
text: "Click me"
on_release: root.show_load_list()
<LoadDialog>:
BoxLayout: