Untitled
Anonymous
plain_text
03/03/2026 6:53 PM
4.2 KB
14
Indexable
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
QApplication, QWidget,
QHBoxLayout, QVBoxLayout,
QGroupBox, QRadioButton,
QPushButton, QLabel, QButtonGroup)
app = QApplication([])
btn_OK = QPushButton('Відповісти')
lb_Question = QLabel('The most difficult question in the world!')
RadioGroupBox = QGroupBox("Answer options")
rbtn_1 = QRadioButton('Option 1')
rbtn_2 = QRadioButton('Option 2')
rbtn_3 = QRadioButton('Option 3')
rbtn_4 = QRadioButton('Option 4')
RadioButtonGroup = QButtonGroup()
RadioButtonGroup.addButton(rbtn_1)
RadioButtonGroup.addButton(rbtn_2)
RadioButtonGroup.addButton(rbtn_3)
RadioButtonGroup.addButton(rbtn_4)
layout_ans1 = QHBoxLayout()
layout_ans2 = QVBoxLayout()
layout_ans3 = QVBoxLayout()
layout_ans2.addWidget(rbtn_1) # two answers in the first column
layout_ans2.addWidget(rbtn_2)
layout_ans3.addWidget(rbtn_3) # two answers in the second column
layout_ans3.addWidget(rbtn_4)
layout_ans1.addLayout(layout_ans2)
layout_ans1.addLayout(layout_ans3)
RadioGroupBox.setLayout(layout_ans1)
# Create a results panel
AnsGroupBox = QGroupBox("Test result")
lb_Result = QLabel('Are you correct or not?') # “Correct” or “Incorrect” text will be here
lb_Correct = QLabel('the answer will be here!') # correct answer text will be written 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)
# Place all the widgets in the window:
layout_line1 = QHBoxLayout() # question
layout_line2 = QHBoxLayout() # answer options or test result
layout_line3 = QHBoxLayout() # “Answer” button
layout_line1.addWidget(lb_Question, alignment=(Qt.AlignHCenter | Qt.AlignVCenter))
# Put both panels in the same line; one of them will be hidden and the other will be shown:
layout_line2.addWidget(RadioGroupBox)
layout_line2.addWidget(AnsGroupBox)
AnsGroupBox.hide()
layout_line3.addStretch(2)
layout_line3.addWidget(btn_OK,stretch=4)
layout_line3.addStretch(2)
layout_card = QVBoxLayout()
layout_card.addLayout(layout_line1, stretch=2)
layout_card.addLayout(layout_line2, stretch=8)
layout_card.addStretch(3)
layout_card.addLayout(layout_line3, stretch=1)
layout_card.addStretch(1)
layout_card.setSpacing(5)
def uncheck_buttons():
RadioButtonGroup.setExclusive(False) # мати змогу вимкнути кнопки
rbtn_1.setChecked(False)
rbtn_2.setChecked(False)
rbtn_3.setChecked(False)
rbtn_4.setChecked(False)
RadioButtonGroup.setExclusive(True)
def show_result():
uncheck_buttons()
RadioGroupBox.hide()
AnsGroupBox.show()
btn_OK.setText("Наступне питання")
def show_question():
AnsGroupBox.hide()
RadioGroupBox.show()
btn_OK.setText("Відповісти")
btn_OK.clicked.connect(show_result)
window = QWidget()
window.setLayout(layout_card)
window.setWindowTitle('Memory Card')
window.resize(400,300)
window.show()
app.exec()
Editor is loading...
Leave a Comment