【问题标题】:How to get value from popup filechooser?如何从弹出文件选择器中获取价值?
【发布时间】:2019-04-10 23:24:12
【问题描述】:

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

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()

这里是kivy代码:

<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

有人可以给我一些关于如何让值从弹出窗口传递到文本输入的指示吗?

【问题讨论】:

  • 我认为您的 kv 文件中有一些错误。请更正。
  • @ikolim 我修复了 kivy 中的缩进和缺少的类引用。我只发布了代码的 sn-p,因为我认为如果需要更多,有人会问。非常感谢您在下面的回答,因为它解决了我的问题。我也这样标记它。

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


【解决方案1】:

参考文本输入

使用以下代码填充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

输出

【讨论】:

  • 您是如何发布屏幕截图的?
  • 在编辑模式下,1) 选择放置图像/屏幕截图的位置。 2) 在菜单栏中,单击Picture Frame 图标(又名Image)。 3) 打开您的计算机文件浏览器/管理器,选择图像,拖放到包含“浏览、拖放或粘贴图像或链接”。 4) 上传图片后,点击“添加图片”按钮。
  • 感谢您的回答!
  • 作为一个编程初学者,这段代码在很短的时间内教会了我很多关于 kivy 的知识,谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-03
  • 2020-06-02
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
相关资源
最近更新 更多