Kivy Spinner - Values Size

490 Views Asked by At

Is there a way to change the text size from the values argument within Spinner? I've found that I can change the height and width of the dropdown boxes and change the size of the "label" (Layer). But is there a way to change the text size of both heads and all?

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

There are 1 best solutions below

2
On BEST ANSWER

Yes, the options in a Spinner are displayed using the SpinnerOption class (just a Button) by default. You can set a different class to display the options using option_cls atribute, and specify the font_size for that class, like this in your 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

If you want the options to inherit the font_size of the Spinner, you can define a new Spinner class extension:

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

The new on_font_size() method adjust the font_size of the options whenever the font_size of MpSpinner changes. You also need to adjust your 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

This uses the new MySpinner class and the MyOption class now adjusts its height based on its texture_size.