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

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

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

    2. Kivy:访问不同类的方法

      时间:2023-10-09
        <tbody id='Y6TST'></tbody>

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

          <tfoot id='Y6TST'></tfoot>

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

                问题描述

                假设我正在构建一个井字游戏(因为它与结构非常相似)我希望结果显示在带有新游戏按钮的弹出窗口中,并且我希望此弹出窗口允许我访问设置(使用另一个按钮)并更改它们,始终保持在弹出窗口中,然后离开并最终关闭它并开始一个新游戏.

                Let's pretend I am building a tic-tac-toe game (becouse it's pretty similar as a structure) I want the result to be shown in a popup, with a new game button, and I want this popup to let me access settings (with another button) and change them, always staying within the popup, then leave and finally close it and start a new game.

                我希望我可以保持有序,因此有一个单独的弹出窗口类,我可以在其中构建我的自定义弹出窗口.

                I wish i could keep things ordered and therefore have a separate popup class where i can build my custom popup.

                很明显,我有 newgame 方法和 reset 方法作为我的游戏网格类的方法.另一方面,更改设置的方法是在自定义设置类上

                I have the newgame method and reset method as method of my game-grid class, as obvious. Methods for changing settings are, on the other hand, on a custom settings class

                在设计弹出类时,如何将其按钮(例如新游戏)绑定到完全不同的类中包含的方法?我查看了一些 kv 示例,他们通常使用 root.blabla.method 来访问位于同一棵树的不同位置(在 .kv 文件中)的方法,但这里我试图达到的方法超出了树!

                While designing the popup class how can I bind it's buttons (e.g new game) to methods that are contained on a completly different class? I've looked on some kv examples and they usually use root.blabla.method to acces a method that is in a different position of the same tree (in the .kv file) but here the methods I am trying to reach are out of the tree!

                我会尝试放一些示例代码以使其更清晰

                I'll try to put some example code to make it more clear

                class Settings():
                
                    def changeSettings(self):
                        ....
                
                class GmeGrid(GridLayout):
                
                    def newGame(self):
                        ....
                
                    def reset(self):
                        ...
                
                class customPopup(Popup):
                
                    pass
                

                然后,在一个 .kv 文件上,我希望我可以将一些弹出按钮绑定到 newGame 并更改设置方法

                Then, on a .kv file I wish I could bind some popup's buttons to the newGame and change settings methods

                这里的问题是我应该将 popop 类上的按钮绑定到完全不同的类的方法上,但我不知道该怎么做(尤其是在 .kv 文件上)

                The problem here is that I should bind buttons on the popop class to the mothods of completly different class and I don't know how to to that (on the .kv file especially)

                推荐答案

                只要widget已经完全实例化并添加到widget树中,就可以使用self.parent来访问widget的父母.不过,您可能会考虑传递引用:

                As long as the widget has been fully instantiated and added to the widget tree, you can use self.parent to access the widget's parent. You might look into passing references instead though:

                Builder.load_string('''
                <CustomPopup>:
                    BoxLayout:
                        orientation: 'vertical'
                        # some settings stuff here
                        BoxLayout:
                            orientation: 'horizontal'
                            Button:
                                text: 'New Game'
                                on_press: root.do_new_game()
                ''')
                
                class CustomPopup(Popup):
                    settings_widget = ObjectProperty()
                    new_game = ObjectProperty()
                
                    def do_new_game(self):
                        self.settings_widget.some_property = some_value
                        self.dismiss()
                        self.new_game()
                
                p = CustomPopup(settings_widget=my_widget, new_game=mygame.newGame)
                p.open()
                

                这比假设父级具有设置更好,因为如果您更改保存设置的位置,您只需要更改一个引用.

                This is better that assuming the parent has the settings, because if you change where you keep the settings, you just need to change one reference.

                这篇关于Kivy:访问不同类的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何使用 Kivy 的 FileChooser 选择多个文件? 下一篇:如何使用按钮退出 Kivy 应用程序

                相关文章

                最新文章

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

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

              4. <tfoot id='jfGqc'></tfoot>