How to Load Kivy IDs Before Class Method is Initialized (Python with Kivy)

和自甴很熟 提交于 2020-07-22 04:01:08

问题


Goal: Successfully initialize a class method (create_button(), in my case) that references kv ids in its parameter from the __init__ method of the same class.

I'm receiving the following error:

  File "C:/Users/phili/scrollablelabelexample.py", line 33, in __init__
    self.create_button(self.ids.box_share)

  File "kivy\properties.pyx", line 839, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:12654)

AttributeError: 'super' object has no attribute '__getattr__'

Suspicion: I believe it is because my kv ids are not loaded before class is initialized

Question: How do I ensure kivy ids are loaded in advance?

Things I've tried:

  1. Building kv file earlier in code
  2. Use clock to delay initialization of create_button()
  3. Using @mainthread to let ids load first

Python Code:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.scrollview import ScrollView
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

class AnotherScreen(Screen):
    pass

class Sequence(Screen):
    pass

class ScreenManagement(ScreenManager):
    pass

class MainScreen(Screen):
    pass

class CleanScreen(BoxLayout):
    def __init__(self, **kwargs):
        super(CleanScreen, self).__init__(**kwargs)
        self.orientation = "vertical"
        self.create_button(self.ids.box_share)

    def create_button(self, box_share):
        top_button_share = 1.1
        top_label_share = 1.4

        for i in range(50):
            top_button_share -= .4
            top_label_share -= .4

            button_share = Button(pos_hint={"x": 0, "top": top_button_share}, size_hint_y=None, height=40)
            label_share = Label(text=str(i), pos_hint={"x": 0, "top": top_label_share}, size_hint_y=None)

            box_share.add_widget(button_share)
            box_share.add_widget(label_share)

presentation = Builder.load_file("garagemainexample.kv")

class MainApp(App):
    def build(self):
        return presentation 

if __name__ == "__main__":
    MainApp().run()

Kv code:

#: import FadeTransition kivy.uix.screenmanager.FadeTransition

ScreenManagement:
    transition: FadeTransition()
    MainScreen:
    Sequence:

<BigButton@Button>:
    font_size: 40
    size_hint: 0.5, 0.15
    color: 0,1,0,1 

<SmallNavButton@Button>:    
    font_size: 32
    size: 125, 50    
    color: 0,1,0,1

<MainScreen>:
    name: "main"
    FloatLayout:
        BigButton:
            on_release: app.root.current = "sequence"
            text: "Sequence"
            pos_hint: {"x":0.25, "top": 0.4} 

<CleanScreen>:
    ScrollView:
        GridLayout:
            id: box_share
            cols: 1
            size_hint_y: None
            spacing: 10
            padding: 10
            height: self.minimum_height
            canvas:
                Color:
                    rgb: 1, 0, 1
                Rectangle:
                    pos: self.pos
                    size: self.size

<Sequence>:
    name: "sequence"
    CleanScreen:
        id: cleanscreen

回答1:


The creation of the children of a widget occurs after the creation of the parent, in your case the ids are created after CleanScreen. So a possible solution is to call that method an instant after the constructor is called with Clock:

class CleanScreen(BoxLayout):
    def __init__(self, **kwargs):
        super(CleanScreen, self).__init__(**kwargs)
        self.orientation = "vertical"
        # You must call the method at the end of the constructor
        Clock.schedule_once(lambda *args:self.create_button(self.ids.box_share))


来源:https://stackoverflow.com/questions/48997181/how-to-load-kivy-ids-before-class-method-is-initialized-python-with-kivy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!