Kivy:如何更改“ Spinner”按钮的颜色和大小?

时间:2019-06-04 13:18:31

标签: python kivy

我正在寻找一种在Spinner小部件中指定按钮颜色和按钮大小的方法。

我想指定不是主(显示)按钮,但要指定下拉列表按钮。 似乎Spinner小部件可能仅具有用于指定有关主(显示)按钮的API。

如何更改微调器的颜色和大小?

import kivy
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.spinner import Spinner

class SpinnerWidget(Spinner):
    def __init__(self, **kwargs):
        super(SpinnerWidget, self).__init__(**kwargs)
        self.pos_hint = {'top':0.8, 'x':0.2}
        self.size_hint_x = 0.5
        self.size_hint_y = 0.1

        # default value shown
        self.text = 'Home'

        # main button color = white
        self.background_color = [255,255,255,1]

        # font size
        self.font_size = "12sp"

        # font_color
        self.color = [0,0,0,1]

        # available values
        self.values=('Home', 'Work', 'Other', 'Custom')

        # Is it possible to change color of drop-down button here? 
        # ~~~

    def show_selected_value(spinner, text):
        print('The spinner', spinner, 'has text', text)

class TestScreen(Screen):
    def __init__(self, **kwargs):
        super(TestScreen, self).__init__(**kwargs)
        spi = SpinnerWidget()
        self.add_widget(spi)

sm = ScreenManager()

class DemoApp(App):
    def build(self):
        sm.add_widget(TestScreen(name='test'))
        return sm

if __name__ == '__main__':
    DemoApp().run()

1 个答案:

答案 0 :(得分:1)

要更改SpinnerButton的大小和颜色,请实现dropdown_clsoption_cls

更改按钮的宽度-dropdown_cls

  • auto_width设置为False(默认为True
  • 提供自己的width

更改按钮的颜色和高度-option_cls

代码段-py文件

from kivy.uix.spinner import Spinner, SpinnerOption
from kivy.uix.dropdown import DropDown


class SpinnerOptions(SpinnerOption):

    def __init__(self, **kwargs):
        super(SpinnerOptions, self).__init__(**kwargs)
        self.background_normal = ''
        self.background_color = [0, 0, 1, 1]    # blue colour
        self.height = 26


class SpinnerDropdown(DropDown):

    def __init__(self, **kwargs):
        super(SpinnerDropdown, self).__init__(**kwargs)
        self.auto_width = False
        self.width = 150


class SpinnerWidget(Spinner):
    def __init__(self, **kwargs):
        super(SpinnerWidget, self).__init__(**kwargs)
        self.dropdown_cls = SpinnerDropdown
        self.option_cls = SpinnerOptions
        ...

输出

Result