Abacito
Abacito

Reputation: 151

How to save the value of my checkbox on a pop up window

I have a button that when clicked, a pop up window appears and on this pop up window, there is a list with checkboxes. When I click the checkboxes, they respond and become active or inactive. The problems is that the ticks disappear when I close and reopen the pop up window. What can I do to have some checkboxes active even after the pop up window has been closed.

I've tried using ids where each checkbox will have an id and I would manually have something happen if a check box was checked but that failed because I can't get the state of the check box.

class SwipeDhikrScreen(Screen):
    counter = NumericProperty(0)
    counter_text = StringProperty(str(counter))  #

    def on_touch_move(self, touch):
        if touch.x - touch.ox > 50:
            MainApp.get_running_app().change_screen(screen_name="home_screen", screen_direction="right")

    def dhikr_count(self):
        self.counter += 1
        print(self.counter)
        self.counter_text = str(self.counter)

    def custompop_up(self):  # calls open the pop up custom window
        content = CustomOptionPopUp()
        global Cpop
        Cpop = Popup(title="Custom Dhikr", content=content, size_hint=(None, None), size=(250, 400))
        Cpop.open()

class CustomOptionPopUp(FloatLayout):
    subhanAllah_click = ObjectProperty(None)
    alhamdullilah_check = ObjectProperty(None)

    def checkbox_click(self, instance, value):  # Deals with the okay button of the Custom pop up window
        if value:
            print(self.ids)
            print(self.subhanAllah_click)


    def quit_custom_popup(self):
        Cpop.dismiss()  # closes the custom pop up window

and here is the kv file

<SwipeDhikrScreen>

    FloatLayout:
        GridLayout:
            rows: 1
            pos_hint: {"left": 0.3, "y": 0.7}

            Label:
                text_size: self.size
                size: self.texture_size
                text: "SubhanAllah"
                font_size: 20
                halign: "center"

        Button: # this will be an image button
            id: counter
            text: "Press Me"
            pos_hint: {"x":0.35, "y":0.5}
            size_hint: 0.3, 0.1
            halign: "center"
            on_release: root.dhikr_count()

        Label:
            #id: count_num
            text: root.counter_text
            pos_hint: {"x":0.35, "y":0.43}
            size_hint: 0.3, 0.1
            halign: "center"

        Button:
            text: "Custom"
            pos_hint: {"x": 0.38, "y":0.35}
            size_hint: 0.25, 0.07
            halign: "center"
            on_release: root.custompop_up()


<CustomOptionPopUp>
    checkbox: subhanAllah_check

    Label:
        text: "SubhanAllah"
        font_size: 20
        pos_hint: {"x": 0.1, "y": 0.8}
        size_hint: 0.3, 0.1
        halign: "center"
    CheckBox:
        id: subhanAllah_check
        pos_hint: {"x": 0.6, "y": 0.8}
        size_hint: 0.3, 0.1
        on_active: root.checkbox_click(self, self.active)

    Label:
        text: "Alhamdullilah"
        font_size: 20
        pos_hint: {"x": 0.1, "y": 0.7}
        size_hint: 0.3, 0.1
        halign: "center"
    CheckBox:
        id: alhamdullilah_check
        pos_hint: {"x": 0.6, "y": 0.7}
        size_hint: 0.3, 0.1
        on_active: root.checkbox_click(self, self.active)

    Label:
        text: "AllahuAkbar"
        font_size: 20
        pos_hint: {"x": 0.1, "y": 0.6}
        size_hint: 0.3, 0.1
        halign: "center"
    CheckBox:
        pos_hint: {"x": 0.6, "y": 0.6}
        size_hint: 0.3, 0.1
        on_active: root.checkbox_click(self, self.active)

    Label:
        text: "Lailahaillallah"
        font_size: 20
        pos_hint: {"x": 0.1, "y": 0.5}
        size_hint: 0.3, 0.1
        halign: "center"
    CheckBox:
        pos_hint: {"x": 0.6, "y": 0.5}
        size_hint: 0.3, 0.1
        on_active: root.checkbox_click(self, self.active)



    Button: #OK button
        text: "OK"
        pos_hint: {"x":0.5, "y":0}
        size_hint: 0.5, 0.1
        on_release: root.quit_custom_popup()


Upvotes: 0

Views: 420

Answers (1)

John Anderson
John Anderson

Reputation: 38992

The reason that your CheckBox values get reset is because you are creating a new Popup (and thus, new CheckBoxes) every time the custompop_up() is called. To correct this, just create one Popup to show whenever custompop_up() is called. Something like this:

def custompop_up(self):  # calls open the pop up custom window
    global Cpop
    if Cpop is None:
        content = CustomOptionPopUp()
        Cpop = Popup(title="Custom Dhikr", content=content, size_hint=(None, None), size=(250, 400))
    Cpop.open()

This requires that Cpop be defined before custompop_up() is called, so the following statement should appear in your code outside any class:

Cpop = None

Upvotes: 1

Related Questions