<bdo id='rmdmZ'></bdo><ul id='rmdmZ'></ul>
    1. <tfoot id='rmdmZ'></tfoot>
      <legend id='rmdmZ'><style id='rmdmZ'><dir id='rmdmZ'><q id='rmdmZ'></q></dir></style></legend>

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

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

        Python Kivy:如何在按钮单击时调用函数?

        时间:2023-06-08
          <tbody id='OZItl'></tbody>
              <bdo id='OZItl'></bdo><ul id='OZItl'></ul>

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

                  <i id='OZItl'><tr id='OZItl'><dt id='OZItl'><q id='OZItl'><span id='OZItl'><b id='OZItl'><form id='OZItl'><ins id='OZItl'></ins><ul id='OZItl'></ul><sub id='OZItl'></sub></form><legend id='OZItl'></legend><bdo id='OZItl'><pre id='OZItl'><center id='OZItl'></center></pre></bdo></b><th id='OZItl'></th></span></q></dt></tr></i><div id='OZItl'><tfoot id='OZItl'></tfoot><dl id='OZItl'><fieldset id='OZItl'></fieldset></dl></div>
                  <legend id='OZItl'><style id='OZItl'><dir id='OZItl'><q id='OZItl'></q></dir></style></legend>
                  <tfoot id='OZItl'></tfoot>
                  本文介绍了Python Kivy:如何在按钮单击时调用函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对使用 kivy 库很陌生.

                  i'm pretty new at using kivy library.

                  我有一个 app.py 文件和一个 app.kv 文件,我的问题是我无法在按下按钮时调用函数.

                  I have an app.py file and an app.kv file , my problem is that I can't call a function on button press.

                  app.py:

                  import kivy
                  from kivy.app import App
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.uix.button import Button
                  
                  class Launch(BoxLayout):
                      def __init__(self, **kwargs):
                          super(Launch, self).__init__(**kwargs)
                  
                      def say_hello(self):
                          print "hello"
                  
                  
                  class App(App):
                      def build(self):
                          return Launch()
                  
                  
                  if __name__ == '__main__':
                      App().run()
                  

                  app.kv:

                  #:kivy 1.9.1
                  
                  <Launch>:
                      BoxLayout:
                          Button:
                              size:(80,80)
                              size_hint:(None,None)
                              text:"Click me"
                              on_press: say_hello
                  

                  推荐答案

                  Mode:.kv

                  很简单,say_hello 属于 Launch 类,所以要在 .kv 文件中使用它,你必须编写 <代码>root.say_hello.请注意,say_hello 是您要执行的函数,因此您不必忘记 () ---> root.say_hello().

                  Mode:.kv

                  It's very simple, say_hello belongs to the Launch class so in order to use it in your .kv file you have to write root.say_hello. Note that say_hello is a function that you want to execute so you don't have to forget the () ---> root.say_hello().

                  另外,如果 say_helloApp 类中,您应该使用 App.say_hello() 因为它属于应用程序.(注意:即使你的 App 类是 class MyFantasicApp(App): 它总是 App.say_hello()app.say_hello() 我不记得了,抱歉).

                  Also, if say_hello were in App class you should use App.say_hello() because it belongs to the app. (Note: even if your App class were class MyFantasicApp(App): it would always be App.say_hello() or app.say_hello() I don't remember, sorry).

                  #:kivy 1.9.1
                  
                  <Launch>:
                      BoxLayout:
                          Button:
                              size:(80,80)
                              size_hint:(None,None)
                              text:"Click me"
                              on_press: root.say_hello()
                  

                  模式:.py

                  你可以绑定函数.

                  from kivy.uix.button import Button # You would need futhermore this
                  class Launch(BoxLayout):
                      def __init__(self, **kwargs):
                          super(Launch, self).__init__(**kwargs)
                          mybutton = Button(
                                              text = 'Click me',
                                              size = (80,80),
                                              size_hint = (None,None)
                                            )
                          mybutton.bind(on_press = self.say_hello) # Note: here say_hello doesn't have brackets.
                          Launch.add_widget(mybutton)
                  
                      def say_hello(self):
                          print "hello"
                  

                  为什么要使用 bind?对不起,不知道.但是您在 kivy 指南中使用了它.

                  Why use bind? Sorry, no idea. But you it's used in the kivy guide.

                  这篇关于Python Kivy:如何在按钮单击时调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

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

                        <bdo id='TAMkZ'></bdo><ul id='TAMkZ'></ul>
                          <tbody id='TAMkZ'></tbody>

                        <tfoot id='TAMkZ'></tfoot>