[英]Bind function to Kivy button
I'm trying to bind the following function to a Button
in 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()
I've tried 我试过了
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?
我究竟做错了什么?
here is my whole code 这是我的整个代码
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()
I don't think any of the answers are very clear. 我不认为任何答案都很清楚。 Neither explains that problem is that the callback given to
on_press
gets called with a parameter, the instance of button, so LoginScreen.auth
must accept a parameter after the self
: 这两个问题都没有解释为
on_press
的回调是通过参数(按钮的实例)调用的,所以LoginScreen.auth
必须在self
之后接受一个参数:
def auth(self, button):
print('button pressed:', instance)
The problem is not that on_press
must be given via Button.bind
or that the callback must be a function, it can be a bound method, and the docs cited by other answer and by comments link to ButtonbBhavior
which indicates that OP use of on_press
in constructor was fine: 问题不在于
on_press
必须通过Button.bind
给出,或者回调必须是一个函数,它可以是一个绑定方法,其他答案引用的文档和评论链接到ButtonbBhavior
,表明OP使用on_press
构造函数很好:
self.hello = Button(text="hello", on_press=self.auth)
would have worked if auth
had been as described above. 如果
auth
如上所述,那会有效。
If you read the Button documentation , the key seems to be to use the bind
function: 如果你阅读Button文档 ,关键似乎是使用
bind
函数:
def callback(instance):
print('The button <%s> is being pressed' % instance.text)
btn1 = Button(text='Hello world 1')
btn1.bind(on_press=callback)
Replace line 替换线
self.hello = Button(text="hello", on_press=lambda a:self.auth())
of your code and use this : 您的代码并使用此:
self.hello = Button(text="hello", on_press=lambda a:self.auth())
Also add below line in auth function to see if its called :) 在auth函数中添加以下行,看看它是否被调用:)
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()
I report an example of buttons created dynamically within the main class, and then triggered into a single listener: 我报告了在主类中动态创建的按钮的示例,然后触发到单个侦听器:
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.