Python 如何根据.kv文件中的屏幕动态调整文本大小

Python 如何根据.kv文件中的屏幕动态调整文本大小,python,kivy,kivy-language,Python,Kivy,Kivy Language,所以我需要在屏幕上打印一个列表。此列表的大小可以更改,并且限制为15个元素,即15行(\n)。每一行都有一个新名字。此列表将打印在必须覆盖整个列表的大按钮上方。我可以将按钮中的文本整齐地对齐,但一旦屏幕大小改变,文本就会偏移,因为按钮会根据屏幕大小动态变化 下面你可以看到我为那个特定类编写的代码,虽然我不认为这会有任何用处,但我想我还是会包括它 class RecordData(Screen): def printNames(self): with open("

所以我需要在屏幕上打印一个列表。此列表的大小可以更改,并且限制为15个元素,即15行(\n)。每一行都有一个新名字。此列表将打印在必须覆盖整个列表的大按钮上方。我可以将按钮中的文本整齐地对齐,但一旦屏幕大小改变,文本就会偏移,因为按钮会根据屏幕大小动态变化

下面你可以看到我为那个特定类编写的代码,虽然我不认为这会有任何用处,但我想我还是会包括它

class RecordData(Screen):

    def printNames(self):
        with open("Lipo names.txt") as f:
            lineList = f.readlines()
        lipoNames = ("".join(map(str, lineList)))
        self.names.text = lipoNames
        self.remove_widget(self.remove)
正如你所看到的,我一直在玩字体大小,现在动态变化,但即使屏幕只在y轴上拉伸,它也会调整文本的x和y大小。我希望文本的y大小在屏幕y改变时改变,而不是同时改变x和y

<RecordData>
    name: "record"
    names: names
    remove: remove

    Button:
        background_color: 255,255,255,1
        pos_hint: {"x": 0.05, "y": 0.05}
        size_hint: 0.2, 0.9


    Button:
        id: remove
        pos_hint: {"x": 0.09, "y": 0.5}
        size_hint: 0.2, 0.1
        text: "View lipos"
        font_size: (root.width**2 + root.height**2) / 14**4
        on_release:
            root.printNames()

    Label:
        id: names
        text: ""
        color: 0,0,0,1
        font_size: (root.width**2 + root.height**1.9) / 13**4
        pos_hint:{"x": 0.0, "y": 0.32}
        size_hint:0.3, 0.15

名称:“记录”
姓名:姓名
移除:移除
按钮:
背景颜色:255255,1
pos_提示:{“x”:0.05,“y”:0.05}
尺寸提示:0.2,0.9
按钮:
id:删除
pos_提示:{“x”:0.09,“y”:0.5}
大小提示:0.2,0.1
文本:“查看lipos”
字体大小:(根.宽**2+根.高**2)/14**4
发布时:
root.printNames()
标签:
id:姓名
案文:“”
颜色:0,0,0,1
字体大小:(根.宽**2+根.高**1.9)/13**4
pos_提示:{“x”:0.0,“y”:0.32}
尺寸提示:0.3,0.15


我希望这是有道理的。这是我的第一款kivy应用程序,所以我对这门kv语言还是很陌生,所以请你保持简单的解释。提前感谢您的帮助

至少可以用两种不同的方法计算字体大小。最简单的方法就是像你所做的那样使用一个公式。我发现以下方法似乎有效:

Button:
    id: butt  # added id
    background_color: 255,255,255,1
    pos_hint: {"x": 0.05, "y": 0.05}
    size_hint: 0.2, 0.9
Label:
    id: names
    text: ""
    color: 0,0,0,1
    pos_hint: butt.pos_hint  # same position as the Button
    size_hint: 0.3, butt.size_hint_y  # same height as the Button
    
    # set text area to same size as the Label
    text_size: self.size
    
    # position the text in the text area
    valign: 'center'
    halign: 'left'

    font_size: (self.height / 15) * 0.75
另一种方法是使用
kivy.core.text.Label
计算字体大小,方法是设置触发器,在文本或标签大小更改时计算字体大小:

<RecordData>
    name: "record"
    names: names
    remove: remove

    Button:
        id: butt  # added id
        background_color: 255,255,255,1
        pos_hint: {"x": 0.05, "y": 0.05}
        size_hint: 0.2, 0.9


    Button:
        id: remove
        pos_hint: {"x": 0.09, "y": 0.5}
        size_hint: 0.2, 0.1
        text: "View lipos"
        font_size: (root.width**2 + root.height**2) / 14**4
        on_release:
            root.printNames()

    Label:
        id: names
        text: ""
        color: 0,0,0,1
        pos_hint: butt.pos_hint  # same position as the Button
        size_hint: 0.3, butt.size_hint_y  # same height as the Button
        
        # set text area to same size as the Label
        text_size: self.size
        
        # position the text in the text area
        valign: 'center'
        halign: 'left'
        
        # recalculate font size when text or size changes
        on_size: root.adjust_font_size()
        on_text: root.adjust_font_size()

此代码只考虑了标签的高度,但也可以扩展到考虑宽度。

< P>可以以至少两种不同的方式计算字体大小。最简单的方法就是像你所做的那样使用一个公式。我发现以下方法似乎有效:

Button:
    id: butt  # added id
    background_color: 255,255,255,1
    pos_hint: {"x": 0.05, "y": 0.05}
    size_hint: 0.2, 0.9
Label:
    id: names
    text: ""
    color: 0,0,0,1
    pos_hint: butt.pos_hint  # same position as the Button
    size_hint: 0.3, butt.size_hint_y  # same height as the Button
    
    # set text area to same size as the Label
    text_size: self.size
    
    # position the text in the text area
    valign: 'center'
    halign: 'left'

    font_size: (self.height / 15) * 0.75
另一种方法是使用
kivy.core.text.Label
计算字体大小,方法是设置触发器,在文本或标签大小更改时计算字体大小:

<RecordData>
    name: "record"
    names: names
    remove: remove

    Button:
        id: butt  # added id
        background_color: 255,255,255,1
        pos_hint: {"x": 0.05, "y": 0.05}
        size_hint: 0.2, 0.9


    Button:
        id: remove
        pos_hint: {"x": 0.09, "y": 0.5}
        size_hint: 0.2, 0.1
        text: "View lipos"
        font_size: (root.width**2 + root.height**2) / 14**4
        on_release:
            root.printNames()

    Label:
        id: names
        text: ""
        color: 0,0,0,1
        pos_hint: butt.pos_hint  # same position as the Button
        size_hint: 0.3, butt.size_hint_y  # same height as the Button
        
        # set text area to same size as the Label
        text_size: self.size
        
        # position the text in the text area
        valign: 'center'
        halign: 'left'
        
        # recalculate font size when text or size changes
        on_size: root.adjust_font_size()
        on_text: root.adjust_font_size()

此代码只考虑标签的高度,但可以扩展到考虑宽度。