我是编码的初学者,我想为我的学生开发化学选择题测验应用。
在我的Kivy代码中选择三个显示的选项之一并单击“提交”按钮后,显示如何选择了正确的答案,怎么办?
我有三个复选框和一个提交按钮。单击答案并单击提交按钮后,该程序应打印正确或错误。我设法获得了复选框和“提交”按钮,但是我不知道如何编写以下内容:
x单击一个复选框[用户] x单击提交按钮[用户] 如果选择了正确的复选框,则根类中的x函数会求值。 x函数在带有id:结果的Label中打印正确或错误。
我试图遍历文档中的内容,但是老实说,我并不太了解。我在StackOverFlow中尝试过类似(on_active:root.gender = self.text)方法的不同答案,但是我得到了无效的'active'属性。
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()
<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'`