Creating DropDown in kivy with only kv file(仅使用 kv 文件在 kivy 中创建 DropDown)
问题描述
我想使用 DropDown 类获得一个简单的组合框,例如小部件.我可以使用 python 代码来完成,但是否可以只使用 kv 语言?
I wanted to get a simple combo box like widget using the DropDown class. I can do it using python code, but is it possible using just kv language?
我尝试了以下方法.这是我的python代码:
I tried the following. Here's my python code:
class CustomDropDown(DropDown):
pass
class MainForm(BoxLayout):
pass
class MainApp(App):
def build(self):
self.dropdown = CustomDropDown()
self.mainForm = MainForm()
return self.mainForm
def do_something(self):
self.dropdown.open(self.mainForm)
MainApp().run()
这是 kv 文件:
<MainForm>:
Button:
text: 'Press'
size_hint: [None,None]
height: '40dp'
on_release: app.do_something()
<CustomDropDown>:
Button:
text: 'First Item'
Label:
text: 'Disabled item'
Button:
text: 'Second Item'
但这不起作用.你能建议点什么吗?任何帮助表示赞赏.
But this is not working. Can you please suggest something? Any help is appreciated.
推荐答案
是的,可以使用kivy语言.
Yes, it's possible using kivy language.
您可以阅读 DropDownList 或 Spinner 通过这些链接.此外,如果您想了解更多关于他们的工作,您可能需要查看此 kivy-showcase的链接
You can read about DropDownList or Spinner through these links. And also if you want to know more on their working, you might want to check this link for kivy-showcase
我认为代码是不言自明的.(on_select 方法)
I think the code is self explanatory.(on_select method)
这是 main.py 文件
This is the main.py file
from kivy.app import App
from kivy.uix.dropdown import DropDown
from kivy.uix.boxlayout import BoxLayout
class CustomDropDown(BoxLayout):
pass
class MainApp(App):
def build(self):
return CustomDropDown()
if __name__=='__main__':
MainApp().run()
这是main.kv文件
This is the main.kv file
<CustomDropDown>:
Button:
id: btn
text: 'Press'
on_release: dropdown.open(self)
size_hint_y: None
height: '48dp'
DropDown:
id: dropdown
on_parent: self.dismiss()
on_select: btn.text = '{}'.format(args[1])
Button:
text: 'First Item'
size_hint_y: None
height: '48dp'
on_release: dropdown.select('First Item')
Label:
text: 'Second Item'
size_hint_y: None
height: '48dp'
Button:
text: 'Third Item'
size_hint_y: None
height: '48dp'
on_release: dropdown.select('Third Item')
这篇关于仅使用 kv 文件在 kivy 中创建 DropDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:仅使用 kv 文件在 kivy 中创建 DropDown
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
