在kivy python中单击按钮时创建标签和文本输入

发布于 2025-01-16 18:52:46 字数 2327 浏览 2 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

或十年 2025-01-23 18:52:46

首先,您应该避免使用默认名称来命名动态类(例如 等)。

要仅在按下按钮后显示 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

现在通过按钮按下传递实例,

            RoundedButton:
                text:'Sign up'
                id:signUp
                color:utils.get_color_from_hex('#d9fcfa')
                on_press:root.signUp(reEnter) # Pass the object.

然后在方法 <代码>注册,

    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)

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 its height property as follows:

Hide it first,

        MyTextInput:
            text:'Re-enter Password'
            font_size:25
            multiline:False
            size_hint_y: None
            height: 0
            background_color: 0, 0, 0, 0
            id: reEnter

Now pass the instance through button-press,

            RoundedButton:
                text:'Sign up'
                id:signUp
                color:utils.get_color_from_hex('#d9fcfa')
                on_press:root.signUp(reEnter) # Pass the object.

Then in method 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文