• <small id='LCrhr'></small><noframes id='LCrhr'>

      <bdo id='LCrhr'></bdo><ul id='LCrhr'></ul>

        <tfoot id='LCrhr'></tfoot>
        <legend id='LCrhr'><style id='LCrhr'><dir id='LCrhr'><q id='LCrhr'></q></dir></style></legend>
        <i id='LCrhr'><tr id='LCrhr'><dt id='LCrhr'><q id='LCrhr'><span id='LCrhr'><b id='LCrhr'><form id='LCrhr'><ins id='LCrhr'></ins><ul id='LCrhr'></ul><sub id='LCrhr'></sub></form><legend id='LCrhr'></legend><bdo id='LCrhr'><pre id='LCrhr'><center id='LCrhr'></center></pre></bdo></b><th id='LCrhr'></th></span></q></dt></tr></i><div id='LCrhr'><tfoot id='LCrhr'></tfoot><dl id='LCrhr'><fieldset id='LCrhr'></fieldset></dl></div>

        如何在类方法初始化之前加载 Kivy ID(Python with K

        时间:2023-10-09
        <i id='HojFZ'><tr id='HojFZ'><dt id='HojFZ'><q id='HojFZ'><span id='HojFZ'><b id='HojFZ'><form id='HojFZ'><ins id='HojFZ'></ins><ul id='HojFZ'></ul><sub id='HojFZ'></sub></form><legend id='HojFZ'></legend><bdo id='HojFZ'><pre id='HojFZ'><center id='HojFZ'></center></pre></bdo></b><th id='HojFZ'></th></span></q></dt></tr></i><div id='HojFZ'><tfoot id='HojFZ'></tfoot><dl id='HojFZ'><fieldset id='HojFZ'></fieldset></dl></div>
      1. <legend id='HojFZ'><style id='HojFZ'><dir id='HojFZ'><q id='HojFZ'></q></dir></style></legend>
          <bdo id='HojFZ'></bdo><ul id='HojFZ'></ul>
            • <tfoot id='HojFZ'></tfoot>
                <tbody id='HojFZ'></tbody>

                  <small id='HojFZ'></small><noframes id='HojFZ'>

                  本文介绍了如何在类方法初始化之前加载 Kivy ID(Python with Kivy)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  目标:成功初始化一个类方法(在我的例子中是 create_button()),该方法在 __init__ 的参数中引用了 kv id同一个类的方法.

                  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.

                  我收到以下错误:

                    File "C:/Users/phili/scrollablelabelexample.py", line 33, in __init__
                      self.create_button(self.ids.box_share)
                  
                    File "kivyproperties.pyx", line 839, in kivy.properties.ObservableDict.__getattr__ (kivyproperties.c:12654)
                  
                  AttributeError: 'super' object has no attribute '__getattr__'
                  

                  怀疑:我相信这是因为我的 kv id 在类初始化之前没有加载

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

                  问题:如何确保提前加载 kivy id?

                  我尝试过的事情:

                  1. 在代码前面构建 kv 文件
                  2. 使用时钟延迟create_button()的初始化
                  3. 使用@mainthread让ids先加载
                  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 代码:

                  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代码:

                  #: 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
                  

                  推荐答案

                  小部件的子小部件的创建发生在父小部件的创建之后,在您的情况下,id 是在 CleanScreen 之后创建的.所以一个可能的解决方案是在构造函数用 Clock 调用后立即调用该方法:

                  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))
                  

                  这篇关于如何在类方法初始化之前加载 Kivy ID(Python with Kivy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:嵌套在 BoxLayout 内的 ScreenManager 不可见 下一篇:为什么根小部件的窗口大小不同?

                  相关文章

                  最新文章

                    <bdo id='3YwZL'></bdo><ul id='3YwZL'></ul>
                • <tfoot id='3YwZL'></tfoot>
                  1. <i id='3YwZL'><tr id='3YwZL'><dt id='3YwZL'><q id='3YwZL'><span id='3YwZL'><b id='3YwZL'><form id='3YwZL'><ins id='3YwZL'></ins><ul id='3YwZL'></ul><sub id='3YwZL'></sub></form><legend id='3YwZL'></legend><bdo id='3YwZL'><pre id='3YwZL'><center id='3YwZL'></center></pre></bdo></b><th id='3YwZL'></th></span></q></dt></tr></i><div id='3YwZL'><tfoot id='3YwZL'></tfoot><dl id='3YwZL'><fieldset id='3YwZL'></fieldset></dl></div>

                  2. <small id='3YwZL'></small><noframes id='3YwZL'>

                    <legend id='3YwZL'><style id='3YwZL'><dir id='3YwZL'><q id='3YwZL'></q></dir></style></legend>