老师原课件下载地址:
有积分的朋友,支持下我,打赏也OK。
不下载也可以,我每节课会吧代码贴出来
视频链接
课程重点
多选一的按钮
代码位置
完整代码
'''
单选按钮控件(QRadioButton)
'''
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QRadioButtonDemo(QWidget):
def __init__(self):
super(QRadioButtonDemo,self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QRadioButton')
layout = QHBoxLayout()
self.button1 = QRadioButton('单选按钮1')
self.button1.setChecked(True)
self.button1.toggled.connect(self.buttonState)
layout.addWidget(self.button1)
self.button2 = QRadioButton('单选按钮2')
self.button2.toggled.connect(self.buttonState)
layout.addWidget(self.button2)
self.setLayout(layout)
def buttonState(self):
radioButton = self.sender()
if radioButton.isChecked() == True:
print('<' + radioButton.text() + '> 被选中')
else:
print('<' + radioButton.text() + '> 被取消选中状态')
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QRadioButtonDemo()
main.show()
sys.exit(app.exec_())
遇到的问题
改写效果展示
改写注意点
改写代码(QTdesigner模式)
# -*- coding:utf-8 -*-
'''
@Author: knocky
@Blog: https://blog.csdn.net/zzx188891020
@E-mail: 188891020@qq.com
@File: class38.py
@CreateTime: 2020/5/15 15:12
'''
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic
class my_form():
def __init__(self):
super().__init__()
self.ui = uic.loadUi("../ui_package/class38.ui")
self.ui.radioButton.toggled.connect(self.buttonState)
self.ui.radioButton_2.toggled.connect(self.buttonState)
def buttonState(self):
radioButton = self.ui.sender()
if radioButton.isChecked() == True:
print('<' + radioButton.text() + '> 被选中')
else:
print('<' + radioButton.text() + '> 被取消选中状态')
if __name__ == '__main__':
app = QApplication(sys.argv)
main = my_form()
main.ui.show()
sys.exit(app.exec_())