【问题标题】:Kivymd not able to keep label and textfield in same rowKivymd 无法将标签和文本字段保持在同一行
【发布时间】:2021-08-13 04:52:45
【问题描述】:

我创建了一个带有 kivymd 标签和文本字段的表单,但不知何故,标签和文本字段彼此没有正确对齐。 mdlabel 使用 pos_hint 函数在 mdtextfield 行的上方稍微对齐。我的代码如下:

#.py

from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.screenmanager import Screen
from kivy.properties import *

class Main(Screen):
    pass

class MyApp(MDApp):
    def build(self):
        return Main()

MyApp().run()

#.kv
#:import Vector kivy.vector.Vector
<Main>:
    MDNavigationLayout:
        ScreenManager:
            id: screen_manager
            Screen:
                name: "challan"
                MDBoxLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        title: "My App"
                        left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
             
                    
                    MDLabel:
                        text:'Date'
                        pos_hint: {"center_x": .5, "top": 1}
                        theme_text_color:'Primary'

                    MDTextField:
                        id:date
                        hint_text: "Date"
                        pos_hint: {"center_x": .5, "top": 1}
                        size_hint:(None,None)
                        width: 300

                    MDLabel:
                        text:'Challan No.'
                        pos_hint: {"center_x": .5, "top": 1}
                        theme_text_color:'Primary'

                    MDTextField:
                        id: challan
                        hint_text: "Challan No."
                        pos_hint: {"center_x": .5, "top": 1}          
                        size_hint:(None,None)
                        width:300

                    MDLabel:
                        text:'Quantity'
                        pos_hint: {"center_x": .5, "top": 1}
                        theme_text_color:'Primary'

                    MDTextField:
                        id:qty
                        hint_text: "Quantity"
                        pos_hint: {"center_x": .5, "top": 1}
                        size_hint:(None,None)
                        width: 300

                    MDLabel:
                        text:'From Location'
                        pos_hint: {"center_x": .5, "top": 1}
                        theme_text_color:'Primary'

                    MDTextField:
                        id: flo
                        hint_text: "From Location"
                        pos_hint: {"center_x": .5, "top": 1}
                        size_hint:(None,None)
                        width:300
                    
            Screen:
                name: "Message"
                BoxLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        title: "My App"
                        left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
                    MDLabel:
                        text:"Email: "
                        # font_size: (root.width**2 + root.height**2) / 17**4
                        pos_hint: {"x":0.1, "top":0.9}
                        size_hint: 0.25, 0.15

                    MDTextField:
                        id:namee
                        hint_text: "Name"
                        helper_text:"forget"
                        pos_hint : {'center_x':0.0 ,'center_y':0.0}
                        size_hint_x : None
                        width: 300

        MDNavigationDrawer:
            id:nav_drawer
            opening_transition:'out_bounce'
            opening_time:1
            BoxLayout:
                orientation:'vertical'
                MDRectangleFlatIconButton:
                    icon: "android"
                    size_hint_x: 1
                    text: 'Android'
                    on_release:
                        screen_manager.current = "challan"
                MDRectangleFlatIconButton:
                    icon: "magnify"
                    size_hint_x: 1
                    text: 'Search'
                    on_release:
                        screen_manager.current = "Message"
                Widget:

我想让标签和文本字段在同一行中彼此对齐。

【问题讨论】:

    标签: python kivy kivy-language kivymd


    【解决方案1】:

    MDBoxLayout 的设计意图是在 orientationvertical 时将每个子元素放在单独的行中。如果您希望项目水平对齐,您可以使用MDBoxLayoutorientationhorizontal(这是默认设置)。

    在您的情况下,实现目标的一种方法是将 MDLabelMDTextInput 对放在各自的 horizontal MDBoxLayout 中。例如:

            Screen:
                name: "challan"
                MDBoxLayout:
                    orientation: 'vertical'
                    MDToolbar:
                        title: "My App"
                        left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
             
                    MDBoxLayout:
                        MDLabel:
                            text:'Date'
                            pos_hint: {"center_y": .5}
                            theme_text_color:'Primary'
    
                        MDTextField:
                            id:date
                            hint_text: "Date"
                            pos_hint: {"center_y": .5}
                            size_hint:(None,None)
                            width: 300
                            
                    MDBoxLayout:
                        MDLabel:
                            text:'Challan No.'
                            pos_hint: {"center_y": .5}
                            theme_text_color:'Primary'
    
                        MDTextField:
                            id: challan
                            hint_text: "Challan No."
                            pos_hint: {"center_y": .5}     
                            size_hint:(None,None)
                            width:300
    

    同样,对于您想要在自己的行中的其余项目。

    【讨论】:

    • 感谢您的回复。有了这个,我的工具栏就在我的屏幕底部。我不明白 pos_hint 如何与 kivymd 一起工作
    • 在您的kv 中,MDToolbar 仍然是第一个 MDBoxLayout 之后的第一个项目吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    • 2021-01-26
    相关资源
    最近更新 更多