└── game /game: -------------------------------------------------------------------------------- 1 | from PyQt5.QtCore import Qt 2 | from random import shuffle 3 | from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QVBoxLayout, QLabel, QMessageBox, QRadioButton, QGroupBox, QButtonGroup 4 | class Question(): 5 | def __init__(self, question, right_answer, wrong1, wrong2, wrong3): 6 | self.question = question 7 | self.right_answer = right_answer 8 | self.wrong1 = wrong1 9 | self.wrong2 = wrong2 10 | self.wrong3 = wrong3 11 | 12 | questions_list = [] 13 | questions_list.append(Question('2+2','4', '5', '6', '7')) 14 | questions_list.append(Question('3+3', '6', '7','10', '0')) 15 | 16 | 17 | 18 | app = QApplication([]) 19 | main_win = QWidget() 20 | main_win.cur_question = -1 21 | 22 | main_win.setWindowTitle('Конкурс от Crazy People') 23 | main_win.resize(400, 200) 24 | box = QGroupBox() 25 | box_answer = QGroupBox()// 26 | layout_box = QVBoxLayout() 27 | answer_text = QLabel('Прав ты или нет!') 28 | layout_box.addWidget(answer_text, alignment=Qt.AlignCenter) 29 | box_answer.setLayout(layout_box) 30 | question = QLabel('В каком году канал получил «золотую кнопку» от YouTube?') 31 | group_btn = QButtonGroup() 32 | btn_answer1 = QRadioButton('2005') 33 | btn_answer2 = QRadioButton('2010') 34 | btn_answer3 = QRadioButton('2015') 35 | btn_answer4 = QRadioButton('2020') 36 | group_btn.addButton(btn_answer1) 37 | group_btn.addButton(btn_answer2) 38 | group_btn.addButton(btn_answer3) 39 | group_btn.addButton(btn_answer4) 40 | 41 | 42 | layout_main = QVBoxLayout() 43 | layoutH1 = QHBoxLayout() 44 | layoutH2 = QHBoxLayout() 45 | layoutH3 = QHBoxLayout() 46 | layoutH1.addWidget(question, alignment = Qt.AlignCenter) 47 | layoutH2.addWidget(btn_answer1, alignment = Qt.AlignCenter) 48 | layoutH2.addWidget(btn_answer2, alignment = Qt.AlignCenter) 49 | layoutH3.addWidget(btn_answer3, alignment = Qt.AlignCenter) 50 | layoutH3.addWidget(btn_answer4, alignment = Qt.AlignCenter) 51 | layoutV1 = QVBoxLayout() 52 | layoutV1.addLayout(layoutH2) 53 | layoutV1.addLayout(layoutH3) 54 | box.setLayout(layoutV1) 55 | btn = QPushButton('Ответить') 56 | layout_main.addLayout(layoutH1) 57 | layout_main.addWidget(box) 58 | layout_main.addWidget(box_answer) 59 | box_answer.hide() 60 | layout_main.addWidget(btn, stretch=2) 61 | main_win.setLayout(layout_main) 62 | 63 | def show_answer(): 64 | box.hide() 65 | box_answer.show() 66 | btn.setText('Следующий вопрос') 67 | 68 | def show_question(): 69 | box.show() 70 | box_answer.hide() 71 | btn.setText('Ответить') 72 | group_btn.setExclusive(False) 73 | btn_answer1.setChecked(False) 74 | btn_answer2.setChecked(False) 75 | btn_answer3.setChecked(False) 76 | btn_answer4.setChecked(False) 77 | group_btn.setExclusive(True) 78 | answer = [btn_answer1, btn_answer2, btn_answer3,btn_answer4] 79 | def ask(q: Question): 80 | shuffle(answer) 81 | answer[0].setText(q.right_answer) 82 | answer[1].setText(q.wrong1) 83 | answer[2].setText(q.wrong2) 84 | answer[3].setText(q.wrong3) 85 | question.setText(q.question) 86 | 87 | show_question() 88 | def show_correct(res): 89 | answer_text.setText(res) 90 | show_answer() 91 | def check_answer(): 92 | if answer[0].isChecked(): 93 | show_correct('правильный ответ') 94 | else: 95 | if answer[1].isChecked() or answer[2].isChecked() or answer[3].isChecked(): 96 | show_correct('неправильный ответ') 97 | def next_question(): 98 | main_win.cur_question += 1 99 | 100 | if main_win.cur_question >= len (questions_list): 101 | main_win.cur_question = 0 102 | q = questions_list[main_win.cur_question] 103 | ask(q) 104 | 105 | 106 | def change(): 107 | if btn.text() == 'Ответить': 108 | check_answer() 109 | else: 110 | next_question() 111 | 112 | next_question() 113 | btn.clicked.connect(change) 114 | main_win.show() 115 | app.exec_() 116 | # comment 117 | --------------------------------------------------------------------------------