本文主要介绍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_())

运行结果如下:

关键代码介绍:

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_())

运行结果如下:

 关键代码介绍:

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

QComboBox下拉列表框

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

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

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

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

  (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_())

运行结果如下:

 关键代码介绍:

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_())

运行结果如下:

 关键代码如下:

    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下拉框三种基本控件的使用方法介绍完了。本文中的内容和实例也基本回答了开篇提到的问题。这三种基本控件的使用简单但也很频繁。可以多动手实践一下。上文中的程序都可以直接运行。可以运行看看效果。

[ PyQt入门教程 ] PyQt5基本控件使用:单选按钮、复选框、下拉框的更多相关文章

  1. [ PyQt入门教程 ] PyQt5基本控件使用:消息弹出、用户输入、文件对话框

    本文主要介绍PyQt界面实现中常用的消息弹出对话框.提供用户输入的输入框.打开文件获取文件/目录路径的文件对话框.学习这三种控件前,先想一下它们使用的主要场景: 1.消息弹出对话框.程序遇到问题需要退 ...

  2. EXTJS 4.2 资料 控件之Grid 行编辑绑定下拉框,并点一次触发一次事件

    主要代码: { header: '属性值', dataIndex: 'PropertyValueName', width: 130, editor: new Ext.form.field.ComboB ...

  3. C# 如何定义让PropertyGrid控件显示[...]按钮,并且点击后以下拉框形式显示自定义控件编辑属性值

    关于PropertyGrid控件的详细用法请参考文献: 1.C# PropertyGrid控件应用心得 2.C#自定义PropertyGrid属性 首先定义一个要在下拉框显示的控件: using Sy ...

  4. 无废话ExtJs 入门教程十[单选组:RadioGroup、复选组:CheckBoxGroup]

    无废话ExtJs 入门教程十[单选组:RadioGroup.复选组:CheckBoxGroup] extjs技术交流,欢迎加群(201926085) 继上一节内容,我们在表单里加了个一个单选组,一个复 ...

  5. MFC控件编程之复选框单选框分组框

    MFC控件编程之复选框单选框分组框 一丶分组框 分组框 英文叫做 GroubBox 添加了分组框主要就是分组.好看.不重点介绍 二丶单选框 英文: Raido Button 单选框需要注意的事项 1. ...

  6. 032——VUE中表单控件处理之复选框的处理

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. [ PyQt入门教程 ] PyQt5环境搭建和配置

    PyQt入门系列教程主要目的是希望通过该系列课程学习,可以使用PyQt5工具快速实现简单的界面开发,包括界面设计.布局管理以及业务逻辑实现(信号与槽).简单说就是可以使用PyQt5工具快速画一个控件摆 ...

  8. [ PyQt入门教程 ] PyQt5中数据表格控件QTableWidget使用方法

    如果你想让你开发的PyQt5工具展示的数据显得整齐.美观.好看,显得符合你的气质,可以考虑使用QTableWidget控件.之前一直使用的是textBrowser文本框控件,数据展示还是不太美观.其中 ...

  9. [ PyQt入门教程 ] PyQt5信号与槽

    信号和槽是PyQt编程对象之间进行通信的机制.每个继承自QWideget的控件都支持信号与槽机制.信号发射时(发送请求),连接的槽函数就会自动执行(针对请求进行处理).本文主要讲述信号和槽最基本.最经 ...

随机推荐

  1. 跟我学SpringCloud | 第五篇:熔断监控Hystrix Dashboard和Turbine

    SpringCloud系列教程 | 第五篇:熔断监控Hystrix Dashboard和Turbine Springboot: 2.1.6.RELEASE SpringCloud: Greenwich ...

  2. C++ 洛谷 P1273 有线电视网 题解

     P1273 有线电视网  很明显,这是一道树形DP(图都画出来了,还不明显吗?) 未做完,持续更新中…… #include<cstdio> #include<cstring> ...

  3. 解决Spring的java项目打包后执行出现“无法读取方案文档...“、“原因为 1) 无法找到文档; 2) 无法读取文档; 3) 文档的根元素不是...”问题

    问题 一个用Spring建的java项目,在Eclipse或idea中运行正常,为什么打包后运行出现如下错误呢? 2019/07/10/19:04:07 WARN [main] org.springf ...

  4. core文件生成和路径设置

    在程序崩溃时,内核会生成一个core文件,即程序最后崩溃时的内存映像,和程序调试信息. 之后可以通过gdb,打开core文件察看程序崩溃时的堆栈信息,可以找出程序出错的代码所在文件和函数. 1.cor ...

  5. POJ 1966:Cable TV Network(最小点割集)***

    http://poj.org/problem?id=1966 题意:给出一个由n个点,m条边组成的无向图.求最少去掉多少点才能使得图中存在两点,它们之间不连通. 思路:将点i拆成a和b,连一条a-&g ...

  6. 02(c)多元无约束优化问题-牛顿法

    此部分内容接<02(a)多元无约束优化问题>! 第二类:牛顿法(Newton method) \[f({{\mathbf{x}}_{k}}+\mathbf{\delta })\text{ ...

  7. 用JavaScript做一個簡單的計算器

    今天繼續學習JavaScript,視頻講的確實挺差勁的.還是只能跟著W3School自己慢慢摸索著弄了.自己百度了一下,參考了一個大佬寫的一個簡單的計算器代碼.代碼能跑通,但是做出來的樣子實在是感覺太 ...

  8. GreenPlum完全安装_GP5.11.3完整安装

    1 概述 1.1 背景 1.2 目标 1.3 使用对象 2 配置系统信息 2.1 配置系统信息,做安装Greenplum的准备工作 Greenplum 数据库版本5.11.3 2.1.1 Greenp ...

  9. Bzoj 2839 集合计数 题解

    2839: 集合计数 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 495  Solved: 271[Submit][Status][Discuss] ...

  10. STM32F4xx系列_独立看门狗配置

    看门狗由内部LSI驱动,LSI是一个内部RC时钟,并不是准确的32kHz,然而看门狗对时间的要求不精确,因此可以接收: 关键字寄存器IWDG_KR: 写入0xCCCCH开启独立看门狗,此时计数器开始从 ...