import sys
from PyQt5.QtWidgets import QApplication,QWidget,QRadioButton,QLabel,QVBoxLayout
qAp = QApplication(sys.argv)
natang = QWidget()
vbl = QVBoxLayout()
natang.setLayout(vbl)
vbl.addWidget(QLabel('41 + 23 = ?'))
tualueak1 = QRadioButton('ก. 8')
vbl.addWidget(tualueak1)
tualueak2 = QRadioButton('ข. 64')
vbl.addWidget(tualueak2)
tualueak3 = QRadioButton('ค. 256')
vbl.addWidget(tualueak3)
tualueak4 = QRadioButton('ง. 1024')
vbl.addWidget(tualueak4)
natang.show()
qAp.exec_()
import sys
from PyQt5.QtWidgets import QApplication,QWidget,QButtonGroup,QRadioButton,QLabel,QHBoxLayout,QVBoxLayout
from PyQt5.QtGui import QFont
qAp = QApplication(sys.argv)
natang = QWidget()
hbl = QHBoxLayout()
natang.setLayout(hbl)
font = QFont()
font.setPointSize(16)
font.setFamily('Tahoma')
vbl1 = QVBoxLayout()
hbl.addLayout(vbl1)
klum1 = QButtonGroup()
kho1 = QLabel('อายุ')
kho1.setFont(font)
vbl1.addWidget(kho1)
tualueak11 = QRadioButton('ไม่ถึง 18')
tualueak11.setFont(font)
klum1.addButton(tualueak11)
vbl1.addWidget(tualueak11)
tualueak12 = QRadioButton('18+')
tualueak12.setFont(font)
vbl1.addWidget(tualueak12)
klum1.addButton(tualueak12)
tualueak12.setChecked(True)
vbl2 = QVBoxLayout()
hbl.addLayout(vbl2)
klum2 = QButtonGroup()
kho2 = QLabel('เพศ')
kho2.setFont(font)
vbl2.addWidget(kho2)
tualueak21 = QRadioButton('หญิง')
tualueak21.setFont(font)
vbl2.addWidget(tualueak21)
klum2.addButton(tualueak21)
tualueak21.setChecked(True)
tualueak22 = QRadioButton('ชาย')
tualueak22.setFont(font)
vbl2.addWidget(tualueak22)
klum2.addButton(tualueak22)
natang.show()
qAp.exec_()
import sys
from PyQt5.QtWidgets import QApplication,QWidget,QLabel,QButtonGroup,QRadioButton,QVBoxLayout
qAp = QApplication(sys.argv)
natang = QWidget()
vbl = QVBoxLayout()
natang.setLayout(vbl)
vbl.addWidget(QLabel('11923 + 28179 = ?'))
def lueak(pumlueak):
# pumlueak ในที่นี้จะแทนออบเจ็กต์ของปุ่มที่ถูกเลือก
if(tualueak2==pumlueak): # ดูว่าคำตอบตรงหรือไม่
print('ถูกต้องแล้ว')
else:
print('ตอบผิดแล้ว')
# สร้างปุ่ม
tualueak1 = QRadioButton('39402')
tualueak2 = QRadioButton('40102')
tualueak3 = QRadioButton('40402')
tualueak4 = QRadioButton('41202')
# ใส่ปุ่มลงไปในโครง
vbl.addWidget(tualueak1)
vbl.addWidget(tualueak2)
vbl.addWidget(tualueak3)
vbl.addWidget(tualueak4)
# ใส่ปุ่มลงไปในกลุ่มตัวเลือก
klum = QButtonGroup()
klum.addButton(tualueak1)
klum.addButton(tualueak2)
klum.addButton(tualueak3)
klum.addButton(tualueak4)
klum.buttonClicked.connect(lueak)
natang.show()
qAp.exec_()
import sys
from PyQt5.QtWidgets import QApplication,QWidget,QLabel,QButtonGroup,QRadioButton,QVBoxLayout,QPushButton
qAp = QApplication(sys.argv)
natang = QWidget()
natang.setStyleSheet('color: #157; font-family: Tahoma; font-size: 18px')
vbl = QVBoxLayout()
natang.setLayout(vbl)
vbl.addWidget(QLabel('100 + 200 = ?'))
def yuenyan():
if(klum.checkedButton()==tualueak2):
print('ถูกต้อง')
else:
print('ตอบผิด')
klum = QButtonGroup()
tualueak1 = QRadioButton('ก. 3')
tualueak2 = QRadioButton('ข. 300')
tualueak3 = QRadioButton('ค. 303')
klum.addButton(tualueak1)
klum.addButton(tualueak2)
klum.addButton(tualueak3)
vbl.addWidget(tualueak1)
vbl.addWidget(tualueak2)
vbl.addWidget(tualueak3)
pumkot = QPushButton('ยืนยันคำตอบ')
vbl.addWidget(pumkot)
pumkot.clicked.connect(yuenyan)
natang.show()
qAp.exec_()
import sys
from PyQt5.QtWidgets import QApplication,QWidget,QButtonGroup,QRadioButton,QVBoxLayout
qAp = QApplication(sys.argv)
natang = QWidget()
vbl = QVBoxLayout()
natang.setLayout(vbl)
klum = QButtonGroup()
klum.setExclusive(False)
tualueak = []
for s in ['123','345','567','789','999']:
tualueak.append(QRadioButton(s))
klum.addButton(tualueak[-1])
vbl.addWidget(tualueak[-1])
tualueak[-1].setChecked(True)
natang.show()
qAp.exec_()
ติดตามอัปเดตของบล็อกได้ที่แฟนเพจ