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

  • <legend id='MRohu'><style id='MRohu'><dir id='MRohu'><q id='MRohu'></q></dir></style></legend>
        <bdo id='MRohu'></bdo><ul id='MRohu'></ul>

      1. <tfoot id='MRohu'></tfoot>

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

        Kivy 外部规则继承 2

        时间:2023-10-10

          <tfoot id='WsWd6'></tfoot>
        1. <legend id='WsWd6'><style id='WsWd6'><dir id='WsWd6'><q id='WsWd6'></q></dir></style></legend>

            <i id='WsWd6'><tr id='WsWd6'><dt id='WsWd6'><q id='WsWd6'><span id='WsWd6'><b id='WsWd6'><form id='WsWd6'><ins id='WsWd6'></ins><ul id='WsWd6'></ul><sub id='WsWd6'></sub></form><legend id='WsWd6'></legend><bdo id='WsWd6'><pre id='WsWd6'><center id='WsWd6'></center></pre></bdo></b><th id='WsWd6'></th></span></q></dt></tr></i><div id='WsWd6'><tfoot id='WsWd6'></tfoot><dl id='WsWd6'><fieldset id='WsWd6'></fieldset></dl></div>
              <bdo id='WsWd6'></bdo><ul id='WsWd6'></ul>
              • <small id='WsWd6'></small><noframes id='WsWd6'>

                  <tbody id='WsWd6'></tbody>

                  本文介绍了Kivy 外部规则继承 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  作为后续问题:

                  • Kivy 外部规则继承
                  • 使用 add_widget() 继承 Kivy 规则李>

                  main.py

                  import os
                  from kivy.app import App
                  from kivy.uix.button import Button
                  from kivy.uix.stacklayout import StackLayout
                  from kivy.properties import ObjectProperty
                  
                  
                  class FancyButton(Button):
                      imp = ObjectProperty(None)
                  
                  
                  class Important(StackLayout):
                  
                      font_kanji = os.path.join('fonts', 'TakaoPMincho.ttf')
                  
                      def NoInspiration(self, smile):
                          print("Received: {}".format(smile))
                  
                      def AddFancy(self):
                          print(self.ids)
                          temp = FancyButton(text='f', imp = self)
                          self.ids.boxy.add_widget(temp)
                  
                  
                  class TestApp(App):
                      def build(self):
                          pass
                  
                  if __name__ == '__main__':
                      TestApp().run()
                  

                  test.kv

                  #:kivy 1.9.0
                  
                  <FancyButton>:
                      font_name: self.imp.font_kanji  # ERROR
                      on_release: self.imp.NoInspiration(':)')  # WORKS
                  
                  
                  <Important>:
                      id: imp
                  
                      BoxLayout:
                          id: boxy
                          orientation: 'vertical'
                  
                          FancyButton:
                              text: "smiley"
                              imp: root
                  
                          Button:
                              text: "add fancy"
                              on_release: imp.AddFancy()
                  
                  
                  BoxLayout:
                      Important
                  

                  在上面的示例中,'on_release: self.imp.NoInspiration(':)')' 有效,因为 FancyButton 具有 'imp: root'.

                  In the above example 'on_release: self.imp.NoInspiration(':)')' works because FancyButton has 'imp: root'.

                  但是 'font_name: self.imp.font_kanji' 不起作用并给出错误:

                  However 'font_name: self.imp.font_kanji' doesn't work and gives the error:

                  AttributeError: 'NoneType' 对象没有属性 'font_kanji'

                  AttributeError: 'NoneType' object has no attribute 'font_kanji'

                  我的猜测是这样做的原因是 on_release 发生在所有小部件都直接加载和 font_name 之后,所以没有'imp:root'.

                  My guess is the reason for this is that on_release takes place after all widgets are loaded and font_name directly, so without having 'imp: root'.

                  我也试过了:

                  font_kanji = StringProperty(os.path.join('fonts', 'TakaoPMincho.ttf'))
                  

                  ,但无济于事.

                  如何让 font_name 引用 font_kanji?我应该使用全局吗?如果是,您将如何在 Python 中设置可以在 .kv 中访问的全局?

                  How do I get font_name refer to font_kanji? Should I use global? If yes, how would you set a global in Python which can be accessed in the .kv?

                  (如果我将 global 放在 font_Kanji 前面并删除 .kv 文件中的 'self.imp' 我会收到错误:NameError: name 'font_kanji' is not defined")

                  (If I put global in front of font_Kanji and remove 'self.imp' in the .kv file I get the error: " NameError: name 'font_kanji' is not defined")

                  推荐答案

                  你的猜测是正确的:当你的按钮被创建时,它的 imp 属性是 None.解决这个问题的方法是观察 imp 属性并在其处理程序中设置 font_name 的值:

                  Your guess is right: when your button is created its imp property is None. One to go around this would be to observe imp property and set the value of font_name in its handler:

                  class FancyButton(Button):
                      imp = ObjectProperty(None)
                  
                      def on_imp(self, obj, imp):
                          if imp:
                              self.font_name = imp.font_kanji
                  

                  这样字体是在 imp 属性用适当的 Important 实例初始化之后设置的.这种方法的缺点是Instance.font_kanji的变化不会触发FancyButton.font_name的变化.

                  This way the font is set after imp property is initialzied with proper Important instance. The dissadvantage of this method is that changes of Instance.font_kanji won't trigger changes of FancyButton.font_name.

                  如果你想绑定两个属性,那么你必须从 Instance.font_kanji 端调用 bind 函数(因为我们想对其更改做出反应)以动态创建 FancyButton 实例:

                  If you want to have both properties binded, then you have to call bind funtion from Instance.font_kanji side (since we want to react to its changes) for dynamically created FancyButton instances:

                  class Important(StackLayout):
                      font_kanji = os.path.join('fonts', 'TakaoPMincho.ttf')
                  
                      def NoInspiration(self, smile):
                          print("Received: {}".format(smile))
                  
                      def AddFancy(self):
                          temp = FancyButton(text='f', imp = self)
                          self.bind(font_kanji=temp.setter('font_name'))
                          self.ids.boxy.add_widget(temp)
                  

                  kv语言定义的接口可以直接绑定:

                  Interface defined in the kv language can do the binding directly:

                  <Important>:
                      id: imp
                  
                      BoxLayout:
                          id: boxy
                          orientation: 'vertical'
                  
                          FancyButton:
                              text: "smiley"
                              font_name: root.font_kanji
                              imp: root
                  
                          Button:
                              text: "add fancy"
                              on_release: imp.AddFancy()
                  ''')
                  

                  这篇关于Kivy 外部规则继承 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:用于简单计算的 Kivy 输入值 下一篇:找不到 TableRecycleGridLayout 来修复 Kivy 代码

                  相关文章

                  最新文章

                    <bdo id='aK3Bd'></bdo><ul id='aK3Bd'></ul>
                1. <small id='aK3Bd'></small><noframes id='aK3Bd'>

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

                    <tfoot id='aK3Bd'></tfoot>
                  1. <legend id='aK3Bd'><style id='aK3Bd'><dir id='aK3Bd'><q id='aK3Bd'></q></dir></style></legend>