Below here is the source code for Quiz Application for kivy. Please if you like this content. Don't forget to Subscribe to my YouTube Channel here now on Python Programming now or other programming content now and please support me on PayPal Now for better content! and Join our WhatsApp group here or here also with our Telegram group here to start learning now! Thanks!
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.togglebutton import ToggleButton
class ObjectiveQuiz(App):
def __init__(self):
super().__init__()
self.questions = [
{"question": "What is the capital of France?",
"options": ["Paris", "London", "Berlin", "Rome"],
"correct_answer": "Paris"
},
{"question": "What is the capital of United Kingdom?",
"options": ["Paris", "London", "Berlin", "Rome"],
"correct_answer": "London"
},
{"question": "what is 2 + 2",
"options": ["7", "1", "0", "4"],
"correct_answer": "4"
},
{"question": "what continent is Italy on",
"options": ["Africa", "Europe", "North America", "South America"],
"correct_answer": "Europe"
},
{"question": "what is 1 + 1",
"options": ["0", "2", "5", "20"],
"correct_answer": "2"
}
]
self.current_question = 0
self.score = 0
def build(self):
self.queston_label = Label(text=self.questions[self.current_question]["question"])
self.option_buttons = []
for option in self.questions[self.current_question]["options"]:
button = ToggleButton(text=option, group="options", on_press=self.select_option)
self.option_buttons.append(button)
layout = BoxLayout(orientation='vertical')
layout.add_widget(self.queston_label)
for button in self.option_buttons:
layout.add_widget(button)
return layout
def select_option(self, instance):
selected_option = instance.text
correct_answer = self.questions[self.current_question]["correct_answer"]
if selected_option == correct_answer:
self.score += 20
self.current_question +=1
if self.current_question <len(self.questions):
self.update_question()
else:
self.end_quiz()
def update_question(self):
self.queston_label.text = self.questions[self.current_question]["question"]
for button in self.option_buttons:
button.text = self.questions[self.current_question]["options"][self.option_buttons.index(button)]
button.state = "normal"
def end_quiz(self):
self.queston_label.text = "Quiz Finished!"
for button in self.option_buttons:
button.disabled = True
result_label = Label(text=f"Your score is: {self.score}/100")
self.root.add_widget(result_label)
if __name__ == '__main__':
ObjectiveQuiz().run()
from kivy.uix.label import Label
from kivy.uix.togglebutton import ToggleButton
class ObjectiveQuiz(App):
def __init__(self):
super().__init__()
self.questions = [
{"question": "What is the capital of France?",
"options": ["Paris", "London", "Berlin", "Rome"],
"correct_answer": "Paris"
},
{"question": "What is the capital of United Kingdom?",
"options": ["Paris", "London", "Berlin", "Rome"],
"correct_answer": "London"
},
{"question": "what is 2 + 2",
"options": ["7", "1", "0", "4"],
"correct_answer": "4"
},
{"question": "what continent is Italy on",
"options": ["Africa", "Europe", "North America", "South America"],
"correct_answer": "Europe"
},
{"question": "what is 1 + 1",
"options": ["0", "2", "5", "20"],
"correct_answer": "2"
}
]
self.current_question = 0
self.score = 0
def build(self):
self.queston_label = Label(text=self.questions[self.current_question]["question"])
self.option_buttons = []
for option in self.questions[self.current_question]["options"]:
button = ToggleButton(text=option, group="options", on_press=self.select_option)
self.option_buttons.append(button)
layout = BoxLayout(orientation='vertical')
layout.add_widget(self.queston_label)
for button in self.option_buttons:
layout.add_widget(button)
return layout
def select_option(self, instance):
selected_option = instance.text
correct_answer = self.questions[self.current_question]["correct_answer"]
if selected_option == correct_answer:
self.score += 20
self.current_question +=1
if self.current_question <len(self.questions):
self.update_question()
else:
self.end_quiz()
def update_question(self):
self.queston_label.text = self.questions[self.current_question]["question"]
for button in self.option_buttons:
button.text = self.questions[self.current_question]["options"][self.option_buttons.index(button)]
button.state = "normal"
def end_quiz(self):
self.queston_label.text = "Quiz Finished!"
for button in self.option_buttons:
button.disabled = True
result_label = Label(text=f"Your score is: {self.score}/100")
self.root.add_widget(result_label)
if __name__ == '__main__':
ObjectiveQuiz().run()
No comments:
Post a Comment