我正在尝试将下拉按钮添加到窗口小部件,但仅向我显示该按钮,没有标签或文本输入。关闭APP后,它会向我显示包含所有内容的第二个小部件。我在哪里弄错了?好,谢谢
我的菜鸟代码:
main.py:
class MyGrid(FloatLayout):
name = ObjectProperty(None)
email = ObjectProperty(None)
psc = ObjectProperty(None)
def btn(self):
self.clear_btn()
def clear_btn(self):
self.email.text = ""
class MyApp(App): # <- Main Class
def build(self):
return MyGrid()
if __name__ == "__main__":
MyApp().run()
dropdown = DropDown()
for index in range(10):
btn = Button(text='Value %d' % index, size_hint_y=None, height=44)
btn.bind(on_release=lambda btn: dropdown.select(btn.text))
dropdown.add_widget(btn)
mainbutton = Button(text='Hello', size_hint=(None, None))
mainbutton.bind(on_release=dropdown.open)
dropdown.bind(on_select=lambda instance, x: setattr(mainbutton, 'text', x))
runTouchApp(mainbutton)
我的kv。文件:
<MyGrid>:
Label:
text: "Email: "
TextInput:
id: email
multiline:False
Button:
size_hint: 0.3, 0.1
pos_hint: {"x":0.5, "top":0.11}
text:"Send"
on_press: root.btn()
您的问题是代码:
if __name__ == "__main__":
MyApp().run()
运行您的MyApp
,并且直到MyApp
关闭后才会返回。因此,此后的代码要到MyApp
关闭后才能运行,然后
runTouchApp(mainbutton)
启动另一个App
。您可能想要在某个地方的某个类(可能是DropDown
)中的类中创建MyGrid
的代码,然后将mainbutton
添加到gui(也许在MyGrid
中)。