将函数绑定到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
我这里举个例子,说明如何在主类里动态创建按钮,然后通过一个统一的监听器来处理这些按钮的点击事件:
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)
2
如果你查看一下按钮的文档,你会发现关键在于使用bind
这个函数:
def callback(instance):
print('The button <%s> is being pressed' % instance.text)
btn1 = Button(text='Hello world 1')
btn1.bind(on_press=callback)
10
我觉得之前的回答都不太清楚。没有一个解释清楚问题所在,就是传给 on_press
的回调函数会带一个参数,这个参数是按钮的实例,所以 LoginScreen.auth
这个函数必须在 self
后面接受一个参数:
def auth(self, button):
print('button pressed:', instance)
问题并不是说 on_press
必须通过 Button.bind
来设置,也不是说回调函数必须是一个普通函数,它可以是一个绑定的方法。而其他回答和评论中提到的文档链接到了 ButtonBehavior
,这说明原作者在构造函数中使用 on_press
是没问题的:
self.hello = Button(text="hello", on_press=self.auth)
如果 auth
按照上面描述的那样写,就能正常工作了。
1
把你代码中的这一行替换成:
self.hello = Button(text="hello", on_press=lambda a:self.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()