如何解决如何创建包含按钮和标签的下拉菜单
我正在使用kivy(python),并且需要下拉菜单而不是textinput放置在这里。但我无法执行此操作,请帮助我继续工作。我的目标是将这些textinputs替换为下拉菜单。 enter image description here
py文件的代码是
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.lang import Builder
class CustomDropDown(DropDown):
pass
class delivery_managementWindow(BoxLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.dropdown = CustomDropDown()
self.mainbutton = Button(text ='Do you in college?',size_hint_x = 0.6,size_hint_y = 0.15)
self.add_widget(self.mainbutton)
self.mainbutton.bind(on_release = self.dropdown.open)
self.dropdown.bind(on_select = lambda\
instance,x: setattr(self.mainbutton,'text',x))
self.dropdown.bind(on_select = self.callback)
def callback(self,instance,x):
print ( "The chosen mode is: {0}" . format ( x ) )
class delivery_managementApp(App):
def build(self):
return delivery_managementWindow()
if __name__ =='__main__':
delivery_managementApp().run()
现在它的kv文件也是
<CustomDropDown>:
Button:
text: 'College Name'
size_hint_y: None
height: 44
on_release: root.select('College is')
Label:
text: 'Not in college'
size_hint_y: None
height: 44
Button:
text: 'KccItm'
size_hint_y: None
height: 44
on_release: root.select('Kcc')
<delivery_managementWindow>:
id:delivery_managment_main_window
orientation:'vertical'
padding:5
spacing:5
canvas.before:
Color:
rgba:(.1,.30,.35,1)
Rectangle:
pos:self.pos
size:self.size
BoxLayout:
id:header
size_hint_x:1
size_hint_y:None
height:50
canvas.before:
Color:
rgba:(.06,.45,1)
Rectangle:
pos:self.pos
size:self.size
Label:
text:"Delivery Management"
bold: True
font_size:18
# color:(.06,1)
BoxLayout:
id:''
orientation:'horizontal'
BoxLayout:
id:add_edit_form
orientation:'vertical'
size_hint_y:1
size_hint_x:1.5
spacing:5
padding:5
# height:40
canvas.before:
Color:
# rgba:(.02,.25,1)
Rectangle:
pos:self.pos
size:self.size
BoxLayout:
size_hint_y:None
size_hint_x:1
# padding:5
height:40
canvas.before:
Color:
rgba:(1,1,.8)
Rectangle:
pos:self.pos
size:self.size
Label:
id:lbl_header
text:"Add New/Edit Delivery"
font_size:17
bold: True
color:(0,1)
GridLayout:
id:grd
cols:2
size_hint_y:1
size_hint_x:1
canvas.before:
Color:
rgba:(.06,1)
Rectangle:
pos:self.pos
size:self.size
Label:
text:"Delivery :"
bold: True
font_size:17
BoxLayout:
id:dropdown_box
Label:
text:"Our Ref: Inv-sup :"
bold: True
font_size:17
TextInput:
Label:
text:"Invoice :"
bold: True
font_size:17
TextInput:
Label:
text:"Supplier :"
bold: True
font_size:17
TextInput:
Label:
text:"Date :"
bold: True
font_size:17
TextInput:
BoxLayout:
cols:2
CheckBox:
Label:
text:'Delivery for Store Room'
Button:
id:btn_proceed
text:'Proceed Next'
BoxLayout:
id:keyboard
size_hint_y:1
size_hint_x:1
canvas.before:
Color:
rgba:(0,1)
Rectangle:
pos:self.pos
size:self.size
BoxLayout:
id:keypad_section
orientation:'vertical'
size_hint_y:1
size_hint_x:.7
# padding:5
canvas.before:
Color:
rgba:(.1,1)
Rectangle:
pos:self.pos
size:self.size
AnchorLayout:
size_hint_x:1
size_hint_y:.2
anchor_x: 'center'
anchor_y: 'center'
canvas:
Color:
rgba:(1,.9)
Rectangle:
pos: self.pos
size: self.size
Button:
text: 'Go To Sale'
size_hint_y:.7
size_hint_x:.5
font_size:17
bold: True
background_normal:''
background_color:(.06,1)
GridLayout:
cols:3
id:keypad
size_hint_y:1
size_hint_x:1
spacing:5
padding:5
canvas.before:
Color:
rgba:(.1,1)
Rectangle:
pos:self.pos
size:self.size
Button:
text:'1'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'2'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'3'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'4'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'5'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'6'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'7'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'8'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'9'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'00'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'0'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'*'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'<--'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'-'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
Button:
text:'Enter'
bold: True
font_size:18
background_normal:''
background_color:(.06,1)
BoxLayout:
id:btn_section
size_hint_y:.2
size_hint_x:1
spacing:5
padding:5
canvas.before:
Color:
rgba:(.1,1)
Rectangle:
pos:self.pos
size:self.size
Button:
id:btn_exit
text:'Exit'
size_hint_x:1
size_hint_y:1
font_size:18
bold:True
color:(.8,.8)
background_normal:''
background_color:(.06,1)
size_hint_x:1
size_hint_y:1
Button:
id:btn_finish
text: "Finish Delivery"
font_size:18
bold:True
background_normal:''
background_color:(.06,1)
size_hint_x:1
size_hint_y:1
解决方法
您可以创建一个DropDown
来替换每个TextInput
。我将提供一个示例,说明如何针对投放选项执行此操作,您可以针对所需的任何其他DropDowns
遵循该模式。
首先,在DropDown
中为新的kv
定义主按钮:
Label:
text:"Delivery :"
bold: True
font_size:17
# BoxLayout:
# id:dropdown_box
Button:
id: del_opt_main_butt
text: 'Select delivery option'
on_release: root.open_delivery_options_dropdown()
然后为新的DropDown
(也在kv
中)制定规则:
<DeliveryOptionDropDown>:
on_select:
app.root.ids.del_opt_main_butt.text = args[1]
app.delivery_option_callback(*args)
Button:
text: 'Overnight'
size_hint_y: None
height: 44
on_release: root.select(self.text)
Button:
text: 'Two Day'
size_hint_y: None
height: 44
on_release: root.select(self.text)
Button:
text: 'Snail Mail'
size_hint_y: None
height: 44
on_release: root.select(self.text)
并定义新的DropDown
类:
class DeliveryOptionDropDown(DropDown):
pass
然后将方法打开新的Dropdown
到delivery_managementWindow
类:
def open_delivery_options_dropdown(self):
self.delivery_options_dropdown = DeliveryOptionDropDown()
self.delivery_options_dropdown.open(self.ids.del_opt_main_butt)
然后将回调添加到App
类:
def delivery_option_callback(self,instance,selection):
print('chosen delivery option is',selection)
仅出于简单起见,后两个方法位于delivery_managementWindow
和App
类中(root
和app
是kv
中的预定义变量)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。