Kivy ScreenManager不再引用.py文件类中的类属性错误:“超级”具有

如何解决Kivy ScreenManager不再引用.py文件类中的类属性错误:“超级”具有

我刚刚开始使用kivy在python中开发我的第一个应用程序。除了主py之外,我还生成了应用程序的总体布局以及python文件中的一些逻辑,以将所有内容分开。一切正常,该应用程序运行无任何问题。我想在点击提供额外信息的按钮时添加一些屏幕。设法做到了,但是现在在添加额外的屏幕之前,当点击有效的按钮时,应用程序崩溃了。这些按钮使用的类不是在main.py中实现的,而是在辅助文件中实现的。如我所解释的,它们在添加ScreenManager之前起作用。这是我得到的错误的简短版本:AttributeError: 'super' object has no attribute '__getattr__'

Main.py: peenomat.py

import kivy
# -*- coding: iso-8859-1 -*-
from kivy.app import App

from kivy.uix.button import Label
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.anchorlayout import AnchorLayout
from kivy.core.text import LabelBase
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import BooleanProperty
from kivy.properties import ObjectProperty
from kivy.config import Config


Builder.load_file('Header.kv')
Builder.load_file('Statusbar.kv')
Builder.load_file('Inputparameters.kv')
Builder.load_file('Outputparameters.kv')
Builder.load_file('Extra.kv')
#loading main kv
Builder.load_file('peenomat.kv')


class Peenomat(Screen):
    pass


class Instruction(Screen):
    pass

class Additional(Screen):
    pass

# Create the screen manager
sm = ScreenManager()
sm.add_widget(Peenomat(name='peenomat'))
sm.add_widget(Instruction(name='instruction'))
sm.add_widget(Additional(name='additional'))


class PeenomatApp(App):
    def build(self):
        self.icon = 'icon.png'
        #return pm
        return sm
        #return Peenomat()

if __name__=="__main__":

    PeenomatApp().run()

主要kv.file: peenomat.kv

<Peenomat>

    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'bottom'
        GridLayout:
            cols: 1
 
            InputParameters:
                id:_input_parameters

            StatusBar:
                id:_status_bar
                       
                

<Instruction>:
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'bottom'
        GridLayout:
            cols: 1
            canvas.before:
            Label:
                text:

            Button:
                text: 
                on_release:
                    app.root.current = "peenomat"
                    app.root.transition.direction = "up"

<Additional>:
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'bottom'
        GridLayout:
            cols: 1
            Button:
                text: 
                on_release:
                    app.root.current = "peenomat"
                    app.root.transition.direction = "left"

.kv文件,其中引用了按钮的功能: statusbar.kv

#: import statusbar StatusBar

<StatusBar@BoxLayout>

    orientation:'horizontal'
    padding:
    spacing: 

    Label:
        size_hint: 0.05,1

    Button:
        text: 'Leeren'
        on_press: root.btn_clear()

    Button:
        text: "Mehr"
        on_release:
            app.root.current = "additional"
            app.root.transition.direction ="right"


    Button:
        text: u"Los schl\u00e4gts"
        on_press: root.btn_submit()

    Button:
        text: "?"
        on_release:
            app.root.current = "instruction"
            app.root.transition.direction ="down"

最后是提供类的python文件,在这些类中我生成了现在崩溃的按钮的功能: StatusBAr.py

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.app import App
from kivy.lang import Builder
#from Inputparameters import InputParameters


ver = ''
class InputParameters(GridLayout):
    verfahren =ObjectProperty(None)

    def on_state(self,togglebutton):
        tb = togglebutton
        global ver
        if tb.state == 'down':
            self.verfahren = tb.text
            ver = tb.text
            print(self.verfahren,ver)
            return self.verfahren


class StatusBar(BoxLayout):
    #InputGrößen
    group_mode = False
    prozess = ObjectProperty(None)
    vorbehandlung = ObjectProperty(None)
    material = ObjectProperty(None)
    haerte = ObjectProperty(None)
    rauheit = ObjectProperty(None)
    verfahren = ObjectProperty(None)

    #OutputGrößen
    frequency = StringProperty(None)
    speed = StringProperty(None)
    hub = StringProperty(None)

    def btn_submit(self):
        global ver
        ip = App.get_running_app().root.ids._input_parameters
        op = App.get_running_app().root.ids._output_parameters

        frequenz = 0
        if ip.haerte.value < 50:
            op.frequency = str(180) +" Hz"
            op.speed = str(2.4) +" mm/s"
            op.hub = str(3.4) + " mm"
        elif ip.haerte.value < 60:
            op.frequency = str(200) +" Hz"
            op.speed = str(3.5) + " mm/s"
            op.hub = str(5.23) + " mm"
        else:
            op.frequency = str(220) +" Hz"
            op.speed = str(1.2) + " mm/s"
            op.hub = str(7.2) + " mm"
        #control to see if right value is taken
        print(op.frequency)

    def btn_clear(self):
        ip = App.get_running_app().root.ids._input_parameters
        op = App.get_running_app().root.ids._output_parameters
        ip.pro1.state = "normal"
        ip.pro2.state = "normal"
        ip.pro3.state = "normal"
        ip.material.text = "Auswahl treffen"
        ip.vorbehandlung.text = "Auswahl treffen"
        ip.haerte.value = 55
        ip.rauheit.value = 5.5
        op.frequency = "---"
        op.speed = "---"
        op.hub = "---"

因此,如果使用提交或清除按钮,则会出现以下错误:

[INFO   ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "kivy\properties.pyx",line 860,in kivy.properties.ObservableDict.__getattr__
 KeyError: '_input_parameters'
 
 During handling of the above exception,another exception occurred:
 
 Traceback (most recent call last):
   File "C:/Users/schum/Dokumente/TUD/Masterthesis/Peenomat.py",line 79,in <module>
     PeenomatApp().run()
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\app.py",line 855,in run
     runTouchApp()
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\base.py",line 504,in runTouchApp
     EventLoop.window.mainloop()
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\core\window\window_sdl2.py",line 747,in mainloop
     self._mainloop()
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\core\window\window_sdl2.py",line 479,in _mainloop
     EventLoop.idle()
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\base.py",line 342,in idle
     self.dispatch_input()
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\base.py",line 327,in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\base.py",line 233,in post_dispatch_input
     listener.dispatch('on_motion',etype,me)
   File "kivy\_event.pyx",line 707,in kivy._event.EventDispatcher.dispatch
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\core\window\__init__.py",line 1402,in on_motion
     self.dispatch('on_touch_down',line 1418,in on_touch_down
     if w.dispatch('on_touch_down',touch):
   File "kivy\_event.pyx",in kivy._event.EventDispatcher.dispatch
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\uix\screenmanager.py",line 1191,in on_touch_down
     return super(ScreenManager,self).on_touch_down(touch)
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\uix\widget.py",line 549,in on_touch_down
     if child.dispatch('on_touch_down',in kivy._event.EventDispatcher.dispatch
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\uix\relativelayout.py",line 288,in on_touch_down
     ret = super(RelativeLayout,in kivy._event.EventDispatcher.dispatch
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\uix\widget.py",in kivy._event.EventDispatcher.dispatch
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\uix\behaviors\button.py",line 151,in on_touch_down
     self.dispatch('on_press')
   File "kivy\_event.pyx",line 703,in kivy._event.EventDispatcher.dispatch
   File "kivy\_event.pyx",line 1214,in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx",line 1098,in kivy._event.EventObservers._dispatch
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\venv\lib\site-packages\kivy\lang\builder.py",line 64,in custom_callback
     exec(__kvlang__.co_value,idmap)
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\Statusbar.kv",line 38,in <module>
     on_press: root.btn_submit()
   File "C:\Users\schum\Dokumente\TUD\Masterthesis\StatusBar.py",line 41,in btn_submit
     ip = App.get_running_app().root.ids._input_parameters
   File "kivy\properties.pyx",line 863,in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

按钮的功能是从.kv.file中获取所选参数

inputparameters.kv

#: import statusbar StatusBar

<InputParameters@GridLayout>
    #Initialisierung .py zu .kv Ids
    prozess: _prozess
    pro1: _prozess1
    pro2: _prozess2
    pro3: _prozess3
    vorbehandlung: _vorbehandlung
    material: _material
    haerte: _haerte
    rauheit: _rauheit

    cols: 2
    padding: 25
    spacing: 10

    #Prozess
    Label:
        text:'Prozess:              '

    BoxLayout:
        orientation: 'horizontal'
        id: _prozess

        ToggleButton:
            id:_prozess1
            text:'P-MOH'
            group: "proc_group"
            on_state: root.on_state(self)

        ToggleButton:
            id:_prozess2
            text:'E-MOH'
            group: "proc_group"
            on_state: root.on_state(self)

        ToggleButton:
            id:_prozess3
            text:'PE-MOH'
            group: "proc_group"
            on_state: root.on_state(self)

    #Material

    Label:
        text: 'Material:'

    Spinner:
        id: _material
        text: "Auswahl treffen"
        values: ['1.2379','Gusseisen','Kautschuk','Carbon','Adamantium']

    # Herstellschritte
    Label:
        text:'Fertigungsschritte:'

    Spinner:
        id: _vorbehandlung
        text: "Auswahl treffen"
        values: [u'Fr\u00e4sen','Erodieren','Schleifen','Polieren']

    # Haerte
    Label:
        text: u"H\u00e4rte:"

    BoxLayout:
        orientation: 'vertical'

        Label:
            text: str(_haerte.value) + ' HRC'

        Slider:
            id: _haerte
            min: 45
            max: 65
            step: 1
            value_track: True

    # Rauheit
    Label:
        text:'Rauheit:'              

    BoxLayout:
        orientation: 'vertical'
        Label:
            text: str("%.1f" % _rauheit.value) + ' Rz' #eine Nachkommastelle

        Slider:
            id: _rauheit
            min: 1
            max: 10
            value_track: True

奇怪的是,切换按钮仍可以使用statusbar.py中的generatet函数来完成其工作。 希望您能帮我解决这个问题,甚至不知道从哪里开始。 预先谢谢!!

解决方法

ids文件中的每个规则创建kv字典,并将那些ids放在该规则根目录的字典中。因此_input_parameters id仅在ids实例的Peenomat中。

所以,我认为您需要更改:

    ip = App.get_running_app().root.ids._input_parameters

类似:

    ip = App.get_running_app().root.get_screen('peenomat').ids._input_parameters

您的root中的AppScreenManager,而get_screen('peenomat')获得了Peenomat Screen实例。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-