from kivy.uix.spinner import Spinner, SpinnerOption
from kivy.uix.dropdown import DropDown
from functools import partial
class ThirdScreen(Screen):
def __init__(self, **kwargs):
super(ThirdScreen, self).__init__(**kwargs)
self.bl_up2 = BoxLayout(orientation='vertical', spacing = 20)
ColoredDropdown = partial(DropDown, bar_color=(1, 0, 1, 1))
self.spin1 = Spinner(dropdown_cls = ColoredDropdown,
text='gender', values = ("1", "2")
self.bl_up2.add_widget(self.spin1)
self.add_widget(self.bl_up2)
I just started learning Python and maybe my mistake is superficial, but I don't understand where the mistake is. Added dropdown but something went wrong. wrote the code without kv lang. I don't understand it and yes my code is huge.
An easy way to do that is by using the kv
language to define your dropdown_cls
. Here is a modified version of your code that does it:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import Screen
from kivy.uix.spinner import Spinner
Builder.load_string('''
<ColoredDropdown@DropDown>:
bar_color: 1, 0, 1, 1
''')
class ThirdScreen(Screen):
def __init__(self, **kwargs):
super(ThirdScreen, self).__init__(**kwargs)
self.bl_up2 = BoxLayout(orientation='vertical', spacing=20)
values = []
for i in range(20):
values.append(str(i))
self.spin1 = Spinner(dropdown_cls='ColoredDropdown', size_hint=[0.25, None], height=55,
text='gender', values=values)
self.bl_up2.add_widget(self.spin1)
self.add_widget(self.bl_up2)
class TestApp(App):
def build(self):
return ThirdScreen()
TestApp().run()
The Builder.load_string
specifies a rule for creating instances of ColoredDropDown
using the kivy language. Then specifying the dropdown_cls
as a string in the Spinner
creation tells Kivy
to use Factory
when creating the ColoredDropDown
, See the documentation.