PyQt5的RadioButton和CheckBox控件

环境

PyQt5 + VSCode
在QtDesigner中新建 Main Window。
将Label、Radio Button、Check Box拖入窗口中,如图
在这里插入图片描述
在VSCode中右键点击文件,选择Compile Form将.ui转换成.py文件Ui_pyqt3.py。
在这里插入图片描述
在Ui_pyqt3.py中添加一行

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(276, 231)
        self.MainWindow = MainWindow  #添加
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

新建文件pyqt3.py。

import sys
from Ui_pyqt3 import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox


class MyMainWindow(QMainWindow,  Ui_MainWindow):
    def __init__(self,  parent=None):   
        super(MyMainWindow,  self).__init__(parent)
        self.setupUi(self)
        
        self.label.setText("主题")
        self.radioButton.setText("标准")  #设置文本
        self.radioButton_2.setText("深色")
        self.radioButton.setChecked(True)  #设置选中

        self.checkBox.setText("开机启动")  #设置文本
        self.checkBox.setChecked(True)  #设置选中
        self.pushButton.setText("确定")
        self.pushButton_2.setText("取消")

        self.radioButton.toggled.connect(self.func1)  #信号:当状态改变时。连接信号和槽
        self.checkBox.stateChanged.connect(self.func2)  #信号:当状态改变时。连接信号和槽


    def func1(self):
        if self.radioButton.isChecked():  #是否选中
            QMessageBox.information(self.MainWindow, "提示", "你选择的主题是 " + self.radioButton.text(),QMessageBox.Ok)
        else:
            QMessageBox.information(self.MainWindow, "提示", "你选择的主题是 " + self.radioButton_2.text(),QMessageBox.Ok)

    def func2(self):
        if self.checkBox.isChecked():  #是否选中
            QMessageBox.information(self.MainWindow, "提示", "选择开机启动",QMessageBox.Ok)
        else:
            QMessageBox.information(self.MainWindow, "提示", "未选择开机启动",QMessageBox.Ok)
            
app = QApplication(sys.argv)
win = MyMainWindow()
win.show()
sys.exit(app.exec_())

按f5运行得到
在这里插入图片描述
RadioButton
方法

方法内容
.setText()设置文本
.text()获取文本
.setChecked(True)设置选中状态
.isChecked()返回选中状态

信号

信号内容
clicked每次点击时
toggled状态改变时

CheckBox
方法

方法内容
.setText()设置文本
.text()获取文本
.setChecked(True)设置选中状态
.isChecked()返回选中状态

信号

信号内容
stateChanged()状态改变时
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用PyQt5将数据插入MySQL数据库的示例代码: ```python import mysql.connector from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QLineEdit, QPushButton class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.lbl1 = QLabel('Name', self) self.lbl1.move(50, 30) self.lbl2 = QLabel('Age', self) self.lbl2.move(50, 70) self.lbl3 = QLabel('Country', self) self.lbl3.move(50, 110) self.name = QLineEdit(self) self.name.move(150, 30) self.age = QLineEdit(self) self.age.move(150, 70) self.country = QLineEdit(self) self.country.move(150, 110) self.btn = QPushButton('Insert', self) self.btn.move(150, 150) self.btn.clicked.connect(self.insert_data) self.setGeometry(300, 300, 300, 200) self.setWindowTitle('Insert Data') self.show() def insert_data(self): name = self.name.text() age = self.age.text() country = self.country.text() # 创建MySQL数据库连接 cnx = mysql.connector.connect(user='username', password='password', host='localhost', database='mydatabase') cursor = cnx.cursor() # 插入数据 add_data = ("INSERT INTO mytable " "(name, age, country) " "VALUES (%s, %s, %s)") data = (name, age, country) cursor.execute(add_data, data) # 提交更改并关闭连接 cnx.commit() cursor.close() cnx.close() if __name__ == '__main__': import sys app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 在这个示例中,我们创建了一个简单的GUI应用程序,用户可以输入姓名,年龄和国家,并将这些信息插入到MySQL数据库中。在 `insert_data()` 方法中,我们创建了一个MySQL连接,然后使用 `cursor.execute()` 方法将数据插入到表中。 注意,我们需要替换示例中的 `username`,`password`,`localhost`,`mydatabase`,`mytable` 为实际的MySQL连接信息和表名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值