• <legend id='hkLFP'><style id='hkLFP'><dir id='hkLFP'><q id='hkLFP'></q></dir></style></legend>
      <tfoot id='hkLFP'></tfoot>
        <bdo id='hkLFP'></bdo><ul id='hkLFP'></ul>
    1. <small id='hkLFP'></small><noframes id='hkLFP'>

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

      1. .py 和 .kv 交互中的 Kivy 类

        时间:2023-10-08

          <tfoot id='OKog5'></tfoot>
          <legend id='OKog5'><style id='OKog5'><dir id='OKog5'><q id='OKog5'></q></dir></style></legend>

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

                <i id='OKog5'><tr id='OKog5'><dt id='OKog5'><q id='OKog5'><span id='OKog5'><b id='OKog5'><form id='OKog5'><ins id='OKog5'></ins><ul id='OKog5'></ul><sub id='OKog5'></sub></form><legend id='OKog5'></legend><bdo id='OKog5'><pre id='OKog5'><center id='OKog5'></center></pre></bdo></b><th id='OKog5'></th></span></q></dt></tr></i><div id='OKog5'><tfoot id='OKog5'></tfoot><dl id='OKog5'><fieldset id='OKog5'></fieldset></dl></div>
                  本文介绍了.py 和 .kv 交互中的 Kivy 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我没有完全理解 python 文件中的类和 kivy 语言是如何交互的.我目前正在修改 Kivy 的 Showcase 示例以增加我的理解.

                  I'm not fully grasping how classes in a python file and the kivy language interact. I'm currently modifying the Showcase example of Kivy to increase my understanding.

                  • main.py
                  • showcase.kv
                  • data/screens/test.kv
                  class Testy(BoxLayout):
                      ttext = 'Bla'
                      #other code
                  
                  class ShowcaseScreen(Screen):
                      fullscreen = BooleanProperty(False)
                  
                      def add_widget(self, *args):
                          if 'content' in self.ids:
                              return self.ids.content.add_widget(*args)
                          return super(ShowcaseScreen, self).add_widget(*args)
                  
                  
                  class ShowcaseApp(App):
                      #code
                  

                  test.kv

                  <Testy>:
                      orientation: 'vertical'
                  
                      Label:
                          text: root.ttext
                  
                  ShowcaseScreen:
                      name: 'LearnKanji'
                      fullscreen: True
                      #Some ref to <Testy>
                  

                  问题

                  我想将我的代码放在 Testy() 中,因为我计划编写大量代码来针对单个屏幕中发生的情况,我不想让 ShowcaseScreen() 变得混乱,因为 ShowcaseScreen 也意味着用于其他屏幕.

                  Question

                  I want to have my code in Testy(), because I'm planning to write a lot of code aimed at what happens in that single screen and I don't want to clutter ShowcaseScreen(), because ShowcaseScreen is also meant for other screens.

                  为了澄清,在 test.kv 中我指的是变量 ttext.如果我没有创建类 Testy() 我会这样说:

                  To clarify, in test.kv I'm referring to the variable ttext. If I didn't make the class Testy() I would have put it as follows:

                  ShowcaseScreen:
                      name: 'LearnKanji'
                      fullscreen: True
                      BoxLayout:
                          text: root.ttext
                  

                  然后在 main.py 我需要放

                  And then in main.py I would need to put

                  class ShowcaseScreen(Screen):
                      ttext = 'Bla'
                  

                  但是,由于除了 Testy 之外的许多屏幕都使用 ShowcaseScreen() 类,因此将所有屏幕的所有代码都放在这里会变得一团糟.因此,我希望每个屏幕的代码在一个单独的类中.我认为这对于更大的代码项目来说是一种更好的方式.

                  However since many screens besides Testy use the class ShowcaseScreen(), putting all the code for all screens in here would make it too much of a mess. Therefore I want the code per screen in a separate class. I think this is a better way for bigger code projects.

                  那么在test.kv文件中调用ShowcaseScreen:后怎么引用呢?这样我就可以将用于 Testy 的代码放在 Testy() 中,并使我的文件和类更有条理.

                  So how do I refer to after calling ShowcaseScreen: in the test.kv file? So that I can put the code meant for Testy in Testy() and keep my files and classes more organized.

                  推荐答案

                  您可以使用ScreenManager.看看这里,我更改了您的一些代码以提供使用 ScreenManager 将每个屏幕的代码分离到它们自己的类中的示例.如果这有问题,请告诉我.

                  You can use the ScreenManager. Have a look here, I changed some of your code to provide an example of using the ScreenManager to seperate out the code for each screen into their own classes. If there are problems with this let me know.

                  ma​​in.py:

                  from kivy.app import App
                  from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
                  from kivy.properties import ObjectProperty
                  
                  
                  class Testy(Screen):
                  
                      ttext = 'Screen 2'
                      #other code
                  
                  class ScreenTwo(Screen):
                      pass
                  
                  class Manager(ScreenManager):
                  
                      testy = ObjectProperty(None)
                      screen_2 = ObjectProperty(None)
                  
                  
                  class ShowcaseApp(App):
                  
                      def build(self):
                          return Manager(transition=FadeTransition())
                  
                  if __name__ == "__main__":
                      ShowcaseApp().run()
                  

                  kv 文件:

                  <Testy>:
                      BoxLayout:
                          Button:
                              text: root.ttext
                              on_press: root.manager.current = "screen_two"
                  
                  <ScreenTwo>:
                      BoxLayout:
                          Button:
                              text: "Screen 1"
                              on_press: root.manager.current = "testy_screen"
                  
                  
                  <Manager>:
                      id: screen_manager
                  
                      testy: testy
                      screen_2: screen_2
                  
                      Testy:
                          id: testy
                          name: 'testy_screen'
                          manager: screen_manager
                  
                      ScreenTwo:
                          id: screen_2
                          name: 'screen_two'
                          manager: screen_manager
                  

                  这篇关于.py 和 .kv 交互中的 Kivy 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:AttributeError:“MainRouter"对象没有属性“_disab 下一篇:AttributeError: 'super' object has no attribute '

                  相关文章

                  最新文章

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

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

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