chiffa
chiffa

Reputation: 2088

Python kivy link checkbox from application to .kv

I am a new user to kivy.

I need to link a checkbox from the .kv interface so that it have an impact on the underlying python application.

I tried the following code for my application:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.checkbox import CheckBox

import os

class LoadDialog(FloatLayout):
    load = ObjectProperty(None)
    cancel = ObjectProperty(None)


class Root(FloatLayout):
    loadfile = ObjectProperty(None)
    checkbox = CheckBox()

    def dismiss_popup(self):
        self._popup.dismiss()

    def show_load(self):
        content = LoadDialog(load=self.load, cancel=self.dismiss_popup)
        self._popup = Popup(title="Load file", content=content, size_hint=(0.9, 0.9))
        self._popup.open()

    def load(self, path, filename):
        print 'user chose: %s' % os.path.join(path, filename[0])
        print self.checkbox.active

        self.dismiss_popup()

    def activate_checkbox(self):
        print 'activate checkbox'
        self.checkbox._toggle_active()


class Chooser(App):
    pass


Factory.register('Root', cls=Root)
Factory.register('LoadDialog', cls=LoadDialog)

if __name__ == '__main__':
    Chooser().run()

with the following chooser.kv file:

#:kivy 1.1.0

Root:

    BoxLayout:
        orientation: 'vertical'
        BoxLayout:
            size_hint_y: 90
            height: 30
            Button:
                text: 'Load'
                on_release: root.show_load()
        BoxLayout:
            size_hint_y: 90
            height: 30
            CheckBox:
                center: self.parent.center
                on_active: root.activate_checkbox
            Label:
                font_size: 20
                center_x: self.parent.width / 4
                top: self.parent.top - 50
                text: 'Faster'


<LoadDialog>:
    BoxLayout:
        size: root.size
        pos: root.pos
        orientation: "vertical"
        FileChooserListView:
            id: filechooser

        BoxLayout:
            size_hint_y: None
            height: 30
            Button:
                text: "Cancel"
                on_release: root.cancel()

            Button:
                text: "Load"
                on_release: root.load(filechooser.path, filechooser.selection)

Unfortunately it was to no avail: it seems that the state of the CheckBox user interface element have no effect whatsoever on the state of the checkbox inside the Root Class.

Is there any simple way to link the two?

Upvotes: 0

Views: 2481

Answers (1)

inclement
inclement

Reputation: 29468

on_active: root.activate_checkbox

This doesn't do anything, you want root.activate_checkbox().

Upvotes: 1

Related Questions