我正在寻找一种在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()
答案 0 :(得分:1)
要更改Spinner
中Button
的大小和颜色,请实现dropdown_cls和option_cls。
auto_width
设置为False
(默认为True
)width
background_normal = ''
background_color
提供自己的颜色height
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
...