沮丧 'object has no attribute' 错误
Kivy 'object has no attribute' Error
我是 python 和 Kivy 编程的新手,所以遇到了麻烦,可能会在这里问一些简单的问题,但现在对我来说是一个很大的障碍。
我正在用 kivy 开发一个 GUI。我有一些获取数值的 TextInputs。在所有文本输入之后,我有一个 'OK' 按钮,它获取所有值并处理它们。
我在从 .kv 文件中的相同 class 调用函数时遇到错误。
main.py 文件:
# 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()
主kv文件
# 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 用于文本输入。
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 文本输入布局
<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()
当我按下 'OK' 按钮时,属性错误生成。
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'
我被困在这里,没有办法解决它。有什么我想念的吗?
编辑:
@eyllanesc:根据您的建议,我已经从我的 kv class 名称中删除了 @BoxLayout。当我从 main.kv
调用 MyLayout 时,我的主 .kv 文件出现错误
MyLayout:
id: _tool_box
size_hint: None,0.75
width: 300
错误:
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>
问题
您收到 AttributeError: 'MyLayout' object has no attribute 'print_something' 因为它找不到函数, print_something.
解决方案
详情请参考解释、示例和输出。
解释
jwelkreator.py
- 添加from simpleForm import MyLayout
- 删除Builder.load_file('simpleForm.kv')
jwelkreator.kv
添加#:include simpleForm.kv以包含外部kivy文件。
include <file> - Kivy Language
Includes an external kivy file. This allows you to split complex widgets into their own files.
simpleForm.py
您不必定义动态 class、LblTxt(BoxLayout),因为您已在其中定义了它你的 kv 文件。
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
由于在Python脚本中,simpleForm.py,你已经定义了classMyLayout是BoxLayout的,你不要在kv文件中继承它。将 替换为
例子
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()
输出
我是 python 和 Kivy 编程的新手,所以遇到了麻烦,可能会在这里问一些简单的问题,但现在对我来说是一个很大的障碍。 我正在用 kivy 开发一个 GUI。我有一些获取数值的 TextInputs。在所有文本输入之后,我有一个 'OK' 按钮,它获取所有值并处理它们。 我在从 .kv 文件中的相同 class 调用函数时遇到错误。
main.py 文件:
# 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()
主kv文件
# 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 用于文本输入。
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 文本输入布局
<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()
当我按下 'OK' 按钮时,属性错误生成。
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'
我被困在这里,没有办法解决它。有什么我想念的吗?
编辑: @eyllanesc:根据您的建议,我已经从我的 kv class 名称中删除了 @BoxLayout。当我从 main.kv
调用 MyLayout 时,我的主 .kv 文件出现错误MyLayout:
id: _tool_box
size_hint: None,0.75
width: 300
错误:
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>
问题
您收到 AttributeError: 'MyLayout' object has no attribute 'print_something' 因为它找不到函数, print_something.
解决方案
详情请参考解释、示例和输出。
解释
jwelkreator.py
- 添加from simpleForm import MyLayout
- 删除Builder.load_file('simpleForm.kv')
jwelkreator.kv
添加#:include simpleForm.kv以包含外部kivy文件。
include <file> - Kivy Language
Includes an external kivy file. This allows you to split complex widgets into their own files.
simpleForm.py
您不必定义动态 class、LblTxt(BoxLayout),因为您已在其中定义了它你的 kv 文件。
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
由于在Python脚本中,simpleForm.py,你已经定义了classMyLayout是BoxLayout的,你不要在kv文件中继承它。将
例子
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()