2016-04-17 35 views
0

爲一個項目我試圖做一個簡單的圖形用戶界面,顯示一些基本信息(如時間,日期,公共交通信息,一些新聞等)做到這一點,我想要一個主頁,顯示所有這些東西的概述和每個主題的詳細視圖的特殊頁面。 Python與Kivy的結合似乎是最好的/最簡單的解決方案,看起來不錯,而且工作起來很簡單。但是,我顯示時間有問題。當我嘗試更新顯示時間的標籤上的文字時,我得到一個AttributeError: 'super' object has no attribute '__getattr__',這並不能提供有關在哪裏尋找解決方案的信息。任何人都可以指出什麼是錯的?python kivy AttributeError:'超'對象沒有屬性'__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__' 

請注意,當我刪除它在錯誤中提到的代碼行時,代碼工作得很好。

回答

0

將小部件之前的kv文件加載。因此請撥打Builder.loadfile("mirror.kv"),然後申報您的課程DateTimeHomeScreen(因爲它使用DateTime)。你得到的關鍵錯誤可能是因爲這個小部件沒有這個id的孩子。

+0

這確實修復了錯誤,但它仍然不顯示我運行它的時間。 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」)'顯示時間在終端中(所以更新函數被調用,但標籤仍然保持爲空 – Liveshort

+0

我認爲問題在於您更新HomeScreen聲明中的obj_datetime小部件,但您從不使用它。您的HomeScreen顯示不同的DateTime實例,在kv文件中聲明的那個,不是計劃更新的,爲了解決這個問題,在HomeScreen的__init__中添加一個計劃更新的DateTime小部件,並將其從kv中移除或者安排正確的小部件約會。 – Leva7

相關問題