设置窗口和应用程序图标
窗口的setWindowIcon方法用于设置窗口的图标,只在windows下可用。
QApplication中的setWindowIcon方法用于设置窗口图标和应用程序图标,但调用了窗口的setWindowIcon方法,因此就只能用于设置应用程序的图标了。
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication
from PyQt5.QtGui import QIcon
class FirstMainWin(QMainWindow):
def __init__(self):
super(FirstMainWin, self).__init__()
self.initUI()
def initUI(self):
# 标题
self.setWindowTitle('设置窗口图标')
# 设置尺寸
self.setGeometry(300,300,250,250)
# 设置窗口图标
self.setWindowIcon(QIcon('D:/PycharmProjects/pro2/th.ico'))
if __name__ == '__main__':
app = QApplication(sys.argv)
# 图标
# app.setWindowIcon(QIcon('D:/PycharmProjects/pro2/th.ico'))
main = FirstMainWin()
main.show()
sys.exit(app.exec_())
为控件添加提示信息
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication,QHBoxLayout,QWidget,QPushButton,QToolTip
from PyQt5.QtGui import QFont
class TooltipForm(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
QToolTip.setFont(QFont('SansSerif', 12))
self.setToolTip('今天是<b>星期五</b>')
self.setGeometry(300,300,200,300)
self.setWindowTitle('设置控件提示消息')
self.button1 = QPushButton('我的按钮')
self.button1.setToolTip('这是一个按钮')
layout = QHBoxLayout()
layout.addWidget(self.button1)
mainFrame = QWidget()
mainFrame.setLayout(layout)
# 主框架放在整个窗口上
self.setCentralWidget(mainFrame)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = TooltipForm()
main.show()
sys.exit(app.exec_())
QLabel控件的基本用法
用来展示文本信息
setAlignment():设置文本的对齐方式
setIndent():设置文本缩进
text():获取文本内容
setBuddy():设置伙伴关系
setText():设置文本内容
selectedText():设置所选择的的字符
setWordWrap():设置是否允许换行
常用的信号:
1.当鼠标滑过QLabel控件触发:linkHovered
2.当鼠标单机Qlabel控件触发:linkActivate
import sys
from PyQt5.QtWidgets import QMainWindow,QApplication,QVBoxLayout,QWidget,QLabel
from PyQt5.QtGui import QPalette # 调色板
from PyQt5.QtGui import QPixmap # 图片
from PyQt5.QtCore import Qt
class QLabelDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
label1 = QLabel(self)
label2 = QLabel(self)
label3 = QLabel(self)
label4 = QLabel(self)
# 文本标签
label1.setText('<font color=yellow>这是一个文本标签</font>')
label1.setAutoFillBackground(True)
palette = QPalette()
palette.setColor(QPalette.Window, Qt.blue)
label1.setPalette(palette)
label1.setAlignment(Qt.AlignCenter)
# 链接
label2.setText("<a href='#'>欢迎使用</a>")
# 图片
label3.setToolTip('这是一个图片标签')
label3.setAlignment(Qt.AlignCenter)
label3.setPixmap(QPixmap("D:/PycharmProjects/pro2/th.jpg"))
# 超链接
label4.setText("<a href='https://item.jd.com/12417265.html'>感谢关注《Python从菜鸟到高手》</a>")
label4.setAlignment(Qt.AlignRight)
label4.setToolTip('这是一个超级链接')
vbox = QVBoxLayout()
vbox.addWidget(label1)
vbox.addWidget(label2)
vbox.addWidget(label3)
vbox.addWidget(label4)
self.setLayout(vbox)
self.setWindowTitle('QLabel控件演示')
label2.linkHovered.connect(self.linkHovered)
label4.linkActivated.connect(self.linkClicked)
def linkHovered(self):
print('鼠标滑过label2标签时,触发事件')
def linkClicked(self):
print('鼠标单机label4标签时,触发时间')
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QLabelDemo()
main.show()
sys.exit(app.exec_())
QLabel与伙伴控件
import sys
from PyQt5.QtWidgets import *
class QLabelBuddy(QDialog):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QLabel与伙伴控件')
nameLabel = QLabel('&Name', self) # 标签
nameLineEdit = QLineEdit(self) # 文本框
# 设置伙伴控件
nameLabel.setBuddy(nameLineEdit)
passwordLabel = QLabel('&Password', self) # 标签
passwordLineEdit = QLineEdit(self) # 文本框
# 设置伙伴控件
passwordLabel.setBuddy(passwordLineEdit)
btnOK = QPushButton('&OK')
btnCancel = QPushButton('&Cancel')
mainLayout = QGridLayout(self)
mainLayout.addWidget(nameLabel,0,0) #放置在第1行第1列
mainLayout.addWidget(nameLineEdit,0,1,1,2) #放置在第1行第2列,占1行2列
mainLayout.addWidget(passwordLabel,1,0)
mainLayout.addWidget(passwordLineEdit,1,1,1,2)
mainLayout.addWidget(btnOK,2,1)
mainLayout.addWidget(btnCancel,2,2)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QLabelBuddy()
main.show()
sys.exit(app.exec_())
QLineEdit控件与回显模式
基本功能:输入单行文本
EchoMode(回显模式):
1、Normal
2、NoEcho
3、Password
4、PasswordEchoOnEdit
from PyQt5.QtWidgets import *
import sys
class QLineEditEchoMode(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('文本框输入的回显模式')
formLayout = QFormLayout(self)
normalLineEdit = QLineEdit()
noEchoLineEdit = QLineEdit()
passwordLineEdit = QLineEdit()
passwordEchoOnEditLineEdit = QLineEdit()
formLayout.addRow("Normal", normalLineEdit)
formLayout.addRow("NoEcho", noEchoLineEdit)
formLayout.addRow("Password", passwordLineEdit)
formLayout.addRow("PasswordEchoOnEdit", passwordEchoOnEditLineEdit)
#
normalLineEdit.setPlaceholderText("Normal")
noEchoLineEdit.setPlaceholderText("NoEcho")
passwordLineEdit.setPlaceholderText("Password")
passwordEchoOnEditLineEdit.setPlaceholderText("PasswordEchoOnEdit")
normalLineEdit.setEchoMode(QLineEdit.Normal)
noEchoLineEdit.setEchoMode(QLineEdit.NoEcho)
passwordLineEdit.setEchoMode(QLineEdit.Password)
passwordEchoOnEditLineEdit.setEchoMode(QLineEdit.PasswordEchoOnEdit)
self.setLayout(formLayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QLineEditEchoMode()
main.show()
sys.exit(app.exec_())
限制QLineEdit控件的输入
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIntValidator,QDoubleValidator,QRegExpValidator
from PyQt5.QtCore import QRegExp
class QLineEditValidator(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('校验器')
formLayout = QFormLayout(self)
intLineEdit = QLineEdit()
doubleLineEdit = QLineEdit()
validatorLineEdit = QLineEdit()
formLayout.addRow('整数类型', intLineEdit)
formLayout.addRow('浮点类型', doubleLineEdit)
formLayout.addRow('数字和字母', validatorLineEdit)
intLineEdit.setPlaceholderText('整型')
doubleLineEdit.setPlaceholderText('浮点型')
validatorLineEdit.setPlaceholderText('字母和数字')
# 整数校验器
intValidator = QIntValidator(self)
intValidator.setRange(1,99)
# 浮点校验器
doubleValidator = QDoubleValidator(self)
doubleValidator.setRange(-360,360)
doubleValidator.setNotation(QDoubleValidator.StandardNotation)
doubleValidator.setDecimals(2) # 精度为小数点后两位
# 字母和数字
reg = QRegExp('[a-zA-z0-9]+$')
validator = QRegExpValidator()
validator.setRegExp(reg)
# 绑定关系:文本框、校验器
intLineEdit.setValidator(intValidator)
doubleLineEdit.setValidator(doubleValidator)
validatorLineEdit.setValidator(validator)
self.setLayout(formLayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QLineEditValidator()
main.show()
sys.exit(app.exec_())
QTextEdit控件
import sys
from PyQt5.QtWidgets import *
class QTextEditDemo(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QTextEdit控件')
self.resize(300,280)
self.textEdit = QTextEdit()
self.buttonText = QPushButton('显示文本')
self.buttonHTML = QPushButton('xianshiHTML')
layout = QVBoxLayout()
layout.addWidget(self.textEdit)
layout.addWidget(self.buttonText)
layout.addWidget(self.buttonHTML)
self.setLayout(layout)
self.buttonText.clicked.connect(self.onClick_ButtonText)
self.buttonHTML.clicked.connect(self.onClick_ButtonHTML)
def onClick_ButtonText(self):
self.textEdit.setPlainText('Hello World')
def onClick_ButtonHTML(self):
self.textEdit.setHtml('<font color="blue" size="5">Hello World</font>')
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QTextEditDemo()
main.show()
sys.exit(app.exec_())