在 kivy 中单击按钮时创建标签和文本输入 python
Create label and text input when button is clicked in 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,直到没有点击注册按钮。如果可能,请为其编写代码。提前致谢
首先,您应该避免使用默认名称命名动态 class(喜欢 <MyTextInput@TextInput>
等)。
要仅在按下按钮后显示 TextInput
,您可以按如下方式使用其 height
属性:
先隐藏起来,
MyTextInput:
text:'Re-enter Password'
font_size:25
multiline:False
size_hint_y: None
height: 0
background_color: 0, 0, 0, 0
id: reEnter
现在通过button-press、
传递实例
RoundedButton:
text:'Sign up'
id:signUp
color:utils.get_color_from_hex('#d9fcfa')
on_press:root.signUp(reEnter) # Pass the object.
然后在方法signUp
,
def signUp(self, obj):
obj.size_hint_y = 0.0 # To avoid 'TypeError'.
Animation(size_hint_y = 0.5, background_color = [1, 1, 1, 1], d = 0.5).start(obj)
在 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,直到没有点击注册按钮。如果可能,请为其编写代码。提前致谢
首先,您应该避免使用默认名称命名动态 class(喜欢 <MyTextInput@TextInput>
等)。
要仅在按下按钮后显示 TextInput
,您可以按如下方式使用其 height
属性:
先隐藏起来,
MyTextInput:
text:'Re-enter Password'
font_size:25
multiline:False
size_hint_y: None
height: 0
background_color: 0, 0, 0, 0
id: reEnter
现在通过button-press、
传递实例 RoundedButton:
text:'Sign up'
id:signUp
color:utils.get_color_from_hex('#d9fcfa')
on_press:root.signUp(reEnter) # Pass the object.
然后在方法signUp
,
def signUp(self, obj):
obj.size_hint_y = 0.0 # To avoid 'TypeError'.
Animation(size_hint_y = 0.5, background_color = [1, 1, 1, 1], d = 0.5).start(obj)