Kivymd不能保持标签和文本字段在同一行



我创建了一个带有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:

我想保持标签和文本字段在同一行,彼此对齐。

orientationvertical时,MDBoxLayout的设计意图是将每个子节点放在单独的行中。如果你想让项目水平对齐,你可以使用MDBoxLayouthorizontalorientation(这是默认的)。

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

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

类似地,对于您希望在其单独行上的其余项。

相关内容

最新更新