【问题标题】:python kivy AttributeError: 'super' object has no attribute '__getattr__'python kivy AttributeError:'super'对象没有属性'__getattr__'
【发布时间】:2016-08-09 08:48:45
【问题描述】:

对于一个项目,我正在尝试制作一个简单的 GUI,它显示一些基本信息(如时间、日期、公共交通信息、一些新闻等)。为此,我想要一个显示所有这些内容概览的主页每个主题的特殊页面都有详细的视图。 Python 与 Kivy 相结合似乎是最好/最简单的解决方案,看起来不错,而且工作起来也很简单。但是,我在显示时间时遇到了问题。当我尝试更新标签上显示时间的文本时,我得到一个AttributeError: 'super' object has no attribute '__getattr__',这并没有给我提供太多关于在哪里寻找解决方案的信息。谁能指出哪里出了问题?

main.py:

import feedparser
import time
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.anchorlayout import AnchorLayout
from kivy.properties import NumericProperty, ReferenceListProperty, ObjectProperty, StringProperty
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.clock import Clock

# Declare widgets
class DateTime(Widget):
    curtime = StringProperty('')

    def update(self, dt):
        self.curtime = time.strftime("%H:%M:%S")
        print self.curtime
        self.ids["kv_timelabel"].text = str(self.curtime)

# Declare all screens
class HomeScreen(Screen):
    obj_datetime = DateTime()
    Clock.schedule_interval(obj_datetime.update, 1.0 / 10.0)

class NewsScreen(Screen):
    pass

class PublicTransportScreen(Screen):
    pass

class TrafficScreen(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

class MirrorApp(App):
    def build(self):
        # Create the screen manager
        render = ScreenManagement()
        render.add_widget(HomeScreen(name='home'))
        render.add_widget(TrafficScreen(name='traffic'))
        render.add_widget(PublicTransportScreen(name='public_transport'))
        render.add_widget(NewsScreen(name='news'))
        return render

if __name__ == '__main__':
    MirrorApp().run()

mirror.kv:

#:kivy 1.9.0

<HomeScreen>:
    name: 'home'
    obj_datetime: kv_datetime

    Button:
        on_release: app.root.current = 'news'
        text: 'News'
        size_hint: 0.5,0.15
        font_size: 50
        pos_hint: {"left":1, "top":0.15}

    Button:
        on_release: app.root.current = 'public_transport'
        text: 'Public Transport'
        size_hint: 0.5,0.15
        font_size: 50
        pos_hint: {"left":1, "top":0.3}

    Button:
        on_release: app.root.current = 'traffic'
        text: 'Traffic'
        size_hint: 0.5,0.15
        font_size: 50
        pos_hint: {"left":1, "top":0.45}

    DateTime:
        id: kv_datetime
        center: self.parent.center

<NewsScreen>:
    name: 'news'

    Button:
        on_release: app.root.current = 'home'
        text: 'back to the home screen'
        font_size: 50

<PublicTransportScreen>:
    name: 'public_transport'

    Button:
        on_release: app.root.current = 'home'
        text: 'back to the home screen'
        font_size: 50

<TrafficScreen>:
    name: 'traffic'

    Button:
        on_release: app.root.current = 'home'
        text: 'back to the home screen'
        font_size: 50

<DateTime>:
    Label:
        id: kv_timelabel
        text:
        font_size: 70  
        center_x: self.parent.center_x
        center_y: self.parent.center_y

我在运行 main.py 时遇到的错误是(我认为这个错误的最后两行左右就足够了,但你永远不够彻底):

[INFO   ] [Logger      ] Record log in /home/matthias/.kivy/logs/kivy_16-04-17_97.txt
[INFO   ] [Kivy        ] v1.9.0
[INFO   ] [Python      ] v2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010]
[INFO   ] [Factory     ] 173 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_gif, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [OSC         ] using <multiprocessing> for socket
[INFO   ] [Window      ] Provider: sdl2(['window_egl_rpi'] ignored)
[INFO   ] [GL          ] OpenGL version <3.0 Mesa 11.0.2>
[INFO   ] [GL          ] OpenGL vendor <Intel Open Source Technology Center>
[INFO   ] [GL          ] OpenGL renderer <Mesa DRI Intel(R) Ivybridge Mobile >
[INFO   ] [GL          ] OpenGL parsed version: 3, 0
[INFO   ] [GL          ] Shading version <1.30>
[INFO   ] [GL          ] Texture max size <8192>
[INFO   ] [GL          ] Texture max units <16>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [ProbeSysfs  ] device match: /dev/input/event5
[INFO   ] [MTD         ] Read event from </dev/input/event5>
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
19:39:33
[INFO   ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "main.py", line 49, in <module>
     MirrorApp().run()
   File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 824, in run
 Exception in thread Thread-1:
     runTouchApp()
 Traceback (most recent call last):
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 487, in runTouchApp
     EventLoop.window.mainloop()
   File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
   File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 525, in mainloop
     self.run()
     self._mainloop()
   File "/usr/lib/python2.7/threading.py", line 763, in run
   File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 290, in _mainloop
     self.__target(*self.__args, **self.__kwargs)
     EventLoop.idle()
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 327, in idle
   File "/usr/lib/python2.7/dist-packages/kivy/input/providers/mtdev.py", line 197, in _thread_run
     Clock.tick()
   File "/usr/lib/python2.7/dist-packages/kivy/clock.py", line 483, in tick
     self._process_events()
     _device = Device(_fn)
   File "/usr/lib/python2.7/dist-packages/kivy/clock.py", line 615, in _process_events
   File "/usr/lib/python2.7/dist-packages/kivy/lib/mtdev.py", line 131, in __init__
     self._fd = os.open(filename, os.O_NONBLOCK | os.O_RDONLY)
 OSError: [Errno 13] Permission denied: '/dev/input/event5'

     event.tick(self._last_tick, remove)
   File "/usr/lib/python2.7/dist-packages/kivy/clock.py", line 374, in tick
     ret = callback(self._dt)
   File "main.py", line 19, in update
     self.ids["kv_timelabel"].text = str(self.curtime)
 KeyError: 'kv_timelabel'

当我用 self.ids.kv_timelabel.text = str(self.curtime) 替换 self.ids["kv_timelabel"].text = str(self.curtime) 时,我得到:

[INFO   ] [Logger      ] Record log in 
/home/matthias/.kivy/logs/kivy_16-04-17_98.txt
[INFO   ] [Kivy        ] v1.9.0
[INFO   ] [Python      ] v2.7.10 (default, Oct 14 2015, 16:09:02) 
[GCC 5.2.1 20151010]
[INFO   ] [Factory     ] 173 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_dds, img_gif, img_sdl2, img_pil (img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [OSC         ] using <multiprocessing> for socket
[INFO   ] [Window      ] Provider: sdl2(['window_egl_rpi'] ignored)
[INFO   ] [GL          ] OpenGL version <3.0 Mesa 11.0.2>
[INFO   ] [GL          ] OpenGL vendor <Intel Open Source Technology Center>
[INFO   ] [GL          ] OpenGL renderer <Mesa DRI Intel(R) Ivybridge Mobile >
[INFO   ] [GL          ] OpenGL parsed version: 3, 0
[INFO   ] [GL          ] Shading version <1.30>
[INFO   ] [GL          ] Texture max size <8192>
[INFO   ] [GL          ] Texture max units <16>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
[INFO   ] [ProbeSysfs  ] device match: /dev/input/event5
[INFO   ] [MTD         ] Read event from </dev/input/event5>
[INFO   ] [Base        ] Start application main loop
[INFO   ] [GL          ] NPOT texture support is available
19:42:01
[INFO   ] [Base        ] Leaving application in progress...
 Traceback (most recent call last):
   File "main.py", line 49, in <module>
 Exception in thread Thread-1:
 Traceback (most recent call last):
   File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
     self.run()
   File "/usr/lib/python2.7/threading.py", line 763, in run
     MirrorApp().run()
     self.__target(*self.__args, **self.__kwargs)
   File "/usr/lib/python2.7/dist-packages/kivy/app.py", line 824, in run
   File "/usr/lib/python2.7/dist-packages/kivy/input/providers/mtdev.py", line 197, in _thread_run
     runTouchApp()
     _device = Device(_fn)
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 487, in runTouchApp
   File "/usr/lib/python2.7/dist-packages/kivy/lib/mtdev.py", line 131, in __init__
     EventLoop.window.mainloop()
     self._fd = os.open(filename, os.O_NONBLOCK | os.O_RDONLY)
   File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 525, in mainloop
 OSError: [Errno 13] Permission denied: '/dev/input/event5'
     self._mainloop()

   File "/usr/lib/python2.7/dist-packages/kivy/core/window/window_sdl2.py", line 290, in _mainloop
     EventLoop.idle()
   File "/usr/lib/python2.7/dist-packages/kivy/base.py", line 327, in idle
     Clock.tick()
   File "/usr/lib/python2.7/dist-packages/kivy/clock.py", line 483, in tick
     self._process_events()
   File "/usr/lib/python2.7/dist-packages/kivy/clock.py", line 615, in _process_events
     event.tick(self._last_tick, remove)
   File "/usr/lib/python2.7/dist-packages/kivy/clock.py", line 374, in tick
     ret = callback(self._dt)
   File "main.py", line 19, in update
     self.ids.kv_timelabel.text = str(self.curtime)
   File "kivy/properties.pyx", line 720, in kivy.properties.ObservableDict.__getattr__ (kivy/properties.c:10938)
 AttributeError: 'super' object has no attribute '__getattr__'

请注意,当我删除错误中提到的行时,代码可以正常工作。

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    将 kv 文件的加载移到小部件之前。所以调用Builder.loadfile("mirror.kv"),然后声明你的类DateTimeHomeScreen(因为它使用DateTime)。您得到的关键错误可能是因为此小部件还没有具有该 id 的孩子。

    【讨论】:

    • 这确实修复了错误,但是它仍然不显示我运行它的时间。 main.py 中的代码现在看起来像这样(就在输入下方):render = Builder.load_file("mirror.kv") # Declare widgets class DateTime(Widget): def update(self, dt): self.ids.kv_timelabel.text = str(time.strftime("%H:%M:%S")) print time.strftime("%H:%M:%S") 时间显示在终端中(因此调用了更新函数,但标签仍然为空。
    • 我认为问题在于您更新了 HomeScreen 声明中的 obj_datetime 小部件,但您从未使用它。您的 HomeScreen 显示另一个 DateTime 实例,即在 kv 文件中声明的实例,它没有计划更新。要解决此问题,请在 HomeScreen 的 init 中添加一个计划更新的 DateTime 小部件,并将其从 kv 中删除或安排正确的小部件进行更新。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-17
    • 2020-09-03
    相关资源
    最近更新 更多