[ PyQt入门教程 ] PyQt5基本控件使用:单选按钮、复选框、下拉框

时间:2023-03-09 20:17:48
[ PyQt入门教程 ] PyQt5基本控件使用:单选按钮、复选框、下拉框

本文主要介绍PyQt5界面最基本使用的单选按钮、复选框、下拉框三种控件的使用方法进行介绍。

1、RadioButton单选按钮/CheckBox复选框。需要知道如何判断单选按钮是否被选中。

2、ComboBox下拉框。需要知道如何对下拉框中的取值进行设置以及代码实现中如何获取用户选中的值。

带着这些问题下面开始介绍这RadioButton单选按钮、CheckBox复选框、ComboBox下拉框三种基本控件的使用方法

QRadioButton单选按钮

单选按钮为用户提供多选一的选择,是一种开关按钮。QRadioButton单选按钮是否选择状态通过isChecked()方法判断。isChecked()方法返回值True表示选中,False表示未选中。

RadioButton示例完整代码如下:

# -*- coding: utf-8 -*-

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QRadioButton class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(309, 126)
self.radioButton = QtWidgets.QRadioButton(Form)
self.radioButton.setGeometry(QtCore.QRect(70, 40, 89, 16))
self.radioButton.setObjectName("radioButton")
self.okButton = QtWidgets.QPushButton(Form)
self.okButton.setGeometry(QtCore.QRect(70, 70, 75, 23))
self.okButton.setObjectName("okButton") self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "RadioButton单选按钮例子"))
self.radioButton.setText(_translate("Form", "单选按钮"))
self.okButton.setText(_translate("Form", "确定")) class MyMainForm(QMainWindow, Ui_Form):
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
self.okButton.clicked.connect(self.checkRadioButton) def checkRadioButton(self):
if self.radioButton.isChecked():
QMessageBox.information(self,"消息框标题","我RadioButton按钮被选中啦!",QMessageBox.Yes | QMessageBox.No) if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainForm()
myWin.show()
sys.exit(app.exec_())

运行结果如下:

[ PyQt入门教程 ] PyQt5基本控件使用:单选按钮、复选框、下拉框

关键代码介绍:

self.radioButton.isChecked()  --> 用于判断RadioButton控件是否被选中。返回值Trule表示按钮被选中,False表示按钮未选中。

QCheckBox复选框

复选框和单选按钮一样都是选项按钮,区别是复选框为用户提供多选多的选择。复选框按钮同样是使用isChecked()方法判断是否被选中。

CheckBox例子完整代码如下:

# -*- coding: utf-8 -*-

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QCheckBox class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(380, 154)
self.freshcheckBox = QtWidgets.QCheckBox(Form)
self.freshcheckBox.setGeometry(QtCore.QRect(50, 40, 71, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.freshcheckBox.setFont(font)
self.freshcheckBox.setObjectName("freshcheckBox")
self.bearcheckBox = QtWidgets.QCheckBox(Form)
self.bearcheckBox.setGeometry(QtCore.QRect(140, 40, 71, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.bearcheckBox.setFont(font)
self.bearcheckBox.setObjectName("bearcheckBox")
self.okButton = QtWidgets.QPushButton(Form)
self.okButton.setGeometry(QtCore.QRect(230, 40, 71, 31))
font = QtGui.QFont()
font.setPointSize(14)
self.okButton.setFont(font)
self.okButton.setObjectName("okButton") self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "CheckBox例子"))
self.freshcheckBox.setText(_translate("Form", "鱼"))
self.bearcheckBox.setText(_translate("Form", "熊掌"))
self.okButton.setText(_translate("Form", "确定")) class MyMainForm(QMainWindow, Ui_Form):
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
self.okButton.clicked.connect(self.checkCheckBox) def checkCheckBox(self):
if self.freshcheckBox.isChecked() and self.bearcheckBox.isChecked():
QMessageBox.information(self,"消息框标题","鱼和熊掌我要兼得!",QMessageBox.Yes | QMessageBox.No) if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainForm()
myWin.show()
sys.exit(app.exec_())

运行结果如下:

[ PyQt入门教程 ] PyQt5基本控件使用:单选按钮、复选框、下拉框

 关键代码介绍:

self.freshcheckBox.isChecked() and self.bearcheckBox.isChecked()  --> 同样适用isChecked()函数判断。

QComboBox下拉列表框

下拉列表框是一个集按钮和下拉选项于一体的控件。通常用于固定的枚举值供用户选择时使用。对于下拉列表框的使用最基本的是要知道如何添加下拉列表框中的值以及如何获取下拉框中选择的值。

(1)如何添加下拉列表框中的值。

1、使用addItem() 添加一个下拉选项或者additems() 从列表中添加下拉选项 方法进行添加。

2、如果使用Qt Designer画图实现,可以将ComboBox控件添加到主界面后双击下拉列表框进行打开添加。如下:

[ PyQt入门教程 ] PyQt5基本控件使用:单选按钮、复选框、下拉框

  (2)如何获取下拉框中的取值

使用函数currentText() 返回选项中的文本进行获取

ComboBox示例完整代码如下:

# -*- coding: utf-8 -*-

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QComboBox class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(400, 130)
self.comboBox = QtWidgets.QComboBox(Form)
self.comboBox.setGeometry(QtCore.QRect(80, 50, 69, 22))
self.comboBox.setObjectName("comboBox")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.comboBox.addItem("")
self.okButton = QtWidgets.QPushButton(Form)
self.okButton.setGeometry(QtCore.QRect(190, 50, 75, 23))
self.okButton.setObjectName("okButton") self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "ComboBox下拉框例子"))
self.comboBox.setItemText(0, _translate("Form", "Python"))
self.comboBox.setItemText(1, _translate("Form", "C++"))
self.comboBox.setItemText(2, _translate("Form", "Go"))
self.comboBox.setItemText(3, _translate("Form", "Java"))
self.okButton.setText(_translate("Form", "确定")) class MyMainForm(QMainWindow, Ui_Form):
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
self.okButton.clicked.connect(self.getComboxBoxValue) def getComboxBoxValue(self):
select_value = self.comboBox.currentText()
QMessageBox.information(self,"消息框标题","你要学%s,为师给你说道说道!" % (select_value,),QMessageBox.Yes | QMessageBox.No) if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainForm()
myWin.show()
sys.exit(app.exec_())

运行结果如下:

[ PyQt入门教程 ] PyQt5基本控件使用:单选按钮、复选框、下拉框

 关键代码介绍:

select_value = self.comboBox.currentText() --> 使用currentText()函数获取下拉框中选择的值

文本框控件(QLineEdit、QTextEdit)

文本框控件分为单行文本框(QLineEdit)和多行文本框(QTextEdit)。单行文本框只允许输入一行字符串。多行文本框可以显示多行文本内容,当文本内容超出控件显示范围时,可以显示水平和垂直滚动条。

针对文本框控件,这里主要了解文本框内容的设置、获取以及清除三种主要方法。单行文本框和多行文本框的设置和获取方法不同,如下。

单行文本框(QLineEdit)方法如下:

setText():设置单行文本框内容。

Text():返回文本框内容

clear():清除文本框内容

多行文本框(QTextEdit)方法如下:

setPlainText():设置多行文本框的文本内容。

toPlainText():获取多行文本框的文本内容。

clear():清除多行文本框的内容

文本框使用实例如下:

# -*- coding: utf-8 -*-

import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QComboBox class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(411, 314)
self.lineEdit = QtWidgets.QLineEdit(Form)
self.lineEdit.setGeometry(QtCore.QRect(120, 50, 251, 41))
self.lineEdit.setObjectName("lineEdit")
self.lineedit_label = QtWidgets.QLabel(Form)
self.lineedit_label.setGeometry(QtCore.QRect(10, 60, 81, 20))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.lineedit_label.setFont(font)
self.lineedit_label.setObjectName("lineedit_label")
self.textEdit = QtWidgets.QTextEdit(Form)
self.textEdit.setGeometry(QtCore.QRect(120, 120, 251, 141))
self.textEdit.setObjectName("textEdit")
self.textedit_label = QtWidgets.QLabel(Form)
self.textedit_label.setGeometry(QtCore.QRect(13, 180, 81, 31))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.textedit_label.setFont(font)
self.textedit_label.setObjectName("textedit_label")
self.run_Button = QtWidgets.QPushButton(Form)
self.run_Button.setGeometry(QtCore.QRect(150, 280, 91, 31))
font = QtGui.QFont()
font.setPointSize(11)
font.setBold(True)
font.setWeight(75)
self.run_Button.setFont(font)
self.run_Button.setObjectName("run_Button") self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "TextEdit_Example"))
self.lineedit_label.setText(_translate("Form", "LineEdit"))
self.textedit_label.setText(_translate("Form", "TextEdit"))
self.run_Button.setText(_translate("Form", "Run")) class MyMainForm(QMainWindow, Ui_Form):
def __init__(self, parent=None):
super(MyMainForm, self).__init__(parent)
self.setupUi(self)
self.run_Button.clicked.connect(self.set_display_edit) def set_display_edit(self):
#设置前先清除文本内容
self.lineEdit.clear()
self.textEdit.clear() #设置文本框内容
self.lineEdit.setText("Lineedit contents")
self.textEdit.setPlainText("Textedit contents") #获取文本框内容,并弹框显示内容
str1 = self.lineEdit.text()
str2 = self.textEdit.toPlainText()
QMessageBox.information(self,"获取信息","LineEdit文本框内容为:%s,TextEdit文本框内容为:%s" %(str1,str2)) if __name__ == "__main__":
app = QApplication(sys.argv)
myWin = MyMainForm()
myWin.show()
sys.exit(app.exec_())

运行结果如下:

[ PyQt入门教程 ] PyQt5基本控件使用:单选按钮、复选框、下拉框

 关键代码如下:

    def set_display_edit(self):
#设置前先清除文本内容
self.lineEdit.clear()
self.textEdit.clear() #设置文本框内容
self.lineEdit.setText("Lineedit contents")
self.textEdit.setPlainText("Textedit contents") #获取文本框内容,并弹框显示内容
str1 = self.lineEdit.text()
str2 = self.textEdit.toPlainText()
QMessageBox.information(self,"获取信息","LineEdit文本框内容为:%s,TextEdit文本框内容为:%s" %(str1,str2))

小结

RadioButton单选按钮、CheckBox复选框、ComboBox下拉框三种基本控件的使用方法介绍完了。本文中的内容和实例也基本回答了开篇提到的问题。这三种基本控件的使用简单但也很频繁。可以多动手实践一下。上文中的程序都可以直接运行。可以运行看看效果。