kivy - Kivy 以编程方式将下拉菜单添加到现有布局
问题描述
我想将下拉列表添加到我已经拥有的布局中,但添加时出现问题。布局随机显示选项,但不显示主按钮。我是否添加了不正确的小部件?
注意:我更喜欢动态创建布局,所以我不使用 kv 文件。我看到的所有示例都只是使用 runapp 函数,而不是将下拉列表添加到现有布局中。
我的代码:
Window.size = (Config.getint('graphics','width'),Config.getint('graphics','height'))
Window.top = 30
Window.left = 10
screen_width = 700
screen_height = 775
Window.clearcolor = color_palette["Royal Purple"]
Window.size = (screen_width,screen_height)
Config.write()
##~ dropdown function ~##
def createDropDown():
dropdown = DropDown()
categories = ['Cat1','Cat2','Cat3','Cat4']
for index in range(len(categories)):
btn = Button(text=categories[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))
return dropdown
##~~ My main layout class ~~##
class SimpleKivy0(App):
App.title = "New GUI"
btn_width = 0.2
btn_height = 0.05
txt_fld1 = None
entry_fld_Label = None
def build(self):
layout = FloatLayout()
banner_bground = layout.canvas.before
with banner_bground:
Rectangle(pos=(0,screen_height-(screen_height*0.1)),size=(screen_width, screen_height*0.1))
Color(68/255,210/255,201/255,1)
banner_Lbl = Label(text='Quick Query GUI',halign='left',valign='middle',font_size=((screen_height*0.1)*0.7),pos=(0,screen_height-(screen_height*0.1)),size_hint=(1,0.1))
banner_Lbl.bind(size=banner_Lbl.setter('text_size'))
layout.canvas.add(banner_bground)
layout.add_widget(banner_Lbl)
dropdown = createDropDown()
dropdown.size_hint=(.25,.1)
dropdown.pos = (screen_width*0.2,screen_height*0.2)
return layout
if __name__ == "__main__":
SimpleKivy0().run()
解决方案
对于那些对此也有疑问的人,我遇到了一种称为微调器的替代方案。微调器对象的工作方式与我对弹出窗口的预期相同。
这是我添加的代码而不是下拉列表:
def get_spinner_selection(spinner,text):
print(text)
spinner = Spinner(text='Option1',values=('Option1','Option2','Option3'),size_hint=(0.2,0.1),background_color=(169/255,80/255,18/255,1))
spinner.bind(text=get_spinner_selection)
layout.add_widget(spinner)
queryType_Lbl = Label(text='Query Type:',valign='middle',size_hint=self.spinner.size_hint,pos=(spinner.x,spinner.top))
推荐阅读
- visual-studio - 如何从 Visual Studio 创建 WSP?
- java - Spring @GetMapping 被忽略
- mysql - 如何使用触发器(MySQL)在插入时更改同一表的字段值
- typescript - 我如何模拟单例构造函数中使用的外部库?打字稿和笑话
- java - Twitter API:Twitter 在 ... 错误中具有私有访问权限
- bash - CGI 的 CONTENT_LENGTH 变量是什么?
- amazon-web-services - 如何以html格式查看存储在aws s3存储桶中的html文件?
- excel - 查找范围内最后一个非零单元格的索引或列号
- jquery - ESP-01和chipkit PIC32项目
- python - 如何从类似字符串的列中访问值?