2017-04-17 12 views
0

pythonやkivyを使用して別のクラスのラベル/ IDにアクセスし、以下のエラーを回避するにはどうすればよいですか? 私は他の投稿を検索しましたが、私はrootの方が別のクラスを参照することができないことを発見しました... Hower、私はそれを行うための他の方法を知らない。あなたが私を助けてくれることを願います。 ありがとうございます。画面サイズ設定Kivy、python:ルートクラス(FaceRecApp)から外部/子クラスにアクセスする方法?

FaceRec.py

ライブラリ

from kivy.app import App 
from kivy.uix.screenmanager import ScreenManager,Screen 
from kivy.uix.widget import Widget 
from kivy.uix.label import Label 
from kivy.uix.settings import SettingsWithSidebar 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.actionbar import ActionBar 
from kivy.logger import Logger 
from kivy.config import Config 

Config.set('graphics', 'width', '850') 
Config.set('graphics', 'height', '850') 

のホームスクリーンクラス

class ScreenManagement(ScreenManager): 
    pass 

class HomeScreen(Screen): 
    pass 

class HomeActionBar(ActionBar): 
    pass 

class TitleLabel(Label): 
    pass 

class StatusBoxLayout(BoxLayout): 
    pass 

class ErrorsBoxLayout(BoxLayout): 
    pass 

メインクラス

class FaceRecApp(App): 
    def build(self): 
     root = HomeScreen() 
     Logger.info('FaceRec.py: FaceRec.kv loaded') 
     self.settings_cls = MySettingsWithSidebar 
     Logger.info('FaceRec.py: MySettingsWithSidebar loaded') 

     status = root.ids.StatusBoxLayout.status 
     status.font_size = float(self.config.get('General', 'font_size')) 
     label = root.ids.label 
     label.font_size = float(self.config.get('General', 'font_size')) 

     return root 

    def build_config(self, config): 
     config.setdefaults('General', {'text': 'Default Hello','font_size': 20}) 

    def build_settings(self, settings): 
     settings.add_json_panel('General', self.config, 'Settings.json') 

    def on_config_change(self, config, section, key, value): 
     Logger.info("FaceRec.py: App.on_config_change: {0}, {1}, {2}, {3}".format(config, section, key, value)) 

     if section == "General": 
      if key == "text": 
       self.root.ids.label.text = value 
      elif key == 'font_size': 
       self.root.ids.label.font_size = float(value) 
       self.root.ids.StatusBoxLayout.status = float(value) 

    def close_settings(self, settings): 
     Logger.info("FaceRec.py: App.close_settings: {0}".format(settings)) 
     super(FaceRecApp, self).close_settings(settings) 

サイドバーの設定

class MySettingsWithSidebar(SettingsWithSidebar): 
    def on_close(self): 
     Logger.info('FaceRec.py: MySettingsWithSidebar.on_close') 

    def on_config_change(self, config, section, key, value): 
     Logger.info(
      "FaceRec.py: MySettingsWithSidebar.on_config_change: " 
     "{0}, {1}, {2}, {3}".format(config, section, key, value)) 

実行

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

FaceRec.kv

ScreenManagement: 
HomeScreen: 

<HomeActionBar>: 
    id: HomeActionBar 
    pos_hint: {'bottom':0} 
    ActionView: 
     use_separator: True 
     ActionPrevious: 
      title: 'Home' 
      with_previous: False 
     ActionOverflow: 
     ActionButton: 
      text: 'Settings' 
      icon: 'settings.png' 
      background_down: 'settings.png' 
      on_release: 
       app.open_settings() 

<TitleLabel>: 
    id: TitleLabel 
    text: '[b]FaceRec[/b] - [i]The Face Recognition Project[/i]' 
    color: 0.0,0.3,1,1 
    markup: True 
    font_size: 38 

<StatusBoxLayout>: 
    orientation: 'horizontal' 
    #padding: 100 
    Label: 
     id: status 
     text: 'Status: ' 
     font_size: 20 
    Label: 
     id: status_value 
     text: 'Error' 
     font_size: 20 
     color: 1,0,0,1 

<ErrorsBoxLayout>: 
    id: ErrorsBoxLayout 
    orientation: 'horizontal' 
    #padding: 100 
    Label: 
     id: errors 
     text: 'Errors No: ' 
     font_size: 20 
    Label: 
     id: errors_value 
     text: '...' 
     font_size: 20 

<HomeScreen>: 
    id: HomeScreen 
    BoxLayout: 
     orientation: 'vertical' 
     HomeActionBar: 
     TitleLabel: 

     BoxLayout: 
      cols: 2 
      orientation: 'vertical' 

      StatusBoxLayout: 
      ErrorsBoxLayout: 

    Label: 
     id: label 
     text: 'Does it run?' 

Settings.json

[ 
    { 
     "type": "string", 
     "title": "Label caption", 
     "desc": "Choose the text that appears in the label", 
     "section": "General", 
     "key": "text" 
    }, 
    { 
     "type": "numeric", 
     "title": "Label font size", 
     "desc": "Choose the font size the label", 
     "section": "General", 
     "key": "font_size" 
    } 
] 

私はラベル変数へのアクセスを試みた(FaceRecApp、構築およびon_config_change機能)とプログラムが正常に実行されました。設定でラベルのフォントサイズを変更すると、ホームスクリーンに変更が適用されました。 )(ビルドで

:その後、私は次のコード行を追加しました

status = root.ids.StatusBoxLayout.status 
status.font_size = float(self.config.get('General', 'font_size')) 

とon_config_changeに():

self.root.ids.StatusBoxLayout.status = float(value) 

結果がエラーとなっている:

[INFO    ] [Logger  ] Record log in C:\***\.kivy\logs\kivy_17-04-17_109.txt 
[INFO    ] [Kivy  ] v1.9.1 
[INFO    ] [Python  ] v2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:53:40) [MSC v.1500 64 bit (AMD64)] 
[INFO    ] [Factory  ] 179 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 <thread> for socket 
[INFO    ] [Window  ] Provider: sdl2 
[INFO    ] [GL   ] GLEW initialization succeeded 
[INFO    ] [GL   ] OpenGL version <4.5.0 NVIDIA 376.53> 
[INFO    ] [GL   ] OpenGL vendor <NVIDIA Corporation> 
[INFO    ] [GL   ] OpenGL renderer <GeForce ***/PCIe/SSE2> 
[INFO    ] [GL   ] OpenGL parsed version: 4, 5 
[INFO    ] [GL   ] Shading version <4.50 NVIDIA> 
[INFO    ] [GL   ] Texture max size <16384> 
[INFO    ] [GL   ] Texture max units <32> 
[INFO    ] [Window  ] auto add sdl2 input provider 
[INFO    ] [Window  ] virtual keyboard not allowed, single mode, not docked 
[INFO    ] [FaceRec.py ] FaceRec.kv loaded 
[INFO    ] [FaceRec.py ] MySettingsWithSidebar loaded 
Traceback (most recent call last): 
    File "C:/***/PycharmProjects/face_recognition_2/FaceRec.py", line 114, in <module> 
    FaceRecApp().run() 
    File "C:\Python27\lib\site-packages\kivy\app.py", line 802, in run 
    root = self.build() 
    File "C:/***/PycharmProjects/face_recognition_2/FaceRec.py", line 65, in build 
    status = root.ids.StatusBoxLayout.status 
    File "kivy\properties.pyx", line 757, in  kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:11882) 
AttributeError: 'super' object has no attribute '__getattr__' 

Process finished with exit code 1 

私はすでにこの投稿(および他の多くの記事)を読んでいます: How to access id/widget of different class from a kivy file (.kv)? 私のプログラムはまだ実行されていません。

また、ご支援いただきありがとうございます。

答えて

0

StatusBoxLayoutにルートクラス(HomeScreen)のIDを入力するのを忘れました。このIDは、self.root.ids.StatusBoxLayoutとしてaccsessできるように、ルートクラスで指定する必要があります。
だから、それをホームスクリーンの中にそのID与える:

<HomeScreen>: 
    id: HomeScreen 
    BoxLayout: 
     orientation: 'vertical' 
     HomeActionBar: 
     TitleLabel: 

     BoxLayout: 
      cols: 2 
      orientation: 'vertical' 

      StatusBoxLayout: 
       id: StatusBoxLayout 
      ErrorsBoxLayout: 
       id: ErrorsBoxLayout 

をそして、あなたはそれでいる間、あなたにも、あまりにもErrorsBoxLayoutへのid、またはあなたがidでアクセスする必要のある他のクラスを与えるかもしれません。

今、あなたはstatus = root.ids.StatusBoxLayout.statusを割り当てるときに、代わりにこのようにそれを割り当てます。後でAppクラスでは、あなたがself.rootを取得または設定するため

status = self.root.ids.StatusBoxLayout.ids.status 

あなたはそれにselfを付加する必要があります。しかし、あなたはクラス属性としてrootを定義しませんでした。ステータスがStatusBoxLayout
だからあなたのビルド方法のIDがあるので
そして第二にidsは、次のようになります。

def build(self): 
    self.root = HomeScreen() 
    Logger.info('FaceRec.py: FaceRec.kv loaded') 
    self.settings_cls = MySettingsWithSidebar 
    Logger.info('FaceRec.py: MySettingsWithSidebar loaded') 

    status = self.root.ids.StatusBoxLayout.ids.status 
    status.font_size = float(self.config.get('General', 'font_size')) 
    label = self.root.ids.label 
    label.font_size = float(self.config.get('General', 'font_size')) 

    return self.root 
関連する問題