Advertisement
shh_algo_PY

Memory Card Part 2 (2/2)

Feb 26th, 2022
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 KB | None | 0 0
  1. from PyQt5.QtCore import Qt
  2. from PyQt5.QtWidgets import (
  3.         QApplication, QWidget,
  4.         QHBoxLayout, QVBoxLayout,
  5.         QGroupBox, QButtonGroup, QRadioButton,  
  6.         QPushButton, QLabel)
  7.  
  8. #===========================================#
  9. from random import shuffle
  10. #===========================================#
  11.  
  12. app = QApplication([])
  13.  
  14. window = QWidget()
  15. window.setWindowTitle('Memory Card')
  16.  
  17. lb_Question = QLabel('Question')
  18.  
  19. btn_OK = QPushButton('Answer')
  20.  
  21. RadioGroupBox = QGroupBox("Answer options")
  22. rbtn_1 = QRadioButton('Option 1')
  23. rbtn_2 = QRadioButton('Option 2')
  24. rbtn_3 = QRadioButton('Option 3')
  25. rbtn_4 = QRadioButton('Option 4')
  26.  
  27. RadioGroup = QButtonGroup()
  28. RadioGroup.addButton(rbtn_1)
  29. RadioGroup.addButton(rbtn_2)
  30. RadioGroup.addButton(rbtn_3)
  31. RadioGroup.addButton(rbtn_4)
  32.  
  33. layout_ans1 = QHBoxLayout()  
  34. layout_ans2 = QVBoxLayout()
  35. layout_ans3 = QVBoxLayout()
  36.  
  37. layout_ans2.addWidget(rbtn_1)
  38. layout_ans2.addWidget(rbtn_2)
  39. layout_ans3.addWidget(rbtn_3)
  40. layout_ans3.addWidget(rbtn_4)
  41.  
  42. layout_ans1.addLayout(layout_ans2)
  43. layout_ans1.addLayout(layout_ans3)
  44.  
  45. RadioGroupBox.setLayout(layout_ans1)
  46.  
  47. AnsGroupBox = QGroupBox("Test result")
  48. lb_Result = QLabel('Did you get it right?')
  49. lb_Correct = QLabel('The answer will be here!')
  50. layout_res = QVBoxLayout()
  51. layout_res.addWidget(lb_Result, alignment=(Qt.AlignLeft | Qt.AlignTop))
  52. layout_res.addWidget(lb_Correct, alignment=Qt.AlignHCenter, stretch=2)
  53. AnsGroupBox.setLayout(layout_res)
  54.  
  55. layout_line1 = QHBoxLayout()
  56. layout_line2 = QHBoxLayout()
  57. layout_line3 = QHBoxLayout()
  58.  
  59. layout_line1.addWidget(lb_Question, alignment=(Qt.AlignHCenter | Qt.AlignVCenter))
  60.  
  61. layout_line2.addWidget(RadioGroupBox)
  62.  
  63. layout_line2.addWidget(AnsGroupBox)  
  64. AnsGroupBox.hide()
  65.  
  66. layout_line3.addStretch(1)
  67. layout_line3.addWidget(btn_OK, stretch=20)
  68. layout_line3.addStretch(1)
  69.  
  70. layout_card = QVBoxLayout()
  71.  
  72. layout_card.addLayout(layout_line1, stretch=2)
  73. layout_card.addLayout(layout_line2, stretch=8)
  74. layout_card.addStretch(1)
  75. layout_card.addLayout(layout_line3, stretch=1)
  76. layout_card.addStretch(1)
  77. layout_card.setSpacing(5)
  78.  
  79. def show_result():
  80.     ''' show answer panel '''
  81.     RadioGroupBox.hide()
  82.     AnsGroupBox.show()
  83.     btn_OK.setText('Next question')
  84.  
  85. def show_question():
  86.     ''' show question panel '''
  87.     RadioGroupBox.show()
  88.     AnsGroupBox.hide()
  89.     btn_OK.setText('Answer')
  90.     RadioGroup.setExclusive(False)
  91.    
  92.     rbtn_1.setChecked(False)
  93.     rbtn_2.setChecked(False)
  94.     rbtn_3.setChecked(False)
  95.     rbtn_4.setChecked(False)
  96.     RadioGroup.setExclusive(True)
  97.  
  98. #===========================================#
  99. answers = [rbtn_1, rbtn_2, rbtn_3, rbtn_4]
  100. #===========================================#
  101.  
  102. # REMOVE THE TEST FUNCTION, WE'RE DONE TESTING!
  103. '''    
  104. def test():
  105.    if 'Answer' == btn_OK.text():
  106.        show_result()
  107.    else:
  108.        show_question()
  109. '''
  110.  
  111. #===========================================#
  112.  
  113. def ask(question, right_answer, wrong1, wrong2, wrong3):
  114.     ''' the function writes the value of the question and answers into the corresponding widgets while distributing the answer options randomly'''
  115.     shuffle(answers)
  116.     answers[0].setText(right_answer)
  117.     answers[1].setText(wrong1)
  118.     answers[2].setText(wrong2)
  119.     answers[3].setText(wrong3)
  120.     lb_Question.setText(question)
  121.     lb_Correct.setText(right_answer)
  122.     show_question()
  123.  
  124. def show_correct(res):
  125.     ''' show result - put the written text into "result" and show the corresponding panel '''
  126.     lb_Result.setText(res)
  127.     show_result()
  128.  
  129. def check_answer():
  130.     ''' if an answer option was selected, check and show answer panel '''
  131.     if answers[0].isChecked():
  132.         show_correct('Correct!')
  133.     else:
  134.         if answers[1].isChecked() or answers[2].isChecked() or answers[3].isChecked():
  135.             show_correct('Incorrect!')
  136.  
  137. #===========================================#
  138.  
  139. # QUESTION TIME! BUT ONLY ONE QUESTION
  140.  
  141. ask('The national language of Brazil', 'Portuguese', 'Brazilian', 'Spanish', 'Italian')
  142.  
  143. #===========================================#
  144. btn_OK.clicked.connect(check_answer) # Change 'test' to 'check_answer'
  145.  
  146. window.setLayout(layout_card)
  147. window.show()
  148. app.exec()
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement