将功能绑定到Kivy按钮

时间:2014-04-17 07:34:14

标签: python function button bind kivy

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

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))

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

这是我的整个代码

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()

4 个答案:

答案 0 :(得分:7)

我认为任何答案都不是很明确。也没有解释问题是使用参数(按钮实例)调用on_press的回调,因此LoginScreen.auth必须在self之后接受参数:

def auth(self, button):
    print('button pressed:', instance)

问题是 on_press必须通过Button.bind给出,或者回调必须是函数,它可以是绑定方法,以及引用的文档其他答案和评论链接到ButtonbBhavior,表明OP在构造函数中使用on_press很好:

self.hello = Button(text="hello", on_press=self.auth)
如果auth如上所述,那么

会有效。

答案 1 :(得分:2)

如果您阅读Button documentation,则关键似乎是使用bind功能:

def callback(instance):
    print('The button <%s> is being pressed' % instance.text)

btn1 = Button(text='Hello world 1')
btn1.bind(on_press=callback)

答案 2 :(得分:2)

替换行

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

你的代码并使用它:

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

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

print "auth called"

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

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()

答案 3 :(得分:0)

我报告了一个在主类中动态创建并随后触发到单个侦听器中的按钮的示例:

class allMyApp(TabbedPanel):
    def __init__(self, **kwargs):
        super(allMyApp, self).__init__(**kwargs)

        #[...]

        for y in sorted(set(emissionYears)):
            btn = Button(text = y,
                         size_hint_y = None,
                         height = '48dp',
                         on_release = lambda btn: self.ids.choseEmissionDate.select(btn.text))
            btn.bind(on_press = self.sortByYear)
            self.ids.choseEmissionDate.add_widget(btn)

    def sortByYear(self, instance):
        year = instance.text
        print(year)