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

    <tfoot id='Pj4VL'></tfoot>
      <bdo id='Pj4VL'></bdo><ul id='Pj4VL'></ul>

      <legend id='Pj4VL'><style id='Pj4VL'><dir id='Pj4VL'><q id='Pj4VL'></q></dir></style></legend>
      1. 将功能绑定到 Kivy 按钮

        Bind function to Kivy button(将功能绑定到 Kivy 按钮)
        <tfoot id='RakXM'></tfoot>
        • <bdo id='RakXM'></bdo><ul id='RakXM'></ul>

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

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

                  本文介绍了将功能绑定到 Kivy 按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试将以下函数绑定到 Kivy 中的 Button.

                  I'm trying to bind the following function to a Button in Kivy.

                  def auth(self):
                      print(self.username)
                      if self.username == "Hendricko":
                          print("self.username == Hendricko")
                          popup = Popup(title="success",
                              content=Label(text="Howdy !"),
                              size=(100, 100),
                              size_hint=(0.3, 0.3),
                              auto_dismiss=False)
                          popup.open()
                  

                  我试过了

                  class Foo():
                     def initUI(self):
                      self.add_widget(Button(text="Auth User and Password", on_press=self.auth))
                  

                  但这不起作用.我做错了什么?

                  but this doesn't work. What am I doing wrong?

                  这是我的全部代码

                  from kivy.uix.popup import Popup
                  from kivy.app import App
                  from kivy.uix.gridlayout import GridLayout
                  from kivy.uix.label import Label
                  from kivy.uix.textinput import TextInput
                  from kivy.uix.button import Button
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.uix.stacklayout import StackLayout
                  
                  
                  class LoginScreen(GridLayout):
                      def __init__(self, **kwargs):
                          super(LoginScreen, self).__init__(**kwargs)
                          self.cols = 2
                          self.row = 2
                          self.add_widget(Label(text='User Name'))
                          self.username = TextInput(multiline=False)
                          self.add_widget(self.username)
                          self.add_widget(Label(text='password'))
                          self.password = TextInput(password=True, multiline=False)
                          self.add_widget(self.password)
                          self.hello = Button(text="hello", on_press=self.auth)
                          self.add_widget(self.hello)
                  
                      def auth(self):
                          if self.username == "Hendricko":
                              popup = Popup(title="success",
                                  content=Label(text="Howdy !"),
                                  size=(100, 100),
                                  size_hint=(0.3, 0.3),
                                  auto_dismiss=False)
                              popup.open()
                  
                  
                  class MyApp(App):
                      def build(self):
                          return LoginScreen()
                  
                  
                  if __name__ == '__main__':
                      MyApp().run()
                  

                  推荐答案

                  换行

                  self.hello = Button(text="hello", on_press=lambda a:self.auth())
                  

                  您的代码并使用它:

                  self.hello = Button(text="hello", on_press=lambda a:self.auth())
                  

                  还在 auth 函数中添加以下行以查看它是否被调用:)

                  Also add below line in auth function to see if its called :)

                  print "auth called"
                  

                  并且有很多方法可以执行特定任务.上面的代码将以最小的努力修复您的代码,但是如果您想以另一种方式进行.只需使用下面的代码.

                  and There are many ways to perform a particular task .Above code will be to fix your code in minimum effort , However If you would like to do it in another way . Just use code below .

                  from kivy.uix.popup import Popup
                  from kivy.app import App
                  from kivy.uix.gridlayout import GridLayout
                  from kivy.uix.label import Label
                  from kivy.uix.textinput import TextInput
                  from kivy.uix.button import Button
                  from kivy.uix.boxlayout import BoxLayout
                  from kivy.uix.stacklayout import StackLayout
                  
                  
                  class LoginScreen(GridLayout):
                      def __init__(self, **kwargs):
                          super(LoginScreen, self).__init__(**kwargs)
                          self.cols = 2
                          self.row = 2
                          self.add_widget(Label(text='User Name'))
                          self.username = TextInput(multiline=False)
                          self.add_widget(self.username)
                          self.add_widget(Label(text='password'))
                          self.password = TextInput(password=True, multiline=False)
                          self.add_widget(self.password)
                          self.hello = Button(text="hello")
                          self.hello.bind(on_press=self.auth)
                          self.add_widget(self.hello)
                  
                      def auth(self,instance):
                          print "auth called"
                          if self.username == "Hendricko":
                              popup = Popup(title="success",
                                  content=Label(text="Howdy !"),
                                  size=(100, 100),
                                  size_hint=(0.3, 0.3),
                                  auto_dismiss=False)
                              popup.open()
                  
                  
                  class MyApp(App):
                      def build(self):
                          return LoginScreen()
                  
                  
                  if __name__ == '__main__':
                      MyApp().run()
                  

                  这篇关于将功能绑定到 Kivy 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)
                  <tfoot id='MjfVA'></tfoot>
                    <tbody id='MjfVA'></tbody>
                      1. <i id='MjfVA'><tr id='MjfVA'><dt id='MjfVA'><q id='MjfVA'><span id='MjfVA'><b id='MjfVA'><form id='MjfVA'><ins id='MjfVA'></ins><ul id='MjfVA'></ul><sub id='MjfVA'></sub></form><legend id='MjfVA'></legend><bdo id='MjfVA'><pre id='MjfVA'><center id='MjfVA'></center></pre></bdo></b><th id='MjfVA'></th></span></q></dt></tr></i><div id='MjfVA'><tfoot id='MjfVA'></tfoot><dl id='MjfVA'><fieldset id='MjfVA'></fieldset></dl></div>

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

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