我一直在寻找所有互联网上这个问题的解决方案,但我仍然找不到合适的解决方案。 有很多通用答案,但没有一个能解决我的问题..
我正在尝试使用kivy和python 3.6.4构建一个简单的CLOCK应用,但每次运行main.py
时都会收到此错误:
AttributeError:'super'对象没有属性' getattr '
MY MAIN“main.py”文件就是这样:
from kivy.app import App
from kivy.clock import Clock
from kivy.core.text import LabelBase
from kivy.core.window import Window
from kivy.utils import get_color_from_hex
from time import strftime
class ClockApp(App):
def on_start(self):
Clock.schedule_interval(self.update, 0)
def update(self, nap):
self.root.ids.time.text = strftime('[b]%H[/b]:%M:%S')
if __name__ == '__main__':
Window.clearcolor = get_color_from_hex('#101216')
LabelBase.register(name='Roboto',
fn_regular='Roboto-Thin.ttf',
fn_bold='Roboto-Medium.ttf')
ClockApp().run()
我的“clock.kv”文件就是这样:
<Label>:
font_name: 'Roboto'
font_size: 60
markup: True
BoxLayout:
orientation: 'vertical'
Label:
id: time
text: '[b]00[/b]:00:00'
这是我跑“main.py”时出现的错误
[INFO ] [Logger ] Record log in C:\Users\Alessandro\.kivy \logs\kivy_18-05-19_4.txt
[INFO ] [Kivy ] v1.10.0
[INFO ] [Python ] v3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
[INFO ] [Factory ] 194 symbols loaded
[INFO ] [Image ] Providers: img_tex, img_dds, img_sdl2, img_pil, img_gif (img_ffpyplayer ignored)
[INFO ] [Text ] Provider: sdl2
[INFO ] [OSC ] using <thread> for socket
[INFO ] [Window ] Provider: sdl2
[INFO ] [GL ] Using the "OpenGL" graphics system
[INFO ] [GL ] GLEW initialization succeeded
[INFO ] [GL ] Backend used <glew>
[INFO ] [GL ] OpenGL version <b'4.4.0 - Build 20.19.15.4549'>
[INFO ] [GL ] OpenGL vendor <b'Intel'>
[INFO ] [GL ] OpenGL renderer <b'Intel(R) HD Graphics 5500'>
[INFO ] [GL ] OpenGL parsed version: 4, 4
[INFO ] [GL ] Shading version <b'4.40 - Build 20.19.15.4549'>
[INFO ] [GL ] Texture max size <16384>
[INFO ] [GL ] Texture max units <32>
[INFO ] [Shader ] fragment shader: <b"WARNING: 0:7: '' : #version directive missing">
[INFO ] [Shader ] vertex shader: <b"WARNING: 0:7: '' : #version directive missing">
[INFO ] [Window ] auto add sdl2 input provider
[INFO ] [Window ] virtual keyboard not allowed, single mode, not docked
[INFO ] [Base ] Start application main loop
[INFO ] [Base ] Leaving application in progress...
Traceback (most recent call last):
File "kivy\properties.pyx", line 836, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:12509)
KeyError: 'time'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Alessandro\Desktop\Clock\main.py", line 24, in <module>
ClockApp().run()
File "C:\Users\Alessandro\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\app.py", line 828, in run
runTouchApp()
File "C:\Users\Alessandro\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 504, in runTouchApp
EventLoop.window.mainloop()
File "C:\Users\Alessandro\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 663, in mainloop
self._mainloop()
File "C:\Users\Alessandro\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\core\window\window_sdl2.py", line 405, in _mainloop
EventLoop.idle()
File "C:\Users\Alessandro\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\base.py", line 339, in idle
Clock.tick()
File "C:\Users\Alessandro\AppData\Local\Programs\Python\Python36-32\lib\site-packages\kivy\clock.py", line 581, in tick
self._process_events()
File "kivy\_clock.pyx", line 367, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7700)
File "kivy\_clock.pyx", line 397, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7577)
File "kivy\_clock.pyx", line 395, in kivy._clock.CyClockBase._process_events (kivy\_clock.c:7498)
File "kivy\_clock.pyx", line 167, in kivy._clock.ClockEvent.tick (kivy\_clock.c:3490)
File "C:\Users\Alessandro\Desktop\Clock\main.py", line 16, in update
self.root.ids.time.text = strftime('[b]%H[/b]:%M:%S')
File "kivy\properties.pyx", line 839, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:12654)
AttributeError: 'super' object has no attribute '__getattr__'
答案 0 :(得分:0)
.kv的结构看起来不正确,例如,观察到有很多根会导致您出现另一个问题,所以如果您想要一个回复来告诉您问题的原因,您应该改进缩进
相反,我会向您展示一个正确的.kv,其中根将是BoxLayout,而您的孩子是带有ID时间的标签:
<强> clock.kv 强>
<Label>:
font_name: 'Roboto'
font_size: 60
markup: True
BoxLayout:
orientation: 'vertical'
Label:
id: time
text: '[b]00[/b]:00:00'
<强> main.py 强>
from kivy.app import App
from kivy.clock import Clock
from kivy.core.text import LabelBase
from kivy.core.window import Window
from kivy.utils import get_color_from_hex
from time import strftime
class ClockApp(App):
def on_start(self):
Clock.schedule_interval(self.update, 0)
def update(self, *args):
self.root.ids.time.text = strftime('[b]%H[/b]:%M:%S')
if __name__ == '__main__':
Window.clearcolor = get_color_from_hex('#101216')
LabelBase.register(name='Roboto', fn_regular='Roboto-Thin.ttf', fn_bold='Roboto-Medium.ttf')
ClockApp().run()