将功能绑定到Kivy按钮

use*_*797 5 python bind function button kivy

我正在尝试将以下函数绑定到ButtonKivy中.

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()
Run Code Online (Sandbox Code Playgroud)

我试过了

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

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

这是我的整个代码

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()
Run Code Online (Sandbox Code Playgroud)

Oli*_*ver 7

我不认为任何答案都很清楚.这两个问题都没有解释on_press为使用参数,按钮实例调用的回调,所以LoginScreen.auth必须在以下后接受一个参数self:

def auth(self, button):
    print('button pressed:', instance)
Run Code Online (Sandbox Code Playgroud)

问题在于on_press必须通过Button.bind或者回调必须是一个函数,它可以是一个绑定的方法,其他答案引用的文档和注释链接ButtonbBhavior指示on_press构造函数中的OP使用是好的:

self.hello = Button(text="hello", on_press=self.auth)
Run Code Online (Sandbox Code Playgroud)

如果auth已经如上所述将会有效.


paa*_*tra 1

更换线路

self.hello = Button(text="hello", on_press=lambda a:self.auth())
Run Code Online (Sandbox Code Playgroud)

你的代码并使用这个:

self.hello = Button(text="hello", on_press=lambda a:self.auth())
Run Code Online (Sandbox Code Playgroud)

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

print "auth called"
Run Code Online (Sandbox Code Playgroud)

并且执行特定任务的方法有很多种。上面的代码将以最小的努力修复您的代码,但是如果您想以其他方式执行此操作。只需使用下面的代码即可。

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()
Run Code Online (Sandbox Code Playgroud)

  • 我看不出前两行有什么区别 (11认同)