from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.checkbox import CheckBox
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from
kivy.app import App
from kivy.properties import ListProperty
from kivy.metrics import dp
from kivy.uix.label import Label
kv = """
AnchorLayout:
BoxLayout:
size_hint_y: None
height: dp(48)
Label:
text: 'spacer' # these are used to show using a widget to create space in a BoxLayout
Button:
text: 'Scan'
size_hint_x: .5
on_release: app.refresh_ports()
Label:
text: 'spacer' # you can remove the text, or use Widget as a spacer
PortDropDown:
id: port_drop_down
size_hint_x: .5 # this sets the relative width of the dropdown
Label:
text: 'spacer' # the default size_hint is 1, 1
"""
class PortDropDown(Button):
ports = ListProperty(['COM1', 'COM2'])
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.drop_down = DropDown()
self.text = 'Ports'
self.bind(on_release=self.drop_down.open)
# self.drop_down.bind(on_select=lambda instance, x: setattr(self, 'text', x))
self.create_drop_down()
def create_drop_down(self):
for port in self.ports:
cb = CheckBox(active=True, size_hint_x=None, width=dp(32))
port_label = Label(text=port)
entry_layout = BoxLayout(size_hint_y=None, height=dp(48))
entry_layout.add_widget(cb)
entry_layout.add_widget(port_label)
self.drop_down.add_widget(entry_layout)
def on_ports(self, *args): # called when ports is changed
self.drop_down.clear_widgets()
self.create_drop_down()
class PositionDropDown(App):
def build(self):
return Builder.load_string(kv)
def refresh_ports(self):
self.root.ids.port_drop_down.ports = ['COM3', 'COM4', 'COM5', 'COM6']
PositionDropDown().run()