首页 > 解决方案 > Kivy:如何在提交按钮后提交 CheckBox 的状态/值

问题描述

我是编码的初学者,我想为我的学生开发一个化学多项选择测验应用程序。

在 Kivy 的代码中选择三个显示的选项之一并点击提交按钮显示选择了正确的答案后,我该如何做?

我有三个复选框和一个提交按钮。单击答案并单击提交按钮后,程序将打印正确或错误。我设法获得了复选框和提交按钮,但我不知道如何编写执行以下操作的内容:

x 单击 CheckBox [user] x 单击提交按钮 [user] x 根类中的函数评估是否选择了正确的 Checkbox。x 函数在带有 id: 结果的标签中打印正确或错误。

我已经尝试过阅读 kivy 的文档,但老实说我不太了解。我在 StackOverFlow 中尝试了不同的答案,例如 (on_active: root.gender = self.text) 方法,但我得到了一个无效的属性“活动”错误。

逻辑代码: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 代码: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'`

标签: pythoncheckboxkivy

解决方案


推荐阅读