Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5.QtCore import Qt
- from PyQt5.QtWidgets import (
- QApplication, QWidget,
- QHBoxLayout, QVBoxLayout,
- QGroupBox, QButtonGroup, QRadioButton,
- QPushButton, QLabel)
- #===========================================#
- from random import shuffle
- #===========================================#
- app = QApplication([])
- window = QWidget()
- window.setWindowTitle('Memory Card')
- lb_Question = QLabel('Question')
- btn_OK = QPushButton('Answer')
- RadioGroupBox = QGroupBox("Answer options")
- rbtn_1 = QRadioButton('Option 1')
- rbtn_2 = QRadioButton('Option 2')
- rbtn_3 = QRadioButton('Option 3')
- rbtn_4 = QRadioButton('Option 4')
- RadioGroup = QButtonGroup()
- RadioGroup.addButton(rbtn_1)
- RadioGroup.addButton(rbtn_2)
- RadioGroup.addButton(rbtn_3)
- RadioGroup.addButton(rbtn_4)
- layout_ans1 = QHBoxLayout()
- layout_ans2 = QVBoxLayout()
- layout_ans3 = QVBoxLayout()
- layout_ans2.addWidget(rbtn_1)
- layout_ans2.addWidget(rbtn_2)
- layout_ans3.addWidget(rbtn_3)
- layout_ans3.addWidget(rbtn_4)
- layout_ans1.addLayout(layout_ans2)
- layout_ans1.addLayout(layout_ans3)
- RadioGroupBox.setLayout(layout_ans1)
- AnsGroupBox = QGroupBox("Test result")
- lb_Result = QLabel('Did you get it right?')
- lb_Correct = QLabel('The answer will be here!')
- layout_res = QVBoxLayout()
- layout_res.addWidget(lb_Result, alignment=(Qt.AlignLeft | Qt.AlignTop))
- layout_res.addWidget(lb_Correct, alignment=Qt.AlignHCenter, stretch=2)
- AnsGroupBox.setLayout(layout_res)
- layout_line1 = QHBoxLayout()
- layout_line2 = QHBoxLayout()
- layout_line3 = QHBoxLayout()
- layout_line1.addWidget(lb_Question, alignment=(Qt.AlignHCenter | Qt.AlignVCenter))
- layout_line2.addWidget(RadioGroupBox)
- layout_line2.addWidget(AnsGroupBox)
- AnsGroupBox.hide()
- layout_line3.addStretch(1)
- layout_line3.addWidget(btn_OK, stretch=20)
- layout_line3.addStretch(1)
- layout_card = QVBoxLayout()
- layout_card.addLayout(layout_line1, stretch=2)
- layout_card.addLayout(layout_line2, stretch=8)
- layout_card.addStretch(1)
- layout_card.addLayout(layout_line3, stretch=1)
- layout_card.addStretch(1)
- layout_card.setSpacing(5)
- def show_result():
- ''' show answer panel '''
- RadioGroupBox.hide()
- AnsGroupBox.show()
- btn_OK.setText('Next question')
- def show_question():
- ''' show question panel '''
- RadioGroupBox.show()
- AnsGroupBox.hide()
- btn_OK.setText('Answer')
- RadioGroup.setExclusive(False)
- rbtn_1.setChecked(False)
- rbtn_2.setChecked(False)
- rbtn_3.setChecked(False)
- rbtn_4.setChecked(False)
- RadioGroup.setExclusive(True)
- #===========================================#
- answers = [rbtn_1, rbtn_2, rbtn_3, rbtn_4]
- #===========================================#
- # REMOVE THE TEST FUNCTION, WE'RE DONE TESTING!
- '''
- def test():
- if 'Answer' == btn_OK.text():
- show_result()
- else:
- show_question()
- '''
- #===========================================#
- def ask(question, right_answer, wrong1, wrong2, wrong3):
- ''' the function writes the value of the question and answers into the corresponding widgets while distributing the answer options randomly'''
- shuffle(answers)
- answers[0].setText(right_answer)
- answers[1].setText(wrong1)
- answers[2].setText(wrong2)
- answers[3].setText(wrong3)
- lb_Question.setText(question)
- lb_Correct.setText(right_answer)
- show_question()
- def show_correct(res):
- ''' show result - put the written text into "result" and show the corresponding panel '''
- lb_Result.setText(res)
- show_result()
- def check_answer():
- ''' if an answer option was selected, check and show answer panel '''
- if answers[0].isChecked():
- show_correct('Correct!')
- else:
- if answers[1].isChecked() or answers[2].isChecked() or answers[3].isChecked():
- show_correct('Incorrect!')
- #===========================================#
- # QUESTION TIME! BUT ONLY ONE QUESTION
- ask('The national language of Brazil', 'Portuguese', 'Brazilian', 'Spanish', 'Italian')
- #===========================================#
- btn_OK.clicked.connect(check_answer) # Change 'test' to 'check_answer'
- window.setLayout(layout_card)
- window.show()
- app.exec()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement