问题描述
我正在尝试在控制台中打印登录凭据,但是如果该功能未显示错误,那么凭据也不会打印。我想将check_data_login函数绑定到kv文件中定义的“拍摄”按钮,函数在py文件中定义。我无法使用ID绑定它。它显示错误,表明该对象不存在。 这是我的.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()
这是我的.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,0.5
icon_left_color:1,1
ScreenManager:
id: sm
Screen:
id:s1
name:"s1"
manager:sm
Fitimage:
source:"bg4.jpg"
MDToolbar:
md_bg_color:1,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"
spacing:"12dp"
size_hint_x:0.85
canvas:
Color:
rgba: 1,1
RoundedRectangle:
pos: self.pos
size: self.size
radius: [10,10,10]
WhiteLabel:
text:"Welcome to My GUI!!"
text_color:1,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
color_active:1,1
FieldRound:
id:'password'
icon_left:"key"
hint_text:"Password"
normal_color:1,1
Widget:
size_hint_y:None
height:"12dp"
MDFillRoundFlatButton:
id:'bu'
text:"Shoot"
text_color:1,1
md_bg_color:247/255,171/255,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
md_bg_color:1,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
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'
我该如何解决这个问题
解决方法
.py
和.kv
文件中几乎没有问题。在kv
文件的id
文本字段中,FieldRound
不应包含引号。并且在on_release
事件的shoot
按钮中,app.check_data_login
缺少()
。因此,更正后的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,0.5
icon_left_color:1,1
ScreenManager:
id: sm
Screen:
id:s1
name:"s1"
manager:sm
FitImage:
source:"bg4.jpg"
MDToolbar:
md_bg_color:1,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"
spacing:"12dp"
size_hint_x:0.85
canvas:
Color:
rgba: 1,1
RoundedRectangle:
pos: self.pos
size: self.size
radius: [10,10,10]
WhiteLabel:
text:"Welcome to My GUI!!"
text_color:1,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
color_active:1,1
FieldRound:
id: password
icon_left:"key"
hint_text:"Password"
normal_color:1,1
Widget:
size_hint_y:None
height:"12dp"
MDFillRoundFlatButton:
id:'bu'
text:"Shoot"
text_color:1,1
md_bg_color:247/255,171/255,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
md_bg_color:1,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
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'
更正后的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()