教程推荐:PyQt5保姆级教程-- 从入门到精通_pyqt5教程_今晚务必早点睡的博客-CSDN博客
一些摘抄如下:
1.控件提示信息
# 设置字体和字号
QToolTip.setFont(QFont('SansSerif',12))
# 给窗口设置提示,这个方法支持富文本
self.button1 = QPushButton('我的按钮')
# 设置按钮提示
self.button1.setToolTip('这是按钮的提示信息')
2. 状态栏的使用
# 获得状态栏
# self.status = self.statusBar()
#
# # 在状态栏上,设置消息的状态时间5000ms
# self.status.showMessage('只存在5秒的消息',5000)
3.label超链接
# 给label4设置文本内容
label4.setText("<a href='https://www.baidu.com/'>打开百度</a>") # setText里面的内容要用双引号,单引号会报错
# 让label4打开链接
# 如果设为True,用浏览器打开网页,如果设为False,调用槽函数
label4.setOpenExternalLinks(True)
# 让label4的文本右对齐
label4.setAlignment(Qt.AlignRight)
# 给label4设置提示文本
label4.setToolTip('这是一个超链接')
4.热键与伙伴关系
self.setWindowTitle('QLabel与伙伴控件')
# 创建nameLabel控件
#添加热键 添加热键的方法& + 英文字母 ,后面的英文字母就变成了热键。在可视化窗口里通过 "Alt" + 英文字母 就可以起作用
nameLabel =QLabel('&Name',self)
# 创建QLineEdit控件
nameLineEdit = QLineEdit(self)
# 把nameLabel和nameLineEdit设置伙伴关系
nameLabel.setBuddy(nameLineEdit)
# 创建PasswordQLabel控件, 并添加热键
passwordLabel = QLabel('&Password',self)
# 创建PasswordLineEdit控件
passwordLineEdit = QLineEdit(self)
# 把passwordLabel和passwordLineEdit设置成伙伴关系
passwordLabel.setBuddy(passwordLineEdit)
5.lineedit的四种模式
# 设置模式
#1.正常模式
normalLineEdit.setEchoMode(QLineEdit.Normal)
#2.类似于linux的无回显模式,但是内容已经输入
noEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
#3.正常的密码输入后*号模式
passwordLineEdit.setEchoMode(QLineEdit.Password)
#4.密码模式,跳转焦点后,原输入内容会变成*号
passwordEchoONEditLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)
6.设置lineedit限制输入内容
# 创建整数校验器
inValidator = QIntValidator(self)
# 设置整数的范围 [1,99]
inValidator.setRange(1,99)
# 创建浮点校验器
doubleValidator = QDoubleValidator(self)
# 设置浮点校验器[-360,360]
doubleValidator.setRange(-360,-360)
# 小数点的表示
doubleValidator.setNotation(QDoubleValidator.StandardNotation)
# 设置精度,小数点2位
doubleValidator.setDecimals(2)
# 创建数字和字母的正则表达式
reg = QRegExp('[a-zA-Z0-9]+$') # 此时+表示至少有一个
# 创建数字和字母的校验器
validator = QRegExpValidator(self)
# 将正则表达式放置在校验器内
validator.setRegExp(reg)
# 设置校验器
intLineEdit.setValidator(inValidator)
doubleLineEdit.setValidator(doubleValidator)
validatorLineEdit.setValidator(validator)
7.使用掩码限制QLineEdit控件的输入
8.lineedit综合事件
# QLineEdit综合案例
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
# 创建一个类,从QWidget窗口类里面继承
class QLineEditDemo(QWidget):
def __init__(self):
super(QLineEditDemo,self).__init__()
self.initUI()
# 编写初始化方法
def initUI(self):
# 创建多个edit对象
# 创建第一个控件
edit1 = QLineEdit()
# 使用int校验器
edit1.setValidator(QIntValidator())
# 设置文本框最大长度(位数),即不超过9999
edit1.setMaxLength(4)
# 设置文本右对齐
edit1.setAlignment(Qt.AlignRight)
# 设置文本字体为Arial 字号 20
edit1.setFont(QFont('Arial',20))
# 创建第二个控件
edit2 = QLineEdit()
# 使用浮点校验器 范围0.99-99.99 精度为2
edit2.setValidator(QDoubleValidator(0.99,99.99,2))
# 未设置字体字号,对齐方式
# 创建第三个控件
edit3 = QLineEdit()
# 使用掩码 掩码9表示 :ASCⅡ数字字符是必须输入的(0-9)
edit3.setInputMask('99_9999_999999;#') # 后面'#'号指没有输入时,显示为'#'
# 创建第四个控件
edit4 = QLineEdit()
# 绑定事件,当文本变化时,响应到槽
edit4.textChanged.connect(self.textChanged)
# 创建第五个控件
edit5 = QLineEdit()
# 设置回显模式
edit5.setEchoMode(QLineEdit.Password)
# 绑定事件,当编辑完成时,响应到槽
edit5.editingFinished.connect(self.enterPress)
# 创建第六个控件
edit6 =QLineEdit()
# 设为只读
edit6.setReadOnly(True)
# 创建表单布局
formLayout = QFormLayout()
# 把控件添加到表单里
formLayout.addRow('整数校验',edit1)
formLayout.addRow('浮点数校验',edit2)
formLayout.addRow('Input Mask',edit3)
formLayout.addRow('文本变化',edit4)
formLayout.addRow('密码',edit5)
formLayout.addRow('只读',edit6)
# 应用于表单布局
self.setLayout(formLayout)
# 设置窗口的标题
self.setWindowTitle('QLineEdit综合案例')
# 当文本变化时,触发事件
# 定义槽一
def textChanged(self,text):
print('输入的文本:' + text)
# 定义槽二
def enterPress(self):
print('已输入值')
# 防止别的脚本调用,只有自己单独运行时,才会调用下面代码
if __name__ == '__main__':
# 创建app实例,并传入参数
app= QApplication(sys.argv)
# 创建对象
main = QLineEditDemo()
# 创建窗口
main.show()
# 进入程序的主循环,并通过exit函数,确保主循环安全结束(该释放资源的释放资源)
sys.exit(app.exec_())
9.button按钮
def buttonState(self):
# 控件获取数据,获取谁调用了这个槽函数
radioButton = self.sender()
if radioButton.isChecked() == True:
# 如果被选中
print('<' + radioButton.text() + '>被选中')
else:
print('<' + radioButton.text() + '>被取消选中状态')
10.在表格中放控件
# 创建一个button组件
modifyButton = QPushButton('修改')
# 默认是按下状态
modifyButton.setDown(True)
# 使用QSS设置样式 设置所有的QPushButton控件,让它的边距是3px
modifyButton.setStyleSheet('QPushButton{margin:3px};')
# 在单元格放置控件
tableWidget.setCellWidget(0,2,modifyButton)
11.精确搜索tableview中的值
# 搜索满足条件的Cell
text = '(13,1)'
# 精确搜索
items = tableWidget.findItems(text,QtCore.Qt.MatchExactly)
if len(items) > 0:
items = items[0]
# 设置背景色
items.setBackground(QBrush(QColor(0,255,0)))
items.setForeground(QBrush(QColor(255,0,0)))
# 获得当前项所在的行
row = items.row()
# 定位到指定的行
# verticalScrollBar 获得滚动条
tableWidget.verticalScrollBar().setSliderPosition(row)
12.模糊搜索tableview中的值
# 搜索满足条件的Cell
text = '(1'
# MatchStartsWit 以..开头
items = tableWidget.findItems(text, QtCore.Qt.MatchStartsWith)
if len(items) > 0:
#现在是只设置第一个,后期可以写一个for循环后遍历所有符合条件的
items = items[0]
# 设置背景色
items.setBackground(QBrush(QColor(0, 255, 0)))
items.setForeground(QBrush(QColor(255, 0, 0)))
# 获得当前项所在的行
row = items.row()
# 定位到指定的行
# verticalScrollBar 获得滚动条
tableWidget.verticalScrollBar().setSliderPosition(row)
13.定时执行,very nice,自动调用多线程
"""
动态显示当前时间
QTimer 定时器 每隔一定时间会调用一次
QThread
多线程用于同时完成多个任务 在单CPU上是按顺序完成的(时间片切换),从宏观上来看,还是同时完成的
在多CPU上,是可以真正的同时完成
"""
import sys
from PyQt5.QtWidgets import QWidget,QPushButton,QApplication,QListWidget,QGridLayout,QLabel
from PyQt5.QtCore import QTimer,QDateTime
class ShowTime(QWidget):
def __init__(self,parent=None):
super(ShowTime, self).__init__(parent)
# 设置窗口标题
self.setWindowTitle("动态显示当前时间")
# 创建QLabel控件
self.label = QLabel('显示当前时间')
# 创建button按扭
self.startBtn = QPushButton('开始')
# 创建button按钮
self.endBtn = QPushButton('结束')
# 通过栅格布局,安排这三个控件的位置
layout = QGridLayout()
# 设置定时器对象
self.timer = QTimer()
# 时间的 信号 槽
self.timer.timeout.connect(self.showTime)
# 把这三个控件放到栅格布局里面
# 在第一行第一列 占用一行 占用两列
layout.addWidget(self.label,0,0,1,2)
# 在第二行第一列
layout.addWidget(self.startBtn,1,0)
# 在第二行第二列
layout.addWidget(self.endBtn,1,1)
# 开始控件的信号 槽
self.startBtn.clicked.connect(self.startTimer)
# 结束控件的信号 槽
self.endBtn.clicked.connect(self.endTimer)
# 应用于栅格布局
self.setLayout(layout)
# 槽方法
# 显示时间
def showTime(self):
# 获取当前的时间
time = QDateTime.currentDateTime()
# 设置时间显示
timeDisplay = time.toString("yyyy-MM-dd hh:mm:ss dddd")
self.label.setText(timeDisplay)
def startTimer(self):
# 开始时间 1s
#定时每3秒运行一次
self.timer.start(3000)
# 开始之后开始按钮关闭
self.startBtn.setEnabled(False)
# 开始之后关闭按钮开始
self.endBtn.setEnabled(True)
def endTimer(self):
self.timer.stop()
# 开始之后开始按钮开始
self.startBtn.setEnabled(True)
# 开始之后关闭按钮关闭
self.endBtn.setEnabled(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = ShowTime()
demo.show()
sys.exit(app.exec_())
14.通过pyqt打开web网页
"""
用Web浏览器控件(QWebEngineView)显示网页
PyQt5和Web的交互技术
同时使用Python和web开发程序,混合开发
Python + JavaScript + HTML5 + CSS
QWebEngineView 控件,用来显示Web交互界面
"""
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class WebEngineView(QMainWindow):
def __init__(self):
super(WebEngineView, self).__init__()
self.setWindowTitle('打开外部网页例子')
# 在距屏幕宽5px,高30px的坐标,创建一个宽1355,高730的窗口
self.setGeometry(5,30,1355,730)
self.browser = QWebEngineView()
self.browser.load(QUrl('https://www.douban.com/'))
self.setCentralWidget(self.browser)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = WebEngineView()
win.show()
sys.exit(app.exec_())