在kivy python中单击按钮时创建标签和文本输入
在 kivy 中制作登录屏幕时。我想创建一个注册按钮,单击该按钮时将在屏幕上添加一个额外的 TextInput 框以进行密码验证。这是主文件的代码:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.core.window import Window
Window.size=(600,600)
Builder.load_file('loginLayoutCSS.kv')
class loginLayout(Widget):
def signIn(self):
pass
def signUp(self):
pass
class Login(App):
def build(Self):
Window.clearcolor=(0,1,0.8,.1)
return loginLayout()
if __name__=='__main__':
Login().run()
.kv 文件的代码:
#:kivy 2.1.0
#:import utils kivy.utils
<TextInput>
size_hint:(0.5,0.5)
pos_hint:{'center_x':0.5}
background_normal:''
<Label>
font_size:32
background_normal:''
color:utils.get_color_from_hex('#0c4160')
<RoundedButton@Button>
background_color:(0,0,0,0)
background_normal:''
canvas.before:
Color:
rgba:(48/255,84/255,150/255,1)
RoundedRectangle:
size:self.size
pos:self.pos
radius:[45]
<loginLayout>
BoxLayout:
orientation:'vertical'
size:root.width,root.height
spacing:10
padding:10
Label:
id:LabelHeading
text:'Login'
Label:
text:'UserName'
font_size:25
TextInput:
id:username
multiline:False
font_size:25
Label:
text:'Password'
font_size:25
TextInput:
id:password
multiline:False
font_size:25
TextInput:
text:'Re-enter Password'
font_size:25
multiline:False
id: reEnter
BoxLayout:
orientation:'horizontal'
spacing:20
size_hint:(.7,0.5)
pos_hint:{'center_x':0.5}
RoundedButton:
text:'Sign in'
id:signIn
color:utils.get_color_from_hex('#d9fcfa')
RoundedButton:
text:'Sign up'
id:signUp
color:utils.get_color_from_hex('#d9fcfa')
on_press:root.signUp()
我希望隐藏 id=reEnter 的 TextInput,直到未单击注册按钮。如果可以的话请写出代码。提前致谢
while making a login screen in kivy. I want to create a signUp button which when clicked will add an extra TextInput box on the screen for password verification. Here is the code of main file:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.core.window import Window
Window.size=(600,600)
Builder.load_file('loginLayoutCSS.kv')
class loginLayout(Widget):
def signIn(self):
pass
def signUp(self):
pass
class Login(App):
def build(Self):
Window.clearcolor=(0,1,0.8,.1)
return loginLayout()
if __name__=='__main__':
Login().run()
Code for .kv file:
#:kivy 2.1.0
#:import utils kivy.utils
<TextInput>
size_hint:(0.5,0.5)
pos_hint:{'center_x':0.5}
background_normal:''
<Label>
font_size:32
background_normal:''
color:utils.get_color_from_hex('#0c4160')
<RoundedButton@Button>
background_color:(0,0,0,0)
background_normal:''
canvas.before:
Color:
rgba:(48/255,84/255,150/255,1)
RoundedRectangle:
size:self.size
pos:self.pos
radius:[45]
<loginLayout>
BoxLayout:
orientation:'vertical'
size:root.width,root.height
spacing:10
padding:10
Label:
id:LabelHeading
text:'Login'
Label:
text:'UserName'
font_size:25
TextInput:
id:username
multiline:False
font_size:25
Label:
text:'Password'
font_size:25
TextInput:
id:password
multiline:False
font_size:25
TextInput:
text:'Re-enter Password'
font_size:25
multiline:False
id: reEnter
BoxLayout:
orientation:'horizontal'
spacing:20
size_hint:(.7,0.5)
pos_hint:{'center_x':0.5}
RoundedButton:
text:'Sign in'
id:signIn
color:utils.get_color_from_hex('#d9fcfa')
RoundedButton:
text:'Sign up'
id:signUp
color:utils.get_color_from_hex('#d9fcfa')
on_press:root.signUp()
I want the TextInput with id=reEnter to be hidden until the signUp button is not clicked. If it is possible, please write the code for it. Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您应该避免使用默认名称来命名动态类(例如
等)。要仅在按下按钮后显示
TextInput
,您可以使用其height
属性,如下所示:先隐藏它,
现在通过按钮按下传递实例,
然后在方法 <代码>注册,
First of all you should avoid naming dynamic class by their default name (do like
<MyTextInput@TextInput>
etc.).To reveal
TextInput
only after a button is pressed, you can use itsheight
property as follows:Hide it first,
Now pass the instance through button-press,
Then in method
signUp
,