Kivy Spinner - 重视尺寸

问题描述 投票:0回答:1

有没有办法通过

values
中的
Spinner
参数更改文本大小?我发现我可以更改下拉框的高度和宽度,并更改“标签”(图层)的大小。但是有没有办法同时更改
heads
all
的文本大小?

Spinner:
    id: spinner_lry
    text: 'Layer'
    values:['heads', 'all']
    size_hint_y: None
    height: 50
    font_size: 20
list kivy spinner text-size
1个回答
2
投票

是的,默认情况下,使用

Spinner
类(只是
SpinnerOption
)显示
Button
中的选项。您可以使用
option_cls
属性设置不同的类来显示选项,并为该类指定
font_size
,就像在
kv
中这样:

<MyOption@SpinnerOption>:
    font_size: 50

Spinner:
    id: spinner_lry
    text: 'Layer'
    option_cls: "MyOption"
    values:['heads', 'all']
    size_hint_y: None
    height: 50
    font_size: 20

如果你想让选项继承

font_size
Spinner
,你可以定义一个新的
Spinner
类扩展:

class MySpinner(Spinner):
    def on_font_size(self, spinner_instance, new_font_size):
        # this method is modelled after the _update_dropdown_size() method of Spinner
        dp = self._dropdown
        if not dp:
            return

        container = dp.container
        if not container:
            return
        
        for item in container.children[:]:
            item.font_size = new_font_size

新的

on_font_size()
方法会在
font_size
font_size
发生变化时调整选项的
MpSpinner
。你还需要调整你的
kv
:

<MyOption@SpinnerOption>:
    height: self.texture_size[1] * 1.1

MySpinner:
    id: spinner_lry
    text: 'Layer'
    option_cls: "MyOption"
    values:['heads', 'all']
    size_hint_y: None
    height: self.font_size * 1.5
    font_size: 20

这使用新的

MySpinner
类,并且
MyOption
类现在根据其
height
调整其
texture_size

© www.soinside.com 2019 - 2024. All rights reserved.