将功能绑定到Kivy按钮 [英] Bind function to Kivy button

查看:846
本文介绍了将功能绑定到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()

我试过了:

$ $ p $ class Foo():
def initUI(self):
self.add_widget (Button(text =Auth User and Password,on_press = self.auth))

但是这个不起作用。这是我的整个代码

  from kivy.uix.popup从kivy.app导入Popup 
从kivy.uix.gridlayout导入App
从kivy.uix.label导入GridLayout
从kivy.uix中导入标签
.textinput从kivy.uix.button导入TextInput
从kivy.uix.boxlayout导入Button
从kivy.uix.stacklayout导入BoxLayout
导入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 =按钮(text =hello,on_pr ess = self.auth)
self.add_widget(self.hello)

def auth(self):
如果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函数中添加下面的行来查看它是否被调用: p>

  printauth called

以及执行特定任务的方法很多。以最小的努力修复你的代码,但是如果你想以另一种方式来做。只需使用下面的代码即可。

  from kivy.uix.popup导入Popup 
from kivy.app import App
从kivy.uix.gridlayout导入GridLayout
from kivy.uix.label import从$ kivy.uix.textinput标签
从kivy.uix.button导入TextInput
导入Button
从kivy.uix.boxlayout从kivy.uix.stacklayout导入BoxLayout
导入StackLayout


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.aut h)
self.add_widget(self.hello)

def auth(self,instance):
printauth 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()

$ b $ class MyApp(App):
def build(self ):
返回LoginScreen()

$ b $如果__name__ =='__main__':
MyApp()。run()


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

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

解决方案

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

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按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆