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

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

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

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

        从现有实例调用方法

        时间:2023-09-02
        <i id='TZfLJ'><tr id='TZfLJ'><dt id='TZfLJ'><q id='TZfLJ'><span id='TZfLJ'><b id='TZfLJ'><form id='TZfLJ'><ins id='TZfLJ'></ins><ul id='TZfLJ'></ul><sub id='TZfLJ'></sub></form><legend id='TZfLJ'></legend><bdo id='TZfLJ'><pre id='TZfLJ'><center id='TZfLJ'></center></pre></bdo></b><th id='TZfLJ'></th></span></q></dt></tr></i><div id='TZfLJ'><tfoot id='TZfLJ'></tfoot><dl id='TZfLJ'><fieldset id='TZfLJ'></fieldset></dl></div>

              <tbody id='TZfLJ'></tbody>

              1. <small id='TZfLJ'></small><noframes id='TZfLJ'>

                <tfoot id='TZfLJ'></tfoot>

                • <bdo id='TZfLJ'></bdo><ul id='TZfLJ'></ul>
                • <legend id='TZfLJ'><style id='TZfLJ'><dir id='TZfLJ'><q id='TZfLJ'></q></dir></style></legend>
                • 本文介绍了从现有实例调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我对面向对象编程的理解有点不稳定,所以如果你有任何链接可以帮助解释这些概念,我会很高兴看到它们!

                  My understanding of Object Orientated Programming is a little shaky so if you have any links that would help explain the concepts it would be great to see them!

                  我已经稍微缩短了代码.基本原则是我有一个以主 Controller 类的实例开始的游戏.当游戏打开时,Popup 类被打开.事件发生如下:

                  I've shortened the code somewhat. The basic principle is that I have a game that starts with an instance of the main Controller class. When the game is opened the Popup class is opened. The events happens as follows:

                  1. 点击弹窗上的开始按钮
                  2. 方法 start_click() 运行
                  3. 调用Controller实例中的start_game()方法
                  4. 这又将原始控制器实例中的游戏状态更改为真"

                  我的问题在于第 3 步.我收到的错误消息是:

                  My problem is with step 3. The error message I get is:

                  TypeError: unbound method start_game() must be called with Controller 
                  instance as first argument (got nothing instead)
                  

                  我想 StartPopUp 类中需要对 Controller 类进行一些引用.但我不太明白如何创建该参考?

                  I guess there needs to be some reference to the Controller class in the StartPopUp class. But I don't quite understand how to create that reference?

                  import kivy
                  kivy.require('1.8.0')
                  
                  from kivy.app import App
                  from kivy.uix.widget import Widget
                  from kivy.clock import Clock
                  from kivy.properties import BooleanProperty, NumericProperty, ObjectProperty
                  from kivy.uix.popup import Popup
                  from kivy.lang import Builder
                  
                  Builder.load_string('''
                  <StartPopUp>            
                      size_hint: .2, .2
                      auto_dismiss: False
                      title: 'Welcome'
                      Button:
                          text: 'Play'
                          on_press: root.start_click()
                          on_press: root.dismiss()
                  
                  ''')
                  
                  class StartPopUp(Popup):
                  
                      def __init__(self, **kw):
                          super(StartPopUp, self).__init__(**kw)
                  
                      def start_click(self):
                          Controller.start_game()                    
                  
                  class Controller(Widget):
                      playing_label = BooleanProperty(False)          #Intitial phase of game is off
                  
                      def __init__(self, **kw):
                          super(Controller, self).__init__(**kw)        
                  
                      def start_popup(self, dt):    
                          sp = StartPopUp()
                          sp.open()
                  
                      def start_game(self):
                          self.playing_label = True
                          print self.playing_label   
                  
                  class MoleHuntApp(App):
                  
                      def build(self):
                          game = Controller()
                          Clock.schedule_once(game.start_popup, 1)
                          return game
                  
                  if __name__ == '__main__':
                      MoleHuntApp().run() 
                  

                  提前致谢!

                  推荐答案

                  可以这样传递实例

                  class StartPopUp(Popup):
                  
                      def __init__(self, controller, **kw):
                          super(StartPopUp, self).__init__(**kw)
                          self.controller = controller
                  
                      def start_click(self):
                          self.controller.start_game()
                  

                  在控制器中

                  def start_popup(self, dt):    
                      sp = StartPopUp(self)
                      sp.open()
                  

                  这篇关于从现有实例调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在kivy项目中隐藏python代码文件和其他相关文 下一篇:如何为 android 调试 Kivy/Kivymd 应用程序?

                  相关文章

                  最新文章

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

                • <tfoot id='Arr6p'></tfoot>

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

                    <legend id='Arr6p'><style id='Arr6p'><dir id='Arr6p'><q id='Arr6p'></q></dir></style></legend>

                      • <bdo id='Arr6p'></bdo><ul id='Arr6p'></ul>