vendredi 9 août 2019

Kivy: How to submit the state/value of a CheckBox after submit Button

I'm a total beginner in coding and I wanted to develop a Multiple Choice Quiz App in chemistry for my students.

How can I after choosing one of the three displayed Options in my code in Kivy and hitting the submit button display that the right answere was selected?

I have triede to work through the documentation for kivy but I didn't understand much to be honest. I have tried different answeres here in StackOverFlow like the (on_active: root.gender = self.text) method but I got an invalid attribute 'active' error.

Logic code: QuizTestApp.py

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder

Builder.load_file('Question_1.kv')
Builder.load_file('Question_2.kv')

class Question_1(Screen):

#############################################
### The choice function should evaluate the CheckBox and write output if its
### right of wrong.
#############################################

    def choice(self, answer):
        if answer == True:
            output = 'Correct :D'
        else:
            output = 'wrong :/'
        self.ids.result.text = output

###########################################################

class Question_2(Screen):
    pass

sm = ScreenManager()
sm.add_widget(Question_1(name='quest1'))
sm.add_widget(Question_2(name='quest2'))

class QuizApp(App):
    def build(self):
        return sm

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

Kivy code: Question1.kv

<CheckBoxBox@CheckBox>:

<CheckBoxLabel@ButtonBehavior+Label>:

<CheckBoxQuestion@BoxLayout>:
    text: ''
    group: ''

    CheckBoxBox:
        id: cb
        group: root.group

    CheckBoxLabel:
        on_press: cb._do_press()
        text: root.text

<Question_1>:

    BoxLayout:
        orientation: 'vertical'
        Label:
            size_hint_y: 0.2
            text: '1. Question'
            font_size: 30

        BoxLayout:
            orientation: 'horizontal'
            size_hint_y: 0.6

            BoxLayout:
                orientation: 'vertical'
                Label:
                    text: 'Niacin ist giftig'
                Label:
                    text: 'Thiamin ist essentiell'
                Label:
                    text: 'Vitamin C ist wichtig'

            BoxLayout:
                orientation: 'vertical'
                CheckBoxQuestion:
                    text: '1, 2'
                    group: 'opts'
                    id: chk_1

                CheckBoxQuestion:
                    text: '1, 3'
                    group: 'opts'
                    id: chk_2

                CheckBoxQuestion:
                    text: '2, 3'
                    group: 'opts'
                    id: chk_3


        BoxLayout:
            orientation: 'horizontal'
            size_hint_y: 0.2
            Button:
                text: 'back'
            Label:
                id: result
                text: ''

###################################
### The next Button should submit the value/state of the checkbox to the
### choice function 
###################################

            Button:
                text: 'submit'
                on_press: root.choice(chk_3)

####################################

            Button:
                text: 'next'
                on_press: root.manager.current = 'quest2'`




Aucun commentaire:

Enregistrer un commentaire