gpt4 book ai didi

python-3.x - 如何自动设置 MDCard 高度

转载 作者:行者123 更新时间:2023-12-05 02:39:34 24 4
gpt4 key购买 nike

我想设置 mdcard height 使其适合 mdlabel,也许不需要我手动设置 mdcard 大小。请问我该怎么做..提前致谢..

为了更好地理解这里有一个示例代码:

from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.core.window import Window

Window.size = (300, 530)

KV = """
MDBoxLayout:
orientation: 'vertical'
ScrollView:
MDGridLayout:
cols: 1
adaptive_height: True
padding: '10dp', '15dp'
spacing: '15dp'

MDCard:
orientation: 'vertical'
size_hint: None, None
size: 280, 200
MDLabel:
markup: True
padding: [15, 15]
text:
'''
[size=25][b]Ford[/b][/size]
If mdlabel text becomes too many to fit into the specified mdcard size,\n
the text overslaps other things closeby..\n
So my question: How do I set MDCard height to automatically\n
adjusts accordingly to the height/size of widgets inside of it.\n
Thanks in Advance!!
'''

MDCard:
orientation: 'vertical'
size_hint: None, None
size: 280, 200
MDLabel:
markup: True
padding: [15, 15]
text:
'''
[size=25][b]Ford[/b][/size]
If mdlabel text becomes too many to fit into the specified mdcard size,\n
the text overslaps other things closeby..\n
So my question: How do I set MDCard height to automatically\n
adjusts accordingly to the height/size of widgets inside of it.\n
Thanks in Advance!!
'''



"""


class Example(MDApp):
def build(self):
return Builder.load_string(KV)


Example().run()

就像上面的代码一样,如果 mdlabel 文本变得太多而无法适应指定的 mdcard 大小,文本会覆盖附近的其他内容。所以我的问题是:如何设置 MDCard 高度以根据其中的小部件的高度/大小自动调整。提前致谢!!

最佳答案

您可以将MDCard高度设置为与MDLabel高度相同,但您必须允许 MDLabel 调整到其 text 的大小。这是您的 kv 的修改版本:

MDBoxLayout:
orientation: 'vertical'
ScrollView:
MDGridLayout:
cols: 1
adaptive_height: True
padding: '10dp', '15dp'
spacing: '15dp'

MDCard:
orientation: 'vertical'
size_hint: 1, None
# size: 280, 200
height: label1.height
MDLabel:
id: label1
markup: True
padding: [15, 15]
text:
'''
[size=25][b]Ford[/b][/size]
If mdlabel text becomes too many to fit into the specified mdcard size,\n
the text overslaps other things closeby..\n
So my question: How do I set MDCard height to automatically\n
adjusts accordingly to the height/size of widgets inside of it.\n
Thanks in Advance!!
'''
size_hint_y: None
height: self.texture_size[1] + 2*self.padding[1]

MDCard:
orientation: 'vertical'
size_hint: 1, None
# size: 280, 200
height: label2.height
MDLabel:
id: label2
markup: True
padding: [15, 15]
text:
'''
[size=25][b]Ford[/b][/size]
If mdlabel text becomes too many to fit into the specified mdcard size,\n
the text overslaps other things closeby..\n
So my question: How do I set MDCard height to automatically\n
adjusts accordingly to the height/size of widgets inside of it.\n
Thanks in Advance!!
'''
size_hint_y: None
height: self.texture_size[1] + 2*self.padding[1]

关于python-3.x - 如何自动设置 MDCard 高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69055072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com