[英]how to bind function to button in kv file
Iam trying to print the login credentials in the console,But if the function didn't show error also the credentials are not printing.I wanted to bind the check_data_login function to the 'shoot' button which is defined in kv file the function is defined in py file. Iam trying to print the login credentials in the console,But if the function didn't show error also the credentials are not printing.I wanted to bind the check_data_login function to the 'shoot' button which is defined in kv file the function is defined在 py 文件中。 im unable to bind it using ID.
我无法使用 ID 绑定它。 It is showing the error saying the object doesn't exist.
它显示错误说 object 不存在。 this is my.py file
这是我的.py 文件
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class DemoApp(MDApp):
def check_data_login(self,obj):
username = self.username.text
password = self.password.text
print(username)
print(password)
DemoApp().run()
and this is my.kv file这是 my.kv 文件
<WhiteLabel@MDLabel>
size_hint_y:None
height:self.texture_size[1]
theme_text_color:"Custom"
<FieldRound@MDTextFieldRound>
pos_hint:{"center_x":0.5}
normal_color:0,1,1,0.5
icon_left_color:1,1,1,1
ScreenManager:
id: sm
Screen:
id:s1
name:"s1"
manager:sm
FitImage:
source:"bg4.jpg"
MDToolbar:
md_bg_color:1,0,156/255,1
title:"My Demo App"
pos_hint:{"center_x":0.5,"center_y":0.95}
elevation:11
BoxLayout:
orientation:"vertical"
size_hint_y:None
height:self.minimum_height
pos_hint:{"center_x":0.5,"center_y":0.5}
padding:"24dp","24dp","24dp","24dp"
spacing:"12dp"
size_hint_x:0.85
canvas:
Color:
rgba: 1,1,1,1
RoundedRectangle:
pos: self.pos
size: self.size
radius: [10,10,10,10]
WhiteLabel:
text:"Welcome to My GUI!!"
text_color:1,0,156/255,1
bold:True
halign:"center"
font_style:"Caption"
font_size:25
Widget:
size_hint_y:None
height:"12dp"
WhiteLabel:
text:"We just need your details."
font_style:"H1"
font_size:20
WhiteLabel:
text:"Please enter carefully"
font_style:"H1"
font_size:20
Widget:
size_hint_y:None
height:"12dp"
FieldRound:
id:'username'
icon_left:"mail"
hint_text:"Username"
normal_color:1,1,1,1
color_active:1,1,1,1
FieldRound:
id:'password'
icon_left:"key"
hint_text:"Password"
normal_color:1,1,1,1
color_active:1,1,1,1
Widget:
size_hint_y:None
height:"12dp"
MDFillRoundFlatButton:
id:'bu'
text:"Shoot"
text_color:1,0,156/255,1
md_bg_color:247/255,171/255,1,1
pos_hint:{"center_x":0.5}
on_release:
app.check_data_login
sm.current = 's2'
MDFlatButton:
elevation_normal: 12
text:"Signup"
text_color:1,1,1,1
md_bg_color:1,0,156/255,1
pos_hint:{"center_x":0.3,"center_y":0.1}
on_release:
sm.current = 's2'
MDFlatButton:
elevation_normal: 12
text:"Login"
text_color:1,1,1,1
md_bg_color:1,0,156/255,1
pos_hint:{"center_x":0.7,"center_y":0.1}
on_release:
sm.current = 's2'
Screen:
id:s2
name:"s2"
manager:sm
BoxLayout:
Image:
source:"rajat.jpg"
opacity:0.5
MDRaisedButton:
text:"button"
pos_hint:{"center_x":0.5,"center_y":0.2}
on_press:
sm.current ='s1'
how can i solve this issue我该如何解决这个问题
There are few issues both in .py
and .kv
files. .py
和.kv
文件中几乎没有问题。 In the kv
file the id
of FieldRound
text fields should not have the quotes.在
kv
文件中, FieldRound
文本字段的id
不应包含引号。 And also in on_release
event of shoot
button the app.check_data_login
missing the ()
.而且在
shoot
按钮的on_release
事件中app.check_data_login
缺少()
。 So the corrected kv
file should be as below:所以修正后的
kv
文件应该如下:
<WhiteLabel@MDLabel>
size_hint_y:None
height:self.texture_size[1]
theme_text_color:"Custom"
<FieldRound@MDTextFieldRound>
pos_hint:{"center_x":0.5}
normal_color:0,1,1,0.5
icon_left_color:1,1,1,1
ScreenManager:
id: sm
Screen:
id:s1
name:"s1"
manager:sm
FitImage:
source:"bg4.jpg"
MDToolbar:
md_bg_color:1,0,156/255,1
title:"My Demo App"
pos_hint:{"center_x":0.5,"center_y":0.95}
elevation:11
BoxLayout:
orientation:"vertical"
size_hint_y:None
height:self.minimum_height
pos_hint:{"center_x":0.5,"center_y":0.5}
padding:"24dp","24dp","24dp","24dp"
spacing:"12dp"
size_hint_x:0.85
canvas:
Color:
rgba: 1,1,1,1
RoundedRectangle:
pos: self.pos
size: self.size
radius: [10,10,10,10]
WhiteLabel:
text:"Welcome to My GUI!!"
text_color:1,0,156/255,1
bold:True
halign:"center"
font_style:"Caption"
font_size:25
Widget:
size_hint_y:None
height:"12dp"
WhiteLabel:
text:"We just need your details."
font_style:"H1"
font_size:20
WhiteLabel:
text:"Please enter carefully"
font_style:"H1"
font_size:20
Widget:
size_hint_y:None
height:"12dp"
FieldRound:
id: username
icon_left:"mail"
hint_text:"Username"
normal_color:1,1,1,1
color_active:1,1,1,1
FieldRound:
id: password
icon_left:"key"
hint_text:"Password"
normal_color:1,1,1,1
color_active:1,1,1,1
Widget:
size_hint_y:None
height:"12dp"
MDFillRoundFlatButton:
id:'bu'
text:"Shoot"
text_color:1,0,156/255,1
md_bg_color:247/255,171/255,1,1
pos_hint:{"center_x":0.5}
on_release:
app.check_data_login()
sm.current = 's2'
MDFlatButton:
elevation_normal: 12
text:"Signup"
text_color:1,1,1,1
md_bg_color:1,0,156/255,1
pos_hint:{"center_x":0.3,"center_y":0.1}
on_release:
sm.current = 's2'
MDFlatButton:
elevation_normal: 12
text:"Login"
text_color:1,1,1,1
md_bg_color:1,0,156/255,1
pos_hint:{"center_x":0.7,"center_y":0.1}
on_release:
sm.current = 's2'
Screen:
id:s2
name:"s2"
manager:sm
BoxLayout:
Image:
source:"rajat.jpg"
opacity:0.5
MDRaisedButton:
text:"button"
pos_hint:{"center_x":0.5,"center_y":0.2}
on_press:
sm.current ='s1'
And the corrected py
file should be as below with the corrections:更正后的
py
文件应如下所示,并进行更正:
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class DemoApp(MDApp):
def check_data_login(self):
username = self.root.ids.username.text
password = self.root.ids.password.text
print(username)
print(password)
DemoApp().run()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.