如何从弹出文件选择器获取价值?

时间:2019-04-10 23:24:13

标签: python python-3.x kivy kivy-language

我从下面的链接中获得了一个弹出文件选择器:

Kivy popup Filechooser pass variable (selection)

我无法获取在应用程序的主Tab()类实例与FileChoosePopup类弹出实例之间传递的文件路径字符串值。我知道这个问题以前曾被问过,但我似乎无法弄清楚。当我运行该应用程序并单击该文件时,我想传递给主类上的TextInput,出现以下错误:

AttributeError: 'super' object has no attribute '__getattr__'

我尝试使用super init方法传递对主类中弹出窗口类实例的引用,但是该应用程序甚至没有初始化。

这是我在python中的弹出窗口:

class FileChoosePopup(Popup):
    file_path = StringProperty("No file chosen")
    def __init__(self, **kwargs):
        super(FileChoosePopup, self).__init__(**kwargs)
        self.Tab = Tab()

    def load(self, selection):
        self.file_path = str(selection[0])
        path_file = self.file_path
        the_file = self.ids.get_file #is the textinput id

        if path_file != "No file chosen":
            the_file.text = path_file
            self.dismiss()
        else:
            self.dismiss()

class Tab(StackLayout):

    def open_popup(self):
        the_popup = FileChoosePopup()
        the_popup.open()

以下是猕猴桃代码:

<FileChoosePopup>:
    title: "Choose a .CSV File"
    size_hint: .9, .9
    auto_dismiss: False
    BoxLayout:
        orientation: "vertical"
        FileChooser:
            id: filechooser   
            FileChooserIconLayout
        BoxLayout:
            size_hint: (1, 0.1)
            pos_hint: {'center_x': .5, 'center_y': .5}
            spacing: 20
            RoundedCancelButton:
                text: "Cancel"
                on_release: root.dismiss()
            RoundedAcceptButton:
                text: "Load"
                on_release: root.load(filechooser.selection)
                id: ldbtn
                disabled: True if filechooser.selection==[] else False
<Tab>:
    TabbedPanel:
        do_defualt_tab: False
        background_color: (.87, .87, .87, 1)
        border: [0, 0, 0, 0]
        background_image: 'path/to/background/image'
        TabbedPanelItem:
            text: 'Import'
            background_color: (1, .5, 0, 1)
            background_normal: ''
            StackLayout:
                orientation: 'lr-tb'
                size_hint_y: None
                height: 30
                spacing: 5
                Label:
                    text: ''
                    size_hint_x: 1
                Label:
                    text: ''
                    size_hint_x: 0.2
                RoundedButton:
                    text: 'Choose File'
                    size_hint_x: 0.2
                    on_press: root.open_popup()
                TextInput:
                    id: get_file
                    readonly: True
                    size_hint_x: 0.4
                Label:
                    text: ''
                    size_hint_x: 0.2

有人可以给我一些有关如何获取从弹出窗口传递到textinput的值的指针吗?

1 个答案:

答案 0 :(得分:0)

参考TextInput

使用以下命令填充TextInput

self.ids.get_file.text = self.file_path

示例

main.py

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.properties import ObjectProperty, StringProperty
from kivy.lang import Builder


class FileChoosePopup(Popup):
    load = ObjectProperty()


class Tab(TabbedPanel):
    file_path = StringProperty("No file chosen")
    the_popup = ObjectProperty(None)

    def open_popup(self):
        self.the_popup = FileChoosePopup(load=self.load)
        self.the_popup.open()

    def load(self, selection):
        self.file_path = str(selection[0])
        self.the_popup.dismiss()
        print(self.file_path)

        # check for non-empty list i.e. file selected
        if self.file_path:
            self.ids.get_file.text = self.file_path


Builder.load_file('main.kv')


class TestApp(App):

    def build(self):
        return Tab()


if __name__ == "__main__":
    TestApp().run()

main.kv

<FileChoosePopup>:
    title: "Choose a .CSV File"
    size_hint: .9, .9
    auto_dismiss: False

    BoxLayout:
        orientation: "vertical"
        FileChooser:
            id: filechooser
            FileChooserIconLayout

        BoxLayout:
            size_hint: (1, 0.1)
            pos_hint: {'center_x': .5, 'center_y': .5}
            spacing: 20
            Button:
                text: "Cancel"
                on_release: root.dismiss()
            Button:
                text: "Load"
                on_release: root.load(filechooser.selection)
                id: ldbtn
                disabled: True if filechooser.selection==[] else False

<Tab>:
    do_default_tab: False

    TabbedPanelItem:
        text: 'Import'
        background_color: (1, .5, 0, 1)
        background_normal: ''
        StackLayout:
            orientation: 'lr-tb'
            size_hint_y: None
            height: 30
            spacing: 5
            Label:
                text: ''
                size_hint_x: 1
            Label:
                text: ''
                size_hint_x: 0.2
            Button:
                text: 'Choose File'
                size_hint_x: 0.2
                on_press: root.open_popup()
            TextInput:
                id: get_file
                readonly: True
                size_hint_x: 0.4
            Label:
                text: ''
                size_hint_x: 0.2

输出

Result