[Python] How do I get signals of checkboxes checked in mdlist?

Discussão em 'Python' iniciado por Stack, Março 28, 2025.

  1. Stack

    Stack Membro Participativo

    Using KivyMD and Python, when the program displays the mdlist you can check the checkboxes but can't access the on_active status:

    from kivy.uix.gridlayout import GridLayout
    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.core.window import Window
    from kivy.lang import Builder
    from kivy.uix.screenmanager import ScreenManager, Screen
    from kivymd.app import MDApp
    from kivymd.uix.screen import Screen
    from kivymd.uix.list import MDList, MDListItemHeadlineText,MDListItem,MDListItemLeadingIcon,\
    MDListItemSupportingText,MDListItemTertiaryText,MDListItemTrailingCheckbox
    from kivy.uix.scrollview import ScrollView

    class Origin(Screen):

    def test2(self):
    print('something.......')
    def full_list(self):


    Creating a list to simulate a fetchall result:

    listadeobjetos = [['ana','Belgium','blue'],['juan','Uruguay','yellow'],['mary','Congo','brown']]

    list_view = MDList()


    Trying to put an index number to each row:

    numero_fila = 0

    for i in listadeobjetos:
    fila=MDListItem(
    MDListItemHeadlineText(
    text = 'NAME: ' + i[0],
    ),
    MDListItemSupportingText(
    text= 'COUNTRY ' + i[1] ,
    ),
    MDListItemTertiaryText(
    text='ONE COLOR '+ i[2] ,
    ),
    MDListItemTrailingCheckbox(

    # here are all my attempts

    group= 'seleccion',
    #on_active= lambda x= 'A': self.parent.ids.Results.testcheck(x),
    #on_active= self.parent.ids.Origin.test2,
    #on_active= self.parent.ids.Results.test3,
    on_active= lambda x= 'SOMETHING': print(x),
    ),
    )
    numero_fila += 1
    fila.focus_color= 1,0,1,.33
    fila.id= str(numero_fila)
    list_view.add_widget(fila)

    self.parent.ids.Results.ids.scroll.add_widget(list_view)

    self.parent.current = 'RESULTADOS'

    class Results(Screen):

    def testcheck(self, ins ):
    print('TRYING TO GET SOMETHING .....'+ ins)

    def test3(self):
    print('maybehere.....')

    class Window_Manager(ScreenManager):
    pass




    trust = Builder.load_file('the_question.kv')

    class MrhApp(MDApp):

    def build(self):
    Window.borderless = True
    return trust


    app = MrhApp()
    app.run()


    kv file:

    #:kivy 2.3.1

    Window_Manager:
    Origin:
    id: Origin
    Results:
    id: Results

    <Origin>
    id:'Origin'
    name: 'ORIGIN'
    canvas.before:
    Color:
    rgba: 0.188, 0.188, 0.204,1
    Rectangle:
    pos: self.pos
    size: self.size
    GridLayout:
    cols: 1
    rows: 2
    row_force_default: True
    row_default_height: 50
    padding: 10
    spacing:40

    GridLayout:
    cols: 3
    rows: 1
    size_hint_y: .08
    padding: 10

    Label:
    size_hint_y: None
    size_hint_x: None
    width: 50
    height: 50
    pos_hint: {'center_x':0.2}
    text: 'PLEASE'

    Label:
    id:banner_centro
    text:'WILL BE A QUESTION'
    halign: 'center'
    color: 1,0,0,1
    font_size: 20

    Button:
    size_hint_y: None
    size_hint_x: None
    width: 50
    height: 50
    pos_hint: {'center_x':0.8}
    on_release: app.stop()
    background_color: 0.188, 0.188, 0.204,1
    background_normal: ""
    text: 'LOGOUT'

    GridLayout:
    cols: 2
    spacing: 50,20
    padding: 10,10



    Label:
    text: 'CLICK ON BUTTON TO GET FULL LIST'

    Button:
    size_hint: None,None
    height: 150
    width: 135
    id: fulllist
    on_release: app.root.current= root.full_list()
    background_color: 0.188, 0.188, 0.204,1
    background_normal: ""
    text: 'FULL LIST'



    <Results>
    name: 'RESULTADOS'

    GridLayout:
    cols: 1
    rows: 3
    padding: 10
    spacing:20
    canvas.before:
    Color:
    rgba: 0.188, 0.188, 0.204,1
    Rectangle:
    pos: self.pos
    size: self.size
    GridLayout:
    cols: 3
    rows: 1
    size_hint_y: .08
    padding: 10

    Label:
    size_hint_y: None
    size_hint_x: None
    width: 50
    height: 50
    pos_hint: {'center_x':0.2}
    text: ' here was an image'

    Label:
    id:banner_centro
    text:'this is the question'
    halign: 'center'
    font_size: 20
    color: 1,0,0,1

    Button:
    size_hint_y: None
    size_hint_x: None
    width: 50
    height: 50
    pos_hint: {'center_x':0.8}
    on_release: app.stop()
    background_color: 0.188, 0.188, 0.204,1
    background_normal: ""
    text: 'LOGOUT'



    ScrollView:

    id:scroll
    size_hint_y: .8
    padding: 5



    GridLayout:
    cols: 1
    rows: 1
    spacing: 50,50
    padding:10
    size_hint_y: .08

    Button:
    id: button_back
    background_color: 0.188, 0.188, 0.204,1
    text:'BACK'
    size_x: 40
    color: 0.773, 0.525, 0.714,1
    on_release: app.root.current= 'ORIGIN'

    Continue reading...

Compartilhe esta Página