python - 为什么我的 kivy 下拉菜单没有显示(Python)?
问题描述
我目前正在尝试用 kivy 制作词汇训练器,以便可以在手机上使用它。但我不知何故被困在下拉菜单中。我使用了 kivy wiki 中的下拉菜单示例并进行了一些更改,使其适合我现有的代码。我现在的问题是下拉菜单没有显示,python 也没有给我一个错误。我已经尝试了一些其他可能的解决方案,这些解决方案也对我不起作用,以及通过 try 进行调试 - 除了和 pycharm 调试器。
class FloatLayout(FloatLayout):
def __init__(self, **kwargs):
super(FloatLayout, self).__init__(**kwargs)
self.dropdown = DropDown()
self.languages = ["language1", "language2", "language3"]
for i in self.languages:
btn = Button(text="%r" % i, size_hint_y=None, height=30)
btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
self.dropdown.add_widget(btn)
self.dropdownButton = Button(text="Language selection", size_hint=(None, None), pos_hint={"x": 0.5, "top": 0.5})
self.dropdownButton.bind(on_release=self.dropdown.open)
self.dropdown.bind(on_select=lambda instance, x: setattr(self.dropdownButton, "text", x))
class KivyGUI(App):
def build(self):
return FloatLayout()
if __name__ == "__main__":
runKivy()
所有的帮助和赞赏。谢谢!
解决方案
问题 1
需要添加self.dropdownButton
到根小部件,FloatLayout
.
self.add_widget(self.dropdownButton)
问题 2
替换runKivy()
为KivyGUI().run()
片段
self.dropdown.bind(on_select=lambda instance, x: setattr(self.dropdownButton, "text", x))
self.add_widget(self.dropdownButton)
class KivyGUI(App):
def build(self):
return FloatLayout()
if __name__ == "__main__":
KivyGUI().run()
输出
推荐阅读
- algorithm - 需要帮助分析该算法的时间复杂度
- c# - asp.net core 2.1 模型绑定(提交后)
- python - 使用 selenium 查找所有具有 onclick 属性的元素?
- java - 语法错误,声明常量时字符无效
- kubernetes - Kubernetes/Openshift 上的 Gitlab-Runner
- javascript - 如果不期望返回,跨域 Ajax 调用是否需要代理?
- c# - 如何将 MIDI 从 Unity 发送到外部 VST
- java - Appium 框架中的非法参数异常
- git - 从文件中删除所有未跟踪
- html - CSS 正确:-50 并且过渡不起作用