【问题标题】:Trouble changing text of Kivy TextInput widget using id: raises error " AttributeError: 'super' object has no attribute '__getattr__' "使用 id 更改 Kivy TextInput 小部件的文本时出现问题:引发错误“AttributeError: 'super' object has no attribute '__getattr__'”
【发布时间】:2019-07-08 16:01:11
【问题描述】:

在尝试更改 TextInput 小部件的显示文本时,我遇到了这个错误:

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

它抛出错误并且程序停止。
错误位于 .py 文件第 35 行。

这个问题只有在代码中有特定行时才会出现;否则有效。

.py 文件:

from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
import nibabel as nib
from kivy.garden.filebrowser import FileBrowser
from kivy.utils import platform
from kivy.uix.popup import Popup
from kivy.uix.textinput import TextInput
from kivy.lang import Builder

Builder.load_file("stack-test2.kv")

class Tabs(TabbedPanel):

    def nOpen(self):
        npop = niiPop()
        npop.open()

class niiPop(Popup):

    pathVariable = ' '
    file = ' '

    def nProcessor(self):
        if len(self.ids.nFile.selection) == 1:
            niiPop.pathVariable = str(self.ids.nFile.selection[0])
            niiPop.file = nib.load(niiPop.pathVariable)
            displayHeader = TextInput(text = str(niiPop.file.header), readonly = True)
            self.ids.nFile.clear_widgets()
            self.ids.nFile.add_widget(displayHeader)
            niiPop.auto_dismiss = True
            self.ids.fld1.text = niiPop.pathVariable
        else:
            self.ids.nFile.filename = ''

class stackTest1(App):
    def build(self):
        self.title = 'Test app'
        return Tabs()

if __name__ == "__main__":
    app = stackTest1()
    app.run()

.kv 文件:

<TabbedPanelStrip>:    

<Tabs>:
    do_default_tab: False
    tab_width: self.size[0] / 3

    TabbedPanelItem:
        text: 'Setup'
        id: tab1

        BoxLayout: 
            orientation: 'horizontal'

            BoxLayout:
                orientation: 'vertical'

                BoxLayout:
                    orientation: 'horizontal'
                    Label:
                        text: 'file import'
                        size_hint: (0.3, 0.2)
                    TextInput:
                        id: fld1
                        text: ' '
                        multiline: False
                        readonly: True
                        size_hint: (0.6, 0.2)
                    Button:
                        id: btn1
                        text: '...'
                        size_hint: (0.1, 0.2)
                        on_press: root.nOpen()

    TabbedPanelItem:
        text: 'tab2'

    TabbedPanelItem:
        text: 'tab3'

<niiPop>:
    id: niiPop
    title: 'Select a file'
    size_hint: (0.8, 0.8) 
    auto_dismiss: False
    BoxLayout:
        orientation: 'vertical'
        FileBrowser:    
            id: nFile
            filters: ['*.nii'] 
            select_string: 'Select'
            on_success: root.nProcessor()
            on_canceled: root.dismiss()

输出错误日志:

self.ids.fld1.text = niiPop.pathVariable

  File "kivy\properties.pyx", line 841, in kivy.properties.ObservableDict.__getattr__

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

提前感谢您的帮助。

【问题讨论】:

  • 您正在使用的id 是为Tabs 小部件定义的,但您使用它就像是为niiPop 小部件定义的一样。

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


【解决方案1】:

问题 - AttributeError

self.ids.fld1.text = niiPop.pathVariable

  File "kivy\properties.pyx", line 841, in kivy.properties.ObservableDict.__getattr__

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

根本原因

关键字self 始终引用当前小部件,即niiPop id: fld1 未在class niiPop() 中定义。因此,您的 Kivy 应用程序抛出了AttributeError

解决方案

需要在 kv 和 py 文件中进行以下增强才能解决问题。

kv 文件

  • 在类规则&lt;Tabs&gt;: 中添加id: fld1ObjectProperty 之间的链接

片段 - kv 文件

<Tabs>:
    fld1: fld1    # link fld1 to OjectProperty
    do_default_tab: False
    tab_width: self.size[0] / 3

    TabbedPanelItem:
        text: 'Setup'
        ...

py 文件

  1. 使用App.get_running_app() 函数获取您的应用实例。
  2. 使用关键字root 获取应用根目录的实例。
  3. 使用自动生成的ObjectPropertyfld1 来引用TextInput 实例。

片段 - py 文件

class niiPop(Popup):
    ...

    def nProcessor(self):
        if len(self.ids.nFile.selection) == 1:
            ...
            App.get_running_app().root.fld1.text = niiPop.pathVariable
        else:
            self.ids.nFile.filename = ''

输出

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2023-01-22
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多