Kivy:从父小部件中的文件选择器弹出窗口访问数据

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

我正在尝试构建一个带有 3 个按钮的小部件,每个按钮都会打开一个文件选择器弹出窗口。弹出窗口有两个按钮“选择”和“取消”。当用户选择文件并单击“选择”按钮时,弹出窗口将关闭。

但是,现在,我不知道如何从主小部件的弹出窗口访问文件路径。我想在 TextInput 字段中显示文件路径,并将文件路径传递给 python 脚本。 像这样的东西:

enter image description here

下面是我的代码:

蟒蛇:

import kivy

kivy.require('2.3.0')

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup

class FileSelect(Popup):
    filechooser = ObjectProperty(None)

    def selected(self):
        print(self.filechooser.selection[0])
        return self.filechooser.selection[0]

class Prototype(Widget):
    categories_file_path = ObjectProperty(None)
    categories_file_path_btn = ObjectProperty(None)
    

    def select_press(self):
        self.categories_file_path.text = FileSelect().selected()
        print(FileSelect().selected())


class PrototypeApp(App):
    def build(self):
        return Prototype()

    pass


if __name__ == '__main__':
    PrototypeApp().run() 

kv 文件:

#:kivy 2.3.0
#:import Factory kivy.factory.Factory


<FileSelect@Popup>
    filechooser:filechooser
    auto_dismiss: False
    size_hint: 0.5, 0.5
    pos_hint: {"x":0.2, "top":0.9}
    title: "Select File"
    on_dismiss: Factory.Prototype().select_press()

    BoxLayout:
        orientation: "vertical"
        size: root.width, root.height

        FileChooserListView:
            id:filechooser

        GridLayout:
            size_hint: 1, 0.2
            cols:2

            Button:
                text: "Select"
                size_hint: 0.25, 0.1
                pos_hint: {'x':0, 'y':0, 'bottom':1, 'left':1}
                font_size: 12
                on_press: root.selected()
                on_release: root.dismiss()


            Button:
                text: "Close"
                size_hint: 0.25, 0.1
                pos_hint: {'bottom':1, 'right':1}
                font_size: 12
                on_release: root.dismiss()


<Prototype>:
    categories_file_path:categories_file_path
    categories_file_path_btn:categories_file_path_btn
    

    GridLayout:
        cols:2
        size: root.width, root.height

        GridLayout:
            cols:2
            #size: (root.width)/10, root.height

            Label:
                text: "Categories File Path"
            TextInput:
                id:categories_file_path
                multiline:True


            Button:
                id:categories_file_path_btn
                text: "Select"
                #on_press: root.select_press()
                on_release: Factory.FileSelect().open()
            Label:
                text: ""

            Label:
                text: "Another label"
            TextInput:
                id:anotherfilepath
                multiline:False

            Label:
                text: "Third label"
            TextInput:
                id:thirdfilepath
                multiline:False

            Button:
                text: "Submit"
                font_size:60
                size: 100,80
                on_press: root.press()

        Label:
            id:output
            text: ""
            multiline:True

如代码所示,我试图在弹出窗口关闭后立即从主小部件

filechooser.selection[0]
中的弹出窗口
FileSelect
访问
Prototype
。但是,我找不到正确的事件来绑定方法或函数。

python kivy kivy-language
1个回答
0
投票

一个问题是,每当您使用以下表达式时:

FileSelect()

使用

()
,您正在创建
FileSelect
的新实例。该新实例不是您选择文件的实例,因此它无法提供所选文件的路径。

一个修复方法是将

selection
传递给
select_press()
方法。如果你像这样修改
kv

<FileSelect@Popup>
    filechooser:filechooser
    auto_dismiss: False
    size_hint: 0.5, 0.5
    pos_hint: {"x":0.2, "top":0.9}
    title: "Select File"
    # on_dismiss: Factory.Prototype().select_press()
    on_dismiss: app.root.select_press(filechooser.selection)

然后,将使用

select_press()
参数调用
Prototype
实例的
root
方法(即
app
selection
)。然后必须修改
select_press()
方法以接受新参数:

def select_press(self, selection):
    print(selection)
    self.categories_file_path.text = str(selection)
    # print(FileSelect().selected())
© www.soinside.com 2019 - 2024. All rights reserved.