vendredi 28 février 2020

How do you make a checkbox checked by default in Kivy?

I have created two checkboxes in a .KV file (one for default settings and one for custom keywords) and I am trying to get the default box to be checked when the GUI opens this window initially. I am able to get the default value output to be set correctly however, I can't get the actual checkbox to visually show it is selected when the window is initially shown.

The goal of this program is to make a basic graphic user interface for an existing simulation software my lab has created and completed previously.

I am pretty new to Kivy so I am not sure if I am doing something fundamentally wrong or if I just don't understand how checkboxes work properly.

I have tried both methods I've seen on other related posts

defaultCheckbox = ObjectProperty(True) #in .py file

and I've tried adding

active: True # in the .kv file

Here is the checkbox portion of the .kv file

The canvassing section was replicated from another stackoverflow post where the box behind a checkbox was not showing up when a page is rendered and thus the solution was to draw it manually.

            FloatLayout:
                BoxLayout:
                    orientation: "horizontal"
                    height: 20
                    pos_hint: {'center': (0.5, 0.7)}

                    FloatLayout:
                        Label:
                            text: "Default \n Keywords"
                            size_hint_x: 1
                            halign: 'center'
                            pos_hint: {'center': (0.2, 0.5)}
                            font_size: 30

                    #todo make default kwargs checkbox active on program boot
                    CheckBox:
                        id: defaultCheckbox
                        canvas.before:
                            Color:
                                rgb: 30,35,38
                            Ellipse:
                                pos:self.center_x-11, self.center_y-11
                                size:[22,22]
                            Color:
                                rgb: 0,0,0
                            Ellipse:
                                pos:self.center_x-10, self.center_y-10
                                size:[20,20]
                        on_active: root.default_click(self, self.active)
                        size_hint_x: .20
                        group: "keywords"

                    Label:
                        text: " "
                        valign: 'bottom'


                    FloatLayout:
                        Label:
                            text: "Define \n Experiment \n Keywords"
                            size_hint_x: 1
                            halign: 'center'
                            pos_hint: {'center': (0.15, 0.5)}
                            font_size: 25


                    CheckBox:
                        canvas.before:
                            Color:
                                rgb: 30,35,38
                            Ellipse:
                                pos:self.center_x-11, self.center_y-11
                                size:[22,22]
                            Color:
                                rgb: 0,0,0
                            Ellipse:
                                pos:self.center_x-10, self.center_y-10
                                size:[20,20]
                        on_active: root.custom_click(self, self.active)
                        size_hint_x: .20
                        group: "keywords"

and here is the related portion of the .py file

class PhysicsModeling(Screen):
    kwargPopup = ObjectProperty(None)
    physicsModel = ObjectProperty(None)
    initialConditions = ObjectProperty(None)
    default = ObjectProperty(True)
    defaultExpKwargs = {'G0' : 1., 'G1' : .1, 'G2': 1., 'MU':1.,  'scaling' : 25, "solution1" : 1,"solution2" : 3, "orientation2" : "x", "y_shift2" : 1./4.}
    customExpKwargs = ObjectProperty(None)
    defaultCheckbox = BooleanProperty(True)


    def physicsmodel_spinner_clicked(self, value):
        print("Physics Model selected is " + value)

    def initialconditions_spinner_clicked(self, value):
        print("Intial Conditions " + value + " Selected")

    def default_click(self, instance, value):
        if value is True:
            PhysicsModeling.default = True
            print("Checkbox Checked")

        else:
            PhysicsModeling.default = False
            print("Checkbox Unchecked")

    def custom_click(self, instance, value):
        #todo add popup window for custom kwargs
        if value is True:
            PhysicsModeling.default = False
            print("Checkbox Checked")
            PhysicsModeling.default = True
            popup = Popup(title='Define Keyword Arguments', content=Label(text='Test Popup'),
                          auto_dismiss=True, size_hint=(0.5,0.5), pos_hint={'x': 0.25,
                            'y':0.25})
            popup.open()
        else:
            print("Checkbox Unchecked")

    def next_button(self):
        outputArray[11] = self.physicsModel.text
        outputArray[10] = self.initialConditions.text
        print("NEXT button pressed!!")
        print("Batches Selected ", self.physicsModel.text)
        print("Run Solutions ", self.initialConditions.text)
        if self.default == True:
            outputArray[12] = self.defaultExpKwargs
            print("Default KWargs Selected")
        else:
            print("Custom KWargs are DEFINED")
            outputArray[12] = self.customExpKwargs





Aucun commentaire:

Enregistrer un commentaire