如何加载filechooser对话框

问题描述 投票:0回答:1

我是一个Kivy新手,尽管已经阅读了几本书,相当数量的Kivy文档,并查看了大量的例子,但我仍然很难解决如何构建我的程序。我正在尝试使用filechooser对话框。

我想从一个带有单个按钮的简单BoxLayout接口开始。按下此按钮我想显示filechooser对话框。我从其中一本书中摘取了很多代码。我的问题是如何调用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)
python kivy kivy-language
1个回答
1
投票

Python Code

  1. 添加缺少的导入语句from kivy.uix.popup import Popup
  2. 将类LoadDialog重命名为Root
  3. 添加一个只有两个ObjecProperty的类LoadDialog

片段

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 file

  1. Root:之前添加根规则BoxLayout:

片段

Root:
    BoxLayout:
        Button:
            text: "Click me"
            on_release: root.show_load_list()

<LoadDialog>:
    BoxLayout:

Output

Img01

© www.soinside.com 2019 - 2024. All rights reserved.