Проблема в том, что вы вызываете create_drop_down()
и create_go_button()
в методе __init__()
класса TestMe
. Поскольку вы также определяете правило <TestMe>:
в своем kv
, возникает конфликт. Согласно несколько неясной документации , правило kv
применяется после запуска __init__()
. Это означает, что любой Widgets
, добавленный к TestMe
в его __init__()
, будет перезаписан Widegets
, указанным в правиле kv
. Возможные решения - добавить всех дочерних элементов TestMe
либо в метод __init__()
, либо в kv
, либо переместить добавление Widgets
из метода __init__()
. Вот модифицированная версия вашего кода, которая использует последний подход:
class WindowManager(ScreenManager, Screen):
TestMe = ObjectProperty(None)
text_lists = ['hi', 'nice one', 'another one']
class TestMe(Screen, FloatLayout):
global text_lists
main_button = ObjectProperty(None)
selected_list = 'SELECTED'
top_layout = ObjectProperty(None)
#top_layout = GridLayout(cols=4)
def __init__(self, **kwargs):
super(TestMe, self).__init__(**kwargs)
self.dropdown = DropDown()
Clock.schedule_once(self.create_drop_down)
Clock.schedule_once(self.create_go_button)
# self.create_drop_down()
# self.create_go_button()
def create_drop_down(self, *args):
for list_name in text_lists:
# When adding widgets, we need to specify the height manually
# (disabling the size_hint_y) so the dropdown can calculate
# the area it needs.
btn = Button(text= list_name, size_hint_y=None, height=88, width=400, background_color=(41/255, 21/255, 228/255, 1))
# for each button, attach a callback that will call the select() method
# on the dropdown. We'll pass the text of the button as the data of the
# selection.
btn.bind(on_release=lambda btn: self.dropdown.select(btn.text),
on_press=lambda btn: self.select_list(btn.text))
# then add the button inside the dropdown
self.dropdown.add_widget(btn)
# create a big main button
# self.main_button = Button(text='Choose A List', size_hint=(None, None), height=88, width=400, background_color=(41/255, 21/255, 228/255, 1))
self.main_button = Button(text='Choose A List', background_color=(41/255, 21/255, 228/255, 1))
# show the dropdown menu when the main button is released
# note: all the bind() calls pass the instance of the caller (here, the
# mainbutton instance) as the first argument of the callback (here,
# dropdown.open.).
self.main_button.bind(on_release=self.dropdown.open)
# one last thing, listen for the selection in the dropdown list and
# assign the data to the button text.
self.dropdown.bind(on_select=lambda instance, x: setattr(self.main_button, 'text', x))
self.top_layout.add_widget(self.main_button)
def create_go_button(self, *args):
# go_btn = Button(text="Go!", size_hint=(None, None), height=88, width=400, background_color=(41/255, 21/255, 228/255, 1))
go_btn = Button(text="Go!", background_color=(41/255, 21/255, 228/255, 1))
self.top_layout.add_widget(go_btn)
def select_list(self, selected):
self.selected_list = selected
class MyTest(App):
def build(self):
return kv
Я изменил код для вызова методов create
с использованием Clock.schedule_once()
, так что это происходит после правила kv
применяется. Я также удалил аргументы size_hint
и size
из Buttons
, созданного, чтобы разрешить GridLayout
их размер. Я также закомментировал ненужный код.