Kivymd:无法在 Scrollview 中添加多个文本字段和按钮
Posted
技术标签:
【中文标题】Kivymd:无法在 Scrollview 中添加多个文本字段和按钮【英文标题】:Kivymd: Can not add multiple textfield and buttons in Scrollview 【发布时间】:2021-11-24 15:16:21 【问题描述】:我试图在带有滚动视图的屏幕中添加许多文本字段和按钮,以便我可以上下滚动。但是位置和大小没有显示在相应的位置。
这是我的问题演示脚本:
from kivymd.app import MDApp
from kivy.lang import Builder
KV = """
Screen:
MDToolbar:
title:'Mini Agriculturist'
pos_hint:'top':1
left_action_items : [["arrow-left"]]
right_action_items : [["dots-vertical"]]
elevation:15
ScrollView:
pos_hint:'top':0.88
# size_hint_y:0.7
GridLayout:
cols: 1
size_hint_y: None
# orientation:'vertical'
spacing:'10dp'
MDTextField:
hint_text: "Category"
mode: "rectangle"
size_hint_x:0.4
pos_hint_x:0.5
multiple_line: True
id: Test_ss
MDTextField:
hint_text: "Disease name"
mode: "rectangle"
size_hint_x:0.4
pos_hint_x:0.5
MDTextField:
hint_text: "Symptoms"
mode: "rectangle"
size_hint_x:0.4
pos_hint_x:0.5
multiple_line: True
MDTextField:
hint_text: "More about disease"
mode: "rectangle"
size_hint_x:0.4
pos_hint_x:0.5
multiple_line: True
MDTextField:
hint_text: "Category"
mode: "rectangle"
size_hint_x:0.4
pos_hint_x:0.5
multiple_line: True
MDTextField:
hint_text: "Disease name"
mode: "rectangle"
size_hint_x:0.4
pos_hint_x:0.5
multiple_line: True
MDTextField:
hint_text: "Symptoms"
mode: "rectangle"
size_hint_x:0.4
pos_hint_x:0.5
multiple_line: True
MDTextField:
hint_text: "More about disease"
mode: "rectangle"
size_hint_x:0.4
pos_hint_x:0.5
MDTextField:
hint_text: "Disease name"
mode: "rectangle"
size_hint_x:0.4
pos_hint_x:0.5
MDTextField:
hint_text: "Symptoms"
mode: "rectangle"
size_hint_x:0.4
pos_hint_x:0.5
MDRectangleFlatButton:
text:'Choose'
font_size:14
halign: "center"
MDRectangleFlatButton:
text:'Cancel'
font_size:14
halign: "center"
"""
class WeatherApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
pass
def on_start(self):
self.root.ids.Test_ss.text = " Dhur bal\nOi salar putera\nMod khaba naki "
def build(self):
self.screen = Builder.load_string(KV)
return self.screen
WeatherApp().run()
输出快照:
问题:
1st,Scrollview 不能正常工作
2、不能控制元件的位置。有什么解决办法吗?
我想创造这样的东西,
谁能建议我如何在图像中设计这样的屏幕?提前致谢。
【问题讨论】:
【参考方案1】:这可能会有所帮助:
from kivymd.app import MDApp
from kivy.lang import Builder
KV = """
Screen:
MDToolbar:
title:'Contacts jbsidis'
pos_hint:'top':1
md_bg_color: [.2,0,1,.9]
left_action_items : [["menu", lambda x:print(23)]]
right_action_items : [["dots-vertical",lambda x:print(234)]]
elevation:0
FloatLayout:
BoxLayout:
pos_hint: "center_x": .5, "center_y": .38
ScrollView:
BoxLayout:
orientation: "vertical"
spacing: dp(60),dp(100)
size_hint_y: .6
BoxLayout:
## MDGridLayout:
## cols: 1
## row_default_height: (self.width - self.cols*self.spacing[0]) / self.cols
## row_force_default: True
## adaptive_height: True
## padding: dp(10), dp(5)
## spacing: dp(8),dp(1)
BoxLayout:
MDIconButton:
#size_hint: .4, .05
pos_hint: "center_x": .3, "top": .975
theme_text_color: "Custom"
text_color: [0,0,0,.8]
icon: 'account'
markup: True
TextInput:
id: email_jbsidis1
height: dp(40)
pos_hint: "center_x": .4, "top": .975
hint_text_color: [0,0,0, 1]
foreground_color: [0,0,0,.4]
hint_text: "Email"
background_color: [1,1,1,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
size_hint: .5 ,None #.06
canvas.after:
Color:
rgba: [0.0039,0.1921,0.2078,1]
Line:
width: dp(0.5)
rounded_rectangle:
(self.x, self.y, self.width-dp(20), dp(43),\
dp(8),dp(8),dp(8),dp(8),\
dp(50))
BoxLayout:
MDIconButton:
#size_hint: .4, .05
pos_hint: "center_x": .3, "top": .975
theme_text_color: "Custom"
text_color: [0,0,0,.8]
icon: 'phone'
markup: True
TextInput:
id: email_jbsidis2
height: dp(40)
pos_hint: "center_x": .4, "top": .975
hint_text_color: [0,0,0, 1]
foreground_color: [0,0,0,.4]
hint_text: "Phone"
background_color: [1,1,1,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
size_hint: .5 ,None #.06
canvas.after:
Color:
rgba: [0.0039,0.1921,0.2078,1]
Line:
width: dp(0.5)
rounded_rectangle:
(self.x, self.y, self.width-dp(20), dp(43),\
dp(8),dp(8),dp(8),dp(8),\
dp(50))
TextInput:
id: email_jbsidis3
height: dp(40)
pos_hint: "center_x": .4, "top": .975
hint_text_color: [0,0,0, 1]
foreground_color: [0,0,0,.4]
hint_text: "Area"
background_color: [1,1,1,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
size_hint: .5 ,None #.06
canvas.after:
Color:
rgba: [0.0039,0.1921,0.2078,1]
Line:
width: dp(0.5)
rounded_rectangle:
(self.x, self.y, self.width-dp(20), dp(43),\
dp(8),dp(8),dp(8),dp(8),\
dp(50))
## MDIconButton:
## #size_hint: .4, .05
## pos_hint: "center_x": .6, "top": .975
## theme_text_color: "Custom"
## text_color: [0,0,0,.8]
## icon: 'phone'
## markup: True
BoxLayout:
MDIconButton:
#size_hint: .4, .05
pos_hint: "center_x": .3, "top": .975
theme_text_color: "Custom"
text_color: [0,0,0,.8]
icon: 'map-marker'
markup: True
TextInput:
id: email_jbsidis4
height: dp(40)
pos_hint: "center_x": .4, "top": .975
hint_text_color: [0,0,0, 1]
foreground_color: [0,0,0,.4]
hint_text: "Address"
background_color: [1,1,1,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
size_hint: .5 ,None #.06
canvas.after:
Color:
rgba: [0.0039,0.1921,0.2078,1]
Line:
width: dp(0.5)
rounded_rectangle:
(self.x, self.y, self.width-dp(20), dp(43),\
dp(8),dp(8),dp(8),dp(8),\
dp(50))
BoxLayout:
MDIconButton:
#size_hint: .4, .05
pos_hint: "center_x": .3, "top": .975
theme_text_color: "Custom"
text_color: [0,0,0,.8]
icon: 'a11.png'
markup: True
TextInput:
id: email_jbsidis5
height: dp(40)
pos_hint: "center_x": .4, "top": .975
hint_text_color: [0,0,0, 1]
foreground_color: [0,0,0,.4]
hint_text: "State"
background_color: [1,1,1,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
size_hint: .5 ,None #.06
canvas.after:
Color:
rgba: [0.0039,0.1921,0.2078,1]
Line:
width: dp(0.5)
rounded_rectangle:
(self.x, self.y, self.width-dp(20), dp(43),\
dp(8),dp(8),dp(8),dp(8),\
dp(50))
TextInput:
id: email_jbsidis6
height: dp(40)
pos_hint: "center_x": .4, "top": .975
hint_text_color: [0,0,0, 1]
foreground_color: [0,0,0,.4]
hint_text: "Zipcode"
background_color: [1,1,1,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
size_hint: .5 ,None #.06
canvas.after:
Color:
rgba: [0.0039,0.1921,0.2078,1]
Line:
width: dp(0.5)
rounded_rectangle:
(self.x, self.y, self.width-dp(20), dp(43),\
dp(8),dp(8),dp(8),dp(8),\
dp(50))
## MDIconButton:
## #size_hint: .4, .05
## pos_hint: "center_x": .6, "top": .975
## theme_text_color: "Custom"
## text_color: [0,0,0,.8]
## icon: 'phone'
## markup: True
BoxLayout:
MDIconButton:
#size_hint: .4, .05
pos_hint: "center_x": .3, "top": .975
theme_text_color: "Custom"
text_color: [0,0,0,.8]
icon: 'message'
markup: True
TextInput:
id: email_jbsidis7
height: dp(40)
pos_hint: "center_x": .4, "top": .975
hint_text_color: [0,0,0, 1]
foreground_color: [0,0,0,.4]
hint_text: "Email"
background_color: [1,1,1,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
size_hint: .5 ,None #.06
canvas.after:
Color:
rgba: [0.0039,0.1921,0.2078,1]
Line:
width: dp(0.5)
rounded_rectangle:
(self.x, self.y, self.width-dp(20), dp(43),\
dp(8),dp(8),dp(8),dp(8),\
dp(50))
BoxLayout:
MDIconButton:
#size_hint: .4, .05
pos_hint: "center_x": .3, "top": .975
theme_text_color: "Custom"
text_color: [0,0,0,.8]
icon: 'calendar'
markup: True
TextInput:
id: email_jbsidis8
height: dp(40)
pos_hint: "center_x": .4, "top": .975
hint_text_color: [0,0,0, 1]
foreground_color: [0,0,0,.4]
hint_text: "Birthday"
background_color: [1,1,1,0]
background_image: ""
background_normal: ""
background_active: ""
multiline: False
size_hint: .5 ,None #.06
canvas.after:
Color:
rgba: [0.0039,0.1921,0.2078,1]
Line:
width: dp(0.5)
rounded_rectangle:
(self.x, self.y, self.width-dp(20), dp(43),\
dp(8),dp(8),dp(8),dp(8),\
dp(50))
## FloatLayout:
## MDIconButton:
## icon: 'lock-outline'
## markup: True
## opposite_colors: True
## pos_hint: 'center_x':.5, 'center_y':.65
BoxLayout:
spacing: dp(30)
FloatLayout:
MDRectangleFlatButton:
pos_hint: 'center_x':.3, 'center_y':.5
text:'Choose'
font_size:14
halign: "center"
on_release: print(root.ids.email_jbsidis1.text,root.ids.email_jbsidis2.text,root.ids.email_jbsidis3.text,root.ids.email_jbsidis4.text,root.ids.email_jbsidis5.text,root.ids.email_jbsidis6.text,root.ids.email_jbsidis7.text,root.ids.email_jbsidis8.text)
MDRectangleFlatButton:
pos_hint: 'center_x':.7, 'center_y':.5
text:'Cancel'
font_size:14
halign: "center"
on_release:
root.ids.email_jbsidis1.text=""
root.ids.email_jbsidis2.text=""
root.ids.email_jbsidis3.text=""
root.ids.email_jbsidis4.text=""
root.ids.email_jbsidis5.text=""
root.ids.email_jbsidis6.text=""
root.ids.email_jbsidis7.text=""
root.ids.email_jbsidis8.text=""
FloatLayout:
MDIconButton:
pos_hint: "center_x": .8, "center_y": 0.945
icon: "magnify"
theme_text_color: "Custom"
text_color: [1,1,1,1]
"""
class WeatherApp(MDApp):
def __init__(self, **kwargs):
super().__init__(**kwargs)
pass
## def on_start(self):
##
## self.root.ids.Test_ss.text = " Dhur bal\nOi salar putera\nMod khaba naki "
def build(self):
self.screen = Builder.load_string(KV)
return self.screen
WeatherApp().run()
图片:
【讨论】:
谢谢,但我想要一个带有这些组件的可滚动屏幕。 如果需要,您可以增加间距以分隔小部件,如果您发现答案有用,您可以为答案投票。 先生,我没有得到真正的答案,我需要一个带有许多文本字段的可滚动屏幕,但我做不到。 好吧,上面给出的示例是一个屏幕,其中有很多文本字段,您可以滚动它,因为它在 ScrollView 类中,您可以复制并使用我的答案中给出的示例,然后您可以根据您的程序对其进行自定义,一旦您测试了示例,您将看到它滚动并且在许多文本字段下方有两个按钮,如果这不是需要的,那么您可以编辑您的问题,以便每个人都可以提供适当的答案 你想检查一下结果,当我尝试你的技术时会发生什么(还有截图)> github.com/Shourov1702040/hpylearners_Python/tree/main/…【参考方案2】:我已经用下面的脚本解决了这个问题
https://github.com/Shourov1702040/hpylearners_Python/blob/main/Basic%20python/Solutions/Many_textfield_with_scrollView.py
【讨论】:
以上是关于Kivymd:无法在 Scrollview 中添加多个文本字段和按钮的主要内容,如果未能解决你的问题,请参考以下文章
Kivy - 如何在 ModalView 内向 ScrollView 添加多个按钮?
无法在 MDGridLayout 中添加 kivymd 的 MDTextField