Python/Kivy 属性错误
Python/Kivy Attribute Error
我正在编写一些代码来为 Python/Kivy 中的触摸屏创建 UI。我对两者都是新手,并且遇到了一些麻烦。我在 return PtWidg()
上引发了一个 AttributeError,但控制台没有给我任何超级有用的工作:
Traceback (most recent call last):
File "/Users/revascharf/Documents/COLLEGE WORK/SENIOR YEAR/touchscreenInterface/touchUI.py", line 30, in <module>
ptApp().run()
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/app.py", line 802, in run
root = self.build()
File "/Users/revascharf/Documents/COLLEGE WORK/SENIOR YEAR/touchscreenInterface/touchUI.py", line 26, in build
return PtWidg()
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/uix/widget.py", line 320, in __init__
Builder.apply(self, ignored_consts=self._kwargs_applied_init)
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/lang.py", line 1970, in apply
self._apply_rule(widget, rule, rule, ignored_consts=ignored_consts)
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/lang.py", line 2044, in _apply_rule
cls = Factory_get(cname)
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/factory.py", line 130, in __getattr__
raise AttributeError
AttributeError
Process finished with exit code 1
这是我的python文件,touchUI.py:
import kivy
import datetime
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
kivy.require('1.9.0')
from kivy.uix.image import Image
class SendButton(Button):
def on_press(self):
now = datetime.datetime.now()
self.text = 'minute is ' + str(now.minute)
class PtWidg(Widget):
pass
class ptApp(App):
def build(self):
return PtWidg()
if __name__ == '__main__':
ptApp().run()
这是我的 .kv 文件的内容,pt.kv:
#kivy 1.9.0
<sendButton>:
size: 40, 30
#pos: center_x + width / 4, center_y - height / 4
<PtWidg>:
Image:
center_x: root.width / 4
top: root.top - 50
source: 'SensoryWalkLogo.png'
height: db(50)
width: db(50)
sendButton:
center_x: root.width - root.width / 4
top: root.top - 50
text: 'Send minute to MSP430'
font_size: 40
真的,任何提示或技巧都会对我有很大帮助。谢谢!
您的代码存在以下错误:
- 你在 kv 文件中写了
sendButton
而不是 SendButton
。
- 同样在 kv 文件中,您写了
db(50)
而不是 dp(50)
。使用字符串 '50dp'
也是一个有效的选项。
我正在编写一些代码来为 Python/Kivy 中的触摸屏创建 UI。我对两者都是新手,并且遇到了一些麻烦。我在 return PtWidg()
上引发了一个 AttributeError,但控制台没有给我任何超级有用的工作:
Traceback (most recent call last):
File "/Users/revascharf/Documents/COLLEGE WORK/SENIOR YEAR/touchscreenInterface/touchUI.py", line 30, in <module>
ptApp().run()
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/app.py", line 802, in run
root = self.build()
File "/Users/revascharf/Documents/COLLEGE WORK/SENIOR YEAR/touchscreenInterface/touchUI.py", line 26, in build
return PtWidg()
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/uix/widget.py", line 320, in __init__
Builder.apply(self, ignored_consts=self._kwargs_applied_init)
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/lang.py", line 1970, in apply
self._apply_rule(widget, rule, rule, ignored_consts=ignored_consts)
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/lang.py", line 2044, in _apply_rule
cls = Factory_get(cname)
File "/Applications/Kivy.app/Contents/Resources/kivy/kivy/factory.py", line 130, in __getattr__
raise AttributeError
AttributeError
Process finished with exit code 1
这是我的python文件,touchUI.py:
import kivy
import datetime
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
kivy.require('1.9.0')
from kivy.uix.image import Image
class SendButton(Button):
def on_press(self):
now = datetime.datetime.now()
self.text = 'minute is ' + str(now.minute)
class PtWidg(Widget):
pass
class ptApp(App):
def build(self):
return PtWidg()
if __name__ == '__main__':
ptApp().run()
这是我的 .kv 文件的内容,pt.kv:
#kivy 1.9.0
<sendButton>:
size: 40, 30
#pos: center_x + width / 4, center_y - height / 4
<PtWidg>:
Image:
center_x: root.width / 4
top: root.top - 50
source: 'SensoryWalkLogo.png'
height: db(50)
width: db(50)
sendButton:
center_x: root.width - root.width / 4
top: root.top - 50
text: 'Send minute to MSP430'
font_size: 40
真的,任何提示或技巧都会对我有很大帮助。谢谢!
您的代码存在以下错误:
- 你在 kv 文件中写了
sendButton
而不是SendButton
。 - 同样在 kv 文件中,您写了
db(50)
而不是dp(50)
。使用字符串'50dp'
也是一个有效的选项。