Kivyのadd_widget時のparentのタイミングについてご質問があります。
以下のmain.py,main.kvファイルで構成時にエラーが発生します。
画面サイズに関わらず、画像を比率で綺麗に表示させたいところにあります。
parentサから算出しているのですがadd_widgetのタイミングでは、parentはないので
どのように解決したらいいかがわかりません。
main.py
python
1#-*- coding: utf-8 -*- 2from kivy.app import App 3from kivy.uix.image import Image 4from kivy.uix.floatlayout import FloatLayout 5 6 7class Charactor(Image): 8 pass 9 10 11class MainFrame(FloatLayout): 12 def __init__(self, **kwargs): 13 super(MainFrame, self).__init__(**kwargs) 14 self.add_widget(Charactor()) 15 16class MainApp(App): 17 def __init__(self, **kwargs): 18 super(MainApp, self).__init__(**kwargs) 19 20 def build(self): 21 root = MainFrame() 22 return root 23 24MainApp().run()
main.kv
#:kivy 1.10 #-*- coding: utf-8 -*- <MainFrame>: <Image>: allow_stretch:True keep_ratio: True nocache:True <Charactor>: pos_hint:{'center_x':.5,'y':0 } size_hint:1,0.95 source:'./img/ss2.png' size_hint_max:self.height*self.image_ratio,self.parent.width/self.image_ratio
エラー内容
... 25: size_hint:1,0.95 26: source:'./img/ss2.png' >> 27: size_hint_max:self.height*self.image_ratio,self.parent.width/self.image_ratio ... AttributeError: 'NoneType' object has no attribute 'width'
parentがNoneのため取得できない状況のようです。
表示される方法
代わりにKVの中に入れて、add_widgetを外した場合は表示されます。
Kvファイルでの反映タイミングにadd_widget版でも同様に処理させればいけるのではないかと考えています。
main.py
python
1#-*- coding: utf-8 -*- 2from kivy.app import App 3from kivy.uix.image import Image 4from kivy.uix.floatlayout import FloatLayout 5 6 7class Charactor(Image): 8 pass 9 10 11class MainFrame(FloatLayout): 12 def __init__(self, **kwargs): 13 super(MainFrame, self).__init__(**kwargs) 14 15class MainApp(App): 16 def __init__(self, **kwargs): 17 super(MainApp, self).__init__(**kwargs) 18 19 def build(self): 20 root = MainFrame() 21 return root 22 23MainApp().run()
main.kv
#:kivy 1.10 #-*- coding: utf-8 -*- <MainFrame>: Charactor: <Image>: allow_stretch:True keep_ratio: True nocache:True <Charactor>: pos_hint:{'center_x':.5,'y':0 } size_hint:1,0.95 source:'./img/ss2.png' size_hint_max:self.height*self.image_ratio,self.parent.width/self.image_ratio
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/17 23:36
2018/07/18 07:50
2018/07/18 08:15 編集