<small id='5ioaR'></small><noframes id='5ioaR'>

    1. <tfoot id='5ioaR'></tfoot>

        <bdo id='5ioaR'></bdo><ul id='5ioaR'></ul>
    2. <i id='5ioaR'><tr id='5ioaR'><dt id='5ioaR'><q id='5ioaR'><span id='5ioaR'><b id='5ioaR'><form id='5ioaR'><ins id='5ioaR'></ins><ul id='5ioaR'></ul><sub id='5ioaR'></sub></form><legend id='5ioaR'></legend><bdo id='5ioaR'><pre id='5ioaR'><center id='5ioaR'></center></pre></bdo></b><th id='5ioaR'></th></span></q></dt></tr></i><div id='5ioaR'><tfoot id='5ioaR'></tfoot><dl id='5ioaR'><fieldset id='5ioaR'></fieldset></dl></div>
    3. <legend id='5ioaR'><style id='5ioaR'><dir id='5ioaR'><q id='5ioaR'></q></dir></style></legend>
    4. 将功能绑定到 Kivy 按钮

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

            <bdo id='qGqAr'></bdo><ul id='qGqAr'></ul>
          • <tfoot id='qGqAr'></tfoot>
                <tbody id='qGqAr'></tbody>

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

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

                本文介绍了将功能绑定到 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 按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:TextInput Kivy 的自动“焦点" 下一篇:Kivy:标签文本在 for 循环期间不更新

                相关文章

                最新文章

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

              2. <legend id='SgB0E'><style id='SgB0E'><dir id='SgB0E'><q id='SgB0E'></q></dir></style></legend>
              3. <tfoot id='SgB0E'></tfoot>

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

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