shh_algo_PY

Chun Qii - Day 3 - Memory Card

Mar 12th, 2022
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.79 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. from random import shuffle
  8.  
  9. class Question():
  10.     ''' contains the question, one correct answer and three incorrect answers'''
  11.     def __init__(self, question, right_answer, wrong1, wrong2, wrong3):
  12.         self.question = question
  13.         self.right_answer = right_answer
  14.         self.wrong1 = wrong1
  15.         self.wrong2 = wrong2
  16.         self.wrong3 = wrong3
  17.  
  18. #----------------------------------------------------------------------#
  19. #         BUILD THE QUESTION BANK (LIST, use append)                   #
  20. #----------------------------------------------------------------------#
  21.  
  22. questions_list = []
  23. questions_list.append(Question('The state language of Brazil', 'Portuguese', 'English', 'Spanish', 'Brazilian'))
  24. questions_list.append(Question('Which color does not appear on the American flag?', 'Green', 'Red', 'White', 'Blue'))
  25. questions_list.append(Question('A traditional residence of the Yakut people', 'Urasa', 'Yurt', 'Igloo', 'Hut'))
  26.  
  27. #----------------------------------------------------------------------#
  28.  
  29. app = QApplication([])
  30.  
  31. window = QWidget()
  32. window.setWindowTitle('Memory Card')
  33.  
  34. btn_OK = QPushButton('Answer') # answer button
  35. lb_Question = QLabel('The most difficult question in the world!') # question text
  36.  
  37. RadioGroupBox = QGroupBox("Answer options") # on-screen group for radio buttons with answers
  38.  
  39. rbtn_1 = QRadioButton('Option 1')
  40. rbtn_2 = QRadioButton('Option 2')
  41. rbtn_3 = QRadioButton('Option 3')
  42. rbtn_4 = QRadioButton('Option 4')
  43.  
  44. RadioGroup = QButtonGroup() # this groups the radio buttons so we can control their behavior
  45. RadioGroup.addButton(rbtn_1)
  46. RadioGroup.addButton(rbtn_2)
  47. RadioGroup.addButton(rbtn_3)
  48. RadioGroup.addButton(rbtn_4)
  49.  
  50. layout_ans1 = QHBoxLayout()  
  51. layout_ans2 = QVBoxLayout()
  52. layout_ans3 = QVBoxLayout()
  53. layout_ans2.addWidget(rbtn_1) # two answers in the first column
  54. layout_ans2.addWidget(rbtn_2)
  55. layout_ans3.addWidget(rbtn_3) # two answers in the second column
  56. layout_ans3.addWidget(rbtn_4)
  57.  
  58. layout_ans1.addLayout(layout_ans2)
  59. layout_ans1.addLayout(layout_ans3) # put the columns in the same line
  60.  
  61. RadioGroupBox.setLayout(layout_ans1) # a “panel” with the answer options is ready
  62.  
  63. AnsGroupBox = QGroupBox("Test result")
  64. lb_Result = QLabel('Were you correct or not?')
  65. # the word “correct” or “incorrect” will be written here
  66. lb_Correct = QLabel('The answer will be here!')
  67. # the text of the correct answer will be here
  68.  
  69. layout_res = QVBoxLayout()
  70. layout_res.addWidget(lb_Result, alignment=(Qt.AlignLeft | Qt.AlignTop))
  71. layout_res.addWidget(lb_Correct, alignment=Qt.AlignHCenter, stretch=2)
  72. AnsGroupBox.setLayout(layout_res)
  73.  
  74. layout_line1 = QHBoxLayout() # question
  75. layout_line2 = QHBoxLayout() # answer options or test result
  76. layout_line3 = QHBoxLayout() # "Answer" button
  77.  
  78. layout_line1.addWidget(lb_Question, alignment=(Qt.AlignHCenter | Qt.AlignVCenter))
  79. layout_line2.addWidget(RadioGroupBox)  
  80. layout_line2.addWidget(AnsGroupBox)  
  81. AnsGroupBox.hide() # hide the answer panel because the question panel must be visible first
  82.  
  83. layout_line3.addStretch(1)
  84. layout_line3.addWidget(btn_OK, stretch=2) # the button must be large
  85. layout_line3.addStretch(1)
  86.  
  87. layout_card = QVBoxLayout()
  88.  
  89. layout_card.addLayout(layout_line1, stretch=2)
  90. layout_card.addLayout(layout_line2, stretch=8)
  91. layout_card.addStretch(1)
  92. layout_card.addLayout(layout_line3, stretch=1)
  93. layout_card.addStretch(1)
  94. layout_card.setSpacing(5) # spaces between the contents
  95.  
  96. def show_result():
  97.     ''' Show the answer panel '''
  98.     RadioGroupBox.hide()
  99.     AnsGroupBox.show()
  100.     btn_OK.setText('Next question')
  101.  
  102. def show_question():
  103.     ''' Show the answer panel. '''
  104.     RadioGroupBox.show()
  105.     AnsGroupBox.hide()
  106.     btn_OK.setText('Answer')
  107.     RadioGroup.setExclusive(False)
  108.     rbtn_1.setChecked(False)
  109.     rbtn_2.setChecked(False)
  110.     rbtn_3.setChecked(False)
  111.     rbtn_4.setChecked(False)
  112.     RadioGroup.setExclusive(True)
  113.  
  114. answers = [rbtn_1, rbtn_2, rbtn_3, rbtn_4]
  115.  
  116. def ask(q: Question):
  117.     ''' This function writes the value of the question and answers in the corresponding widgets. The answer options are distributed randomly. '''
  118.     shuffle(answers)  
  119.     answers[0].setText(q.right_answer)
  120.     answers[1].setText(q.wrong1)
  121.     answers[2].setText(q.wrong2)
  122.     answers[3].setText(q.wrong3)
  123.     lb_Question.setText(q.question) # question
  124.     lb_Correct.setText(q.right_answer) # answer
  125.     show_question() # show question panel
  126.  
  127. def show_correct(res):
  128.     lb_Result.setText(res)
  129.     show_result()
  130.    
  131.  
  132. def check_answer():
  133.     if answers[0].isChecked():
  134.         show_correct('Correct!')
  135.     else:
  136.         if answers[1].isChecked() or answers[2].isChecked() or answers[3].isChecked():
  137.             show_correct('Incorrect!')
  138.  
  139. #----------------------------------------------------------------------#
  140. #                         NEXT QUESTION!                               #
  141. #----------------------------------------------------------------------#
  142.  
  143. def next_question():
  144.     ''' Asks the next question in the list. '''
  145.     # this function needs a variable that gives the number of the current question
  146.  
  147.     window.current_question = window.current_question + 1 # move on to the next question
  148.    
  149.     if window.current_question >= len(questions_list):
  150.         window.current_question = 0
  151.         # if the list of questions has ended, start over
  152.     q = questions_list[window.current_question] # choose a question
  153.     ask(q) # ask
  154.  
  155. def click_OK():
  156.     ''' This determines whether to show another question
  157.    or check the answer to this question.
  158.    If this looks familiar, we used this in test() before!'''
  159.  
  160.     if btn_OK.text() == 'Answer':
  161.         check_answer() # check the answer
  162.     else:
  163.         next_question() # next question
  164.  
  165. #----------------------------------------------------------------------#
  166. '''Make the current question from the list a property of the “window” object.
  167. That way, we can easily change its functions:'''
  168.  
  169. window.current_question = -1    
  170.  
  171. '''ideally, variables like this one should be properties
  172. we’d have to write a class whose instances have these properties,
  173. but Python allows us to create a property for a single instance'''
  174. #----------------------------------------------------------------------#
  175.  
  176. btn_OK.clicked.connect(click_OK)  # CHANGE TO click_OK
  177.  
  178. #----------------------------------------------------------------------#
  179. '''Everything is set up. Now we ask the question and show the window:'''
  180. next_question()
  181. #----------------------------------------------------------------------#
  182.  
  183. window.setLayout(layout_card)
  184. window.show()
  185. app.exec()
  186.  
Advertisement
Add Comment
Please, Sign In to add comment