如何在Kivymd中添加多个Textfield

发布于 2025-01-22 15:32:16 字数 1968 浏览 3 评论 0原文

我正在制作一个表单屏幕,用户可以在其中输入数据并将其上传到数据库。我希望他们能够在按钮上添加多个文本字段,如果他们仍然想添加一些东西。它应该可以滚动,因为文本场的量基于用户真正想要的数量。我在这里有这个代码,但它并没有真正想到我的想法。

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.textfield import MDTextField

class TextField(MDTextField):
    pass

class Tab(MDFloatLayout, MDTabsBase):
    pass


class FormScreen(Screen):
    pass

class DemoApp(MDApp):
#function to add text field
def add_textfield(self):
    self.help.get_screen('form').ids.box.add_widget(
    TextField(hint_text= 'adsf',
    ))

def upload(self):
    
    name = self.help.get_screen('form').ids.input_1.text


def build(self):
    self.help = Builder.load_file('form.kv')
    # screen.add_widget(self.help)
    return self.help

这是我的KV文件:

ScreenManager:
    FormScreen:

<FormScreen>
    name: 'form'
    MDBoxLayout:
        orientation: "vertical"
        MDToolbar:
            # md_bg_color:app.dark2
            title: "Upload Data"
            type_height: "small"
            left_action_items: [["arrow-left", lambda x : app.swtchScreen('collections')]]
            right_action_items: [["eraser", lambda x : root.eraser()],["plus", lambda x : app.add_textfield()]]
        MDTabs:
            id: tabs
            background_color: rgba(0,0,0,0)
            tab_hint_x: True
            Tab:
                title: "Passport Data"
                MDTextField:
                    id: input_1
                    hint_text: "Name"
                    pos_hint: {"center_x": 0.5, "center_y": 0.95}
                    size_hint: .75,0.09
                    color_mode: 'accent'
                    mode: "rectangle"

                #additional textfieldss
                MDTextField:
                    id: box

谢谢

i am making a form screen in which a user can input data and upload it to database. I want them to be able to add multiple textfields on a button press if they still want to add something. it should be scrollable since the amount of textfields is based on how many the user really wants. i have this code right here but it doesn't really do what i had in mind.

from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.tab import MDTabsBase
from kivymd.uix.floatlayout import MDFloatLayout
from kivymd.uix.textfield import MDTextField

class TextField(MDTextField):
    pass

class Tab(MDFloatLayout, MDTabsBase):
    pass


class FormScreen(Screen):
    pass

class DemoApp(MDApp):
#function to add text field
def add_textfield(self):
    self.help.get_screen('form').ids.box.add_widget(
    TextField(hint_text= 'adsf',
    ))

def upload(self):
    
    name = self.help.get_screen('form').ids.input_1.text


def build(self):
    self.help = Builder.load_file('form.kv')
    # screen.add_widget(self.help)
    return self.help

Here is my kv file:

ScreenManager:
    FormScreen:

<FormScreen>
    name: 'form'
    MDBoxLayout:
        orientation: "vertical"
        MDToolbar:
            # md_bg_color:app.dark2
            title: "Upload Data"
            type_height: "small"
            left_action_items: [["arrow-left", lambda x : app.swtchScreen('collections')]]
            right_action_items: [["eraser", lambda x : root.eraser()],["plus", lambda x : app.add_textfield()]]
        MDTabs:
            id: tabs
            background_color: rgba(0,0,0,0)
            tab_hint_x: True
            Tab:
                title: "Passport Data"
                MDTextField:
                    id: input_1
                    hint_text: "Name"
                    pos_hint: {"center_x": 0.5, "center_y": 0.95}
                    size_hint: .75,0.09
                    color_mode: 'accent'
                    mode: "rectangle"

                #additional textfieldss
                MDTextField:
                    id: box

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

固执像三岁 2025-01-29 15:32:16

您可以修改tab以自定义您需要保存所有必要字段所需的布局,

        MDTabs:
            id: tabs
            background_color: rgba(0,0,0,0)
            tab_hint_x: True
            Tab:
                title: "Passport Data"
                MDBoxLayout: # Add main container.
                    orientation: "vertical"
                    padding: dp(10)
                    spacing: dp(5)
                    MDTextField:
                        id: input_1
                        hint_text: "Name"
#                        pos_hint: {"center_x": 0.5, "center_y": 0.95}
#                        size_hint: .75,0.09 # "size_hint_y" will be set automatically.
                        pos_hint: {"center_x": 0.5}
                        size_hint_x: .75
                        color_mode: 'accent'
                        mode: "rectangle"

                    #additional textfields
                    ScrollView:
                        MDBoxLayout: # Add all text fields in this container.
                            id: box
                            orientation: "vertical"
                            adaptive_height: True # Grow vertically.

或者您可以从mdboxlayout

class Tab(MDBoxLayout, MDTabsBase):
    pass

那么kvlang是,

        MDTabs:
            id: tabs
            background_color: rgba(0,0,0,0)
            tab_hint_x: True
            Tab:
                title: "Passport Data"
                orientation: "vertical"
                padding: dp(10)
                spacing: dp(5)
                MDTextField:
                    id: input_1
                    hint_text: "Name"
#                    pos_hint: {"center_x": 0.5, "center_y": 0.95}
#                    size_hint: .75,0.09 # "y" will be set automatically.
                    pos_hint: {"center_x": 0.5}
                    size_hint_x: .75
                    color_mode: 'accent'
                    mode: "rectangle"

                #additional textfieldss
                ScrollView:
                    MDBoxLayout: # Add all text field here.
                        id: box
                        orientation: "vertical"
                        adaptive_height: True # Grow vertically.

You can modify the Tab to customize the layout you need to hold all necessary fields as follows,

        MDTabs:
            id: tabs
            background_color: rgba(0,0,0,0)
            tab_hint_x: True
            Tab:
                title: "Passport Data"
                MDBoxLayout: # Add main container.
                    orientation: "vertical"
                    padding: dp(10)
                    spacing: dp(5)
                    MDTextField:
                        id: input_1
                        hint_text: "Name"
#                        pos_hint: {"center_x": 0.5, "center_y": 0.95}
#                        size_hint: .75,0.09 # "size_hint_y" will be set automatically.
                        pos_hint: {"center_x": 0.5}
                        size_hint_x: .75
                        color_mode: 'accent'
                        mode: "rectangle"

                    #additional textfields
                    ScrollView:
                        MDBoxLayout: # Add all text fields in this container.
                            id: box
                            orientation: "vertical"
                            adaptive_height: True # Grow vertically.

Alternatively you could've inherited Tab from MDBoxLayout,

class Tab(MDBoxLayout, MDTabsBase):
    pass

Then the kvlang would be,

        MDTabs:
            id: tabs
            background_color: rgba(0,0,0,0)
            tab_hint_x: True
            Tab:
                title: "Passport Data"
                orientation: "vertical"
                padding: dp(10)
                spacing: dp(5)
                MDTextField:
                    id: input_1
                    hint_text: "Name"
#                    pos_hint: {"center_x": 0.5, "center_y": 0.95}
#                    size_hint: .75,0.09 # "y" will be set automatically.
                    pos_hint: {"center_x": 0.5}
                    size_hint_x: .75
                    color_mode: 'accent'
                    mode: "rectangle"

                #additional textfieldss
                ScrollView:
                    MDBoxLayout: # Add all text field here.
                        id: box
                        orientation: "vertical"
                        adaptive_height: True # Grow vertically.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文