【发布时间】: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