Kivy\'对象没有属性\'错误

本文介绍了Kivy'对象没有属性'错误的处理方法,对大家解决问题具有一定的参考价值

问题描述

我是 python 和 Kivy 编程的新手,所以遇到了麻烦,可能会在这里提出简单的问题,但现在对我来说是一个很大的障碍.我正在用 kivy 开发一个 GUI.我有一些 TextInputs 可以获取数值.在所有文本输入之后,我有一个确定"按钮,它可以获取所有值并处理它们.我从 .kv 文件中的同一类调用函数时出错.

main.py 文件:

# 文件名:jwelkreator.py进口猕猴桃kivy.require('1.7.0')从 kivy.app 导入应用程序从 kivy.lang 导入生成器从 kivy.uix.anchorlayout 导入 AnchorLayoutBuilder.load_file('simpleForm.kv')......JwelKreator 类(AnchorLayout):经过JwelKreatorApp 类(应用程序):定义构建(自我):返回 JwelKreator()如果 __name__=="__main__":JwelKreatorApp().run()

主kv文件

# 文件名:jwelkreator.kv#:kivy 1.7.0<JwelKreator>:锚点布局:anchor_x: '左'anchor_y:'顶部'我的布局:编号:_tool_boxsize_hint:无,0.75宽度:300......

simpleForm.py 用于文本输入.

进口kivy从 kivy.app 导入应用程序从 kivy.lang 导入生成器从 kivy.config 导入配置从 kivy.uix.boxlayout 导入 BoxLayout类 LblTxt(BoxLayout):经过类 MyLayout(BoxLayout):def print_something(self):打印你好"

simpleForm.kv 文本输入布局

<LblTxt@BoxLayout>:id:LblTxtid方向:水平"lblTxtIn: '默认'theTxt: iAmTxt标签:文本:root.lblTxtIn大小提示:1,0.5文本输入:编号:iAmTxt多行:假提示文本:仅限数字"输入过滤器:'int'size_hint:0.5,无身高:30<MyLayout@BoxLayout>:方向:垂直"LblTxt:编号:lt0lblTxtIn: '基础层'LblTxt:编号:lt1lblTxtIn: '基本曝光时间(毫秒)'LblTxt:编号:lt2lblTxtIn: '最小支撑高度(mm)'LblTxt:编号:lt3lblTxtIn: '支持层'LblTxt:编号:lt4lblTxtIn: '支持曝光时间(毫秒)'LblTxt:编号:lt5lblTxtIn: '工作暴露时间(毫秒)'标签:text:"层数"按钮:文字:好的"size_hint:0.5,无身高:30on_release: root.print_something()

当我按下确定"按钮时,属性错误生成.

Traceback(最近一次调用最后一次):文件jwelkreator.py",第 21 行,在 <module>JwelKreatorApp().run()运行中的文件/usr/lib/python2.7/dist-packages/kivy/app.py",第 600 行运行触摸应用程序()runTouchApp 中的文件/usr/lib/python2.7/dist-packages/kivy/base.py",第 454 行EventLoop.window.mainloop()文件/usr/lib/python2.7/dist-packages/kivy/core/window/window_pygame.py",第 325 行,在 mainloopself._mainloop()_mainloop 中的文件/usr/lib/python2.7/dist-packages/kivy/core/window/window_pygame.py",第 231 行EventLoop.idle()文件/usr/lib/python2.7/dist-packages/kivy/base.py",第 297 行,处于空闲状态self.dispatch_input()文件/usr/lib/python2.7/dist-packages/kivy/base.py",第 284 行,在 dispatch_inputpost_dispatch_input(*pop(0))post_dispatch_input 中的文件/usr/lib/python2.7/dist-packages/kivy/base.py",第 253 行wid.dispatch('on_touch_up', 我)文件_event.pyx",第 285 行,在 kivy._event.EventDispatcher.dispatch (kivy/_event.c:4184)文件/usr/lib/python2.7/dist-packages/kivy/uix/button.py",第 140 行,在 on_touch_upself.dispatch('on_release')文件_event.pyx",第 281 行,在 kivy._event.EventDispatcher.dispatch (kivy/_event.c:4134)文件simpleForm.kv",第 1 行,在 <module><LblTxt@BoxLayout>:AttributeError:MyLayout"对象没有属性print_something"

我被困在这里,没有办法解决它.有什么我想念的吗?

@eyllanesc:根据你的建议,我已经从我的 kv 类名中删除了 @BoxLayout.当我从 main.kv 调用 MyLayout 时,我的主 .kv 文件出现错误

我的布局:编号:_tool_boxsize_hint:无,0.75宽度:300

错误:

Traceback(最近一次调用最后一次):文件jwelkreator.py",第 21 行,在 <module>JwelKreatorApp().run()运行中的文件/usr/lib/python2.7/dist-packages/kivy/app.py",第 577 行根 = self.build()文件jwelkreator.py",第 18 行,在构建中返回 JwelKreator()文件/usr/lib/python2.7/dist-packages/kivy/uix/anchorlayout.py",第 62 行,在 __init__super(AnchorLayout, self).__init__(**kwargs)文件/usr/lib/python2.7/dist-packages/kivy/uix/layout.py",第 61 行,在 __init__超级(布局,自我).__init__(**kwargs)文件/usr/lib/python2.7/dist-packages/kivy/uix/widget.py",第 163 行,在 __init__Builder.apply(self)文件/usr/lib/python2.7/dist-packages/kivy/lang.py",第 1429 行,在应用中self._apply_rule(小部件,规则,规则)_apply_rule 中的文件/usr/lib/python2.7/dist-packages/kivy/lang.py",第 1534 行self._apply_rule(child, crule, rootrule)_apply_rule 中的文件/usr/lib/python2.7/dist-packages/kivy/lang.py",第 1496 行cls = Factory_get(cname)文件/usr/lib/python2.7/dist-packages/kivy/factory.py",第 91 行,在 __getattr__raise FactoryException('未知类 <%s>' % name)kivy.factory.FactoryException:未知类<MyLayout>

解决方案

问题

您收到 AttributeError: 'MyLayout' object has no attribute 'print_something',因为它找不到函数 print_something.p>

解决方案

详情请参阅解释、示例和输出.

说明

jwelkreator.py

  1. 添加from simpleForm import MyLayout
  2. 删除 Builder.load_file('simpleForm.kv')

jwelkreator.kv

添加 #:include simpleForm.kv 以包含外部 kivy 文件.

I'm new to python and Kivy programming so getting trouble and may be asking simple question here, but its a big hurdle for me now. I am developing a GUI with kivy. I have some TextInputs which get numeric value. after all text inputs , I have a 'OK' button which gets all value and process them. I'm getting error in calling the function from same class in .kv file.

main.py file:

# File name: jwelkreator.py
import kivy
kivy.require('1.7.0')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.anchorlayout import AnchorLayout

Builder.load_file('simpleForm.kv')
...
...

class JwelKreator(AnchorLayout):
    pass

class JwelKreatorApp(App):
    def build(self):
        return JwelKreator()

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

main kv file

# File name: jwelkreator.kv
#:kivy 1.7.0
<JwelKreator>:
    AnchorLayout:
        anchor_x: 'left'
        anchor_y: 'top'
        MyLayout:
            id: _tool_box
            size_hint: None,0.75
            width: 300
   ...
   ...

simpleForm.py for text inputs.

import kivy

from kivy.app import App
from kivy.lang import Builder
from kivy.config import Config

from kivy.uix.boxlayout import BoxLayout

class LblTxt(BoxLayout):
    pass
class MyLayout(BoxLayout):
    def print_something(self):
        print "Hello"

simpleForm.kv TextInput Layout

<LblTxt@BoxLayout>:
    id:LblTxtid
    orientation: 'horizontal'      
    lblTxtIn: 'default'
    theTxt: iAmTxt
    Label:
        text: root.lblTxtIn
        size_hint: 1,0.5
    TextInput:
        id: iAmTxt  
        multiline: False
        hint_text: "numeric only"           
        input_filter: 'int'
        size_hint: 0.5,None
        height: 30
<MyLayout@BoxLayout>:  
    orientation: 'vertical'

    LblTxt:   
        id: lt0
        lblTxtIn: 'Base Layers'

    LblTxt:   
        id: lt1
        lblTxtIn: 'Base exposer time(ms)'

    LblTxt:   
        id: lt2
        lblTxtIn: 'Min Support Height(mm)'

    LblTxt:   
        id: lt3
        lblTxtIn: 'Support Layers'

    LblTxt:   
        id: lt4
        lblTxtIn: 'Support exposer time(ms)'

    LblTxt:   
        id: lt5
        lblTxtIn: 'Job exposer time(ms)'

    Label:
        text:"Number of Layers"
    Button:
        text: 'OK'
        size_hint: 0.5,None
        height: 30
        on_release: root.print_something()

While I press 'OK' button, attribute Error Generated.

Traceback (most recent call last):
   File "jwelkreator.py", line 21, in <module>
     JwelKreatorApp().run()
   File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 600, in run
     runTouchApp()
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 454, in runTouchApp
     EventLoop.window.mainloop()
   File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_pygame.py", line 325, in mainloop
     self._mainloop()
   File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_pygame.py", line 231, in _mainloop
     EventLoop.idle()
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 297, in idle
     self.dispatch_input()
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 284, in dispatch_input
     post_dispatch_input(*pop(0))
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 253, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "_event.pyx", line 285, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:4184)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/button.py", line 140, in on_touch_up
     self.dispatch('on_release')
   File "_event.pyx", line 281, in kivy._event.EventDispatcher.dispatch (kivy/_event.c:4134)
   File "simpleForm.kv", line 1, in <module>
     <LblTxt@BoxLayout>:
 AttributeError: 'MyLayout' object has no attribute 'print_something'

I am stuck here and not getting way to solve it. Is there anything I'm missing?

EDIT: @eyllanesc: as per your suggestion, I've removed @BoxLayout from my kv class name. I'm getting error from my main .kv file as I'm calling MyLayout from main.kv

MyLayout:
            id: _tool_box
            size_hint: None,0.75
            width: 300

ERROR:

Traceback (most recent call last):
   File "jwelkreator.py", line 21, in <module>
     JwelKreatorApp().run()
   File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 577, in run
     root = self.build()
   File "jwelkreator.py", line 18, in build
     return JwelKreator()
   File "/usr/lib/python2.7/dist-packages/kivy/uix/anchorlayout.py", line 62, in __init__
     super(AnchorLayout, self).__init__(**kwargs)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/layout.py", line 61, in __init__
     super(Layout, self).__init__(**kwargs)
   File "/usr/lib/python2.7/dist-packages/kivy/uix/widget.py", line 163, in __init__
     Builder.apply(self)
   File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1429, in apply
     self._apply_rule(widget, rule, rule)
   File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1534, in _apply_rule
     self._apply_rule(child, crule, rootrule)
   File "/usr/lib/python2.7/dist-packages/kivy/lang.py", line 1496, in _apply_rule
     cls = Factory_get(cname)
   File "/usr/lib/python2.7/dist-packages/kivy/factory.py", line 91, in __getattr__
     raise FactoryException('Unknown class <%s>' % name)
 kivy.factory.FactoryException: Unknown class <MyLayout>

解决方案

Problem

You are getting AttributeError: 'MyLayout' object has no attribute 'print_something' because it cannot find the function, print_something.

Solution

Please refer to the explanations, example and output for details.

Explanations

jwelkreator.py

  1. Add from simpleForm import MyLayout
  2. Remove Builder.load_file('simpleForm.kv')

jwelkreator.kv

Add #:include simpleForm.kv to include an external kivy file.

include <file> - Kivy Language

Includes an external kivy file. This allows you to split complex widgets into their own files.

simpleForm.py

You don't have to define the dynamic class, LblTxt(BoxLayout) since you have it defined in your kv file.

Dynamic Classes - Programming Guide » Kv language

This class, created just by the declaration of this rule, inherits from the Button class and allows us to change default values and create bindings for all its instances without adding any new code on the Python side.

simpleform.kv

Since in the Python script, simpleForm.py, you have already defined class MyLayout is of BoxLayout, you don't to inherit it in the kv file. Replace <MyLayout@BoxLayout> with <MyLayout>

Example

jwealkreator.py

from kivy.app import App
from kivy.uix.anchorlayout import AnchorLayout
from simpleForm import MyLayout


class JwelKreator(AnchorLayout):
    pass


class JwelKreatorApp(App):

    def build(self):
        return JwelKreator()


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

jwealkreator.kv

# File name: jwelkreator.kv
#:kivy 1.10.0
#:include simpleform.kv

<JwelKreator>:
    anchor_x: 'left'
    anchor_y: 'top'
    MyLayout:
        id: _tool_box
        size_hint: None,0.75
        width: 300

simpleForm.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout


class MyLayout(BoxLayout):
    def print_something(self):
        print("Hello")


class SimpleFormApp(App):

    def build(self):
        return MyLayout()


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

simpleform.kv

#:kivy 1.10.0

<LblTxt@BoxLayout>:
    id:LblTxtid
    orientation: 'horizontal'
    lblTxtIn: 'default'
    theTxt: iAmTxt
    Label:
        text: root.lblTxtIn
        size_hint: 1,0.5
    TextInput:
        id: iAmTxt
        multiline: False
        hint_text: "numeric only"
        input_filter: 'int'
        size_hint: 0.5,None
        height: 30

<MyLayout>:
    orientation: 'vertical'

    LblTxt:
        id: lt0
        lblTxtIn: 'Base Layers'

    LblTxt:
        id: lt1
        lblTxtIn: 'Base exposer time(ms)'

    LblTxt:
        id: lt2
        lblTxtIn: 'Min Support Height(mm)'

    LblTxt:
        id: lt3
        lblTxtIn: 'Support Layers'

    LblTxt:
        id: lt4
        lblTxtIn: 'Support exposer time(ms)'

    LblTxt:
        id: lt5
        lblTxtIn: 'Job exposer time(ms)'

    Label:
        text:"Number of Layers"
    Button:
        text: 'OK'
        size_hint: 0.5,None
        height: 30
        on_release: root.print_something()

Output

这篇关于Kivy'对象没有属性'错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

admin_action_{$_REQUEST[‘action’]}

do_action( "admin_action_{$_REQUEST[‘action’]}" )动作钩子::在发送“Action”请求变量时激发。Action Hook: Fires when an ‘action’ request variable is sent.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$_REQUEST['action']引用从GET或POST请求派生的操作。源码(Source)更新版本源码位置使用被使用2.6.0 wp-admin/admin.php:...

日期:2020-09-02 17:44:16 浏览:1162

admin_footer-{$GLOBALS[‘hook_suffix’]}

do_action( "admin_footer-{$GLOBALS[‘hook_suffix’]}", string $hook_suffix )操作挂钩:在默认页脚脚本之后打印脚本或数据。Action Hook: Print scripts or data after the default footer scripts.目录锚点:#说明#参数#源码说明(Description)钩子名的动态部分,$GLOBALS['hook_suffix']引用当前页的全局钩子后缀。参数(Parameters)参数类...

日期:2020-09-02 17:44:20 浏览:1065

customize_save_{$this->id_data[‘base’]}

do_action( "customize_save_{$this-&gt;id_data[‘base’]}", WP_Customize_Setting $this )动作钩子::在调用WP_Customize_Setting::save()方法时激发。Action Hook: Fires when the WP_Customize_Setting::save() method is called.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_data...

日期:2020-08-15 15:47:24 浏览:802

customize_value_{$this->id_data[‘base’]}

apply_filters( "customize_value_{$this-&gt;id_data[‘base’]}", mixed $default )过滤器::过滤未作为主题模式或选项处理的自定义设置值。Filter Hook: Filter a Customize setting value not handled as a theme_mod or option.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分,$this->id_date['base'],指的是设置...

日期:2020-08-15 15:47:24 浏览:892

get_comment_author_url

过滤钩子:过滤评论作者的URL。Filter Hook: Filters the comment author’s URL.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/comment-template.php:32610...

日期:2020-08-10 23:06:14 浏览:927

network_admin_edit_{$_GET[‘action’]}

do_action( "network_admin_edit_{$_GET[‘action’]}" )操作挂钩:启动请求的处理程序操作。Action Hook: Fires the requested handler action.目录锚点:#说明#源码说明(Description)钩子名称的动态部分$u GET['action']引用请求的操作的名称。源码(Source)更新版本源码位置使用被使用3.1.0 wp-admin/network/edit.php:3600...

日期:2020-08-02 09:56:09 浏览:874

network_sites_updated_message_{$_GET[‘updated’]}

apply_filters( "network_sites_updated_message_{$_GET[‘updated’]}", string $msg )筛选器挂钩:在网络管理中筛选特定的非默认站点更新消息。Filter Hook: Filters a specific, non-default site-updated message in the Network admin.目录锚点:#说明#参数#源码说明(Description)钩子名称的动态部分$_GET['updated']引用了非默认的...

日期:2020-08-02 09:56:03 浏览:857

pre_wp_is_site_initialized

过滤器::过滤在访问数据库之前是否初始化站点的检查。Filter Hook: Filters the check for whether a site is initialized before the database is accessed.目录锚点:#源码源码(Source)更新版本源码位置使用被使用 wp-includes/ms-site.php:93910...

日期:2020-07-29 10:15:38 浏览:828

WordPress 的SEO 教学:如何在网站中加入关键字(Meta Keywords)与Meta 描述(Meta Description)?

你想在WordPress 中添加关键字和meta 描述吗?关键字和meta 描述使你能够提高网站的SEO。在本文中,我们将向你展示如何在WordPress 中正确添加关键字和meta 描述。为什么要在WordPress 中添加关键字和Meta 描述?关键字和说明让搜寻引擎更了解您的帖子和页面的内容。关键词是人们寻找您发布的内容时,可能会搜索的重要词语或片语。而Meta Description则是对你的页面和文章的简要描述。如果你想要了解更多关于中继标签的资讯,可以参考Google的说明。Meta 关键字和描...

日期:2020-10-03 21:18:25 浏览:1706

谷歌的SEO是什么

SEO (Search Engine Optimization)中文是搜寻引擎最佳化,意思近于「关键字自然排序」、「网站排名优化」。简言之,SEO是以搜索引擎(如Google、Bing)为曝光媒体的行销手法。例如搜寻「wordpress教学」,会看到本站的「WordPress教学:12个课程…」排行Google第一:关键字:wordpress教学、wordpress课程…若搜寻「网站架设」,则会看到另一个网页排名第1:关键字:网站架设、架站…以上两个网页,每月从搜寻引擎导入自然流量,达2万4千:每月「有机搜...

日期:2020-10-30 17:23:57 浏览:1303