tags:
categories:
import sys
from PyQt5.QtWidgets import QVBoxLayout, QMainWindow, QApplication, QLabel, QWidget
from PyQt5.QtGui import QPixmap, QPalette
# Qt常量
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("这是一个文本标签.")
label1.setAutoFillBackground(True)
# QPalette(调色板)类相当于对话框或控件的调色板,它管理着控件或窗体的所有颜色信息。
palette = QPalette()
palette.setColor(QPalette.Window, Qt.blue) # 设置背景色
label1.setPalette(palette)
label1.setAlignment(Qt.AlignCenter)
label2.setText("欢迎使用Python GUI程序")
label3.setAlignment(Qt.AlignCenter)
label3.setToolTip('这是一个图片标签')
label3.setPixmap(QPixmap("./images/python.jpg"))
# 如果设为True,用浏览器打开网页,如果设为False,调用槽函数
label4.setOpenExternalLinks(True)
label4.setText("感谢关注《Python从菜鸟到高手》")
label4.setAlignment(Qt.AlignRight)
label4.setToolTip('这是一个超级链接')
vbox = QVBoxLayout()
vbox.addWidget(label1)
vbox.addWidget(label2)
vbox.addWidget(label3)
vbox.addWidget(label4)
label2.linkHovered.connect(self.linkHovered)
label4.linkActivated.connect(self.linkClicked)
self.setLayout(vbox)
self.setWindowTitle('QLabel控件演示')
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_())
from PyQt5.QtWidgets import *
import sys
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行两列
mainLayout.addWidget(nameLineEdit, 0, 1, 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_())
from PyQt5.QtWidgets import *
import sys
class QLineEditEchoMode(QWidget):
def __init__(self):
super(QLineEditEchoMode, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('文本输入框的回显模式')
formLayout = QFormLayout()
normalLineEdit = QLineEdit()
noEchoLineEdit = QLineEdit()
passwordLineEdit = QLineEdit()
passwordEchoOnEditLineEdit = QLineEdit()
formLayout.addRow("Normal", normalLineEdit)
formLayout.addRow("NoEcho", noEchoLineEdit)
formLayout.addRow("Password", passwordLineEdit)
formLayout.addRow("PasswordEchoOnEdit", passwordEchoOnEditLineEdit)
# placeholdertext
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_())
from PyQt5.QtWidgets import *
from PyQt5.QtGui import QIntValidator, QDoubleValidator, QRegExpValidator
from PyQt5.QtCore import QRegExp
import sys
class QLineEditValidator(QWidget):
def __init__(self):
super(QLineEditValidator, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('校验器')
# 创建表单布局
formLayout = QFormLayout()
intLineEdit = QLineEdit()
doubleLineEdit = QLineEdit()
validatorLineEdit = QLineEdit()
formLayout.addRow('整数类型', intLineEdit)
formLayout.addRow('浮点类型', doubleLineEdit)
formLayout.addRow('数字和字母', validatorLineEdit)
intLineEdit.setPlaceholderText('整型')
doubleLineEdit.setPlaceholderText('浮点型')
validatorLineEdit.setPlaceholderText('字母和数字')
# 整数校验器 [1,99]
intValidator = QIntValidator(self)
intValidator.setRange(1, 99)
# 浮点校验器 [-360,360],精度:小数点后2位
doubleValidator = QDoubleValidator(self)
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(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_())
A ASCII字母字符是必须输入的(A-Z、a-z)
a ASCII字母字符是允许输入的,但不是必需的(A-Z、a-z)
N ASCII字母字符是必须输入的(A-Z、a-z、0-9)
n ASII字母字符是允许输入的,但不是必需的(A-Z、a-z、0-9)
X 任何字符都是必须输入的
x 任何字符都是允许输入的,但不是必需的
9 ASCII数字字符是必须输入的(0-9)
0 ASCII数字字符是允许输入的,但不是必需的(0-9)
D ASCII数字字符是必须输入的(1-9)
d ASCII数字字符是允许输入的,但不是必需的(1-9)
# ASCI数字字符或加减符号是允许输入的,但不是必需的
H 十六进制格式字符是必须输入的(A-F、a-f、0-9)
h 十六进制格式字符是允许输入的,但不是必需的(A-F、a-f、0-9)
B 二进制格式字符是必须输入的(0,1)
b 二进制格式字符是允许输入的,但不是必需的(0,1)
> 所有的字母字符都大写
< 所有的字母字符都小写
! 关闭大小写转换
\ 使用"\"转义上面列出的字符
from PyQt5.QtWidgets import *
import sys
class QLineEditMask(QWidget) :
def __init__(self):
super(QLineEditMask,self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('用掩码限制QLineEdit控件的输入')
formLayout = QFormLayout()
ipLineEdit = QLineEdit()
macLineEdit = QLineEdit()
dateLineEdit = QLineEdit()
licenseLineEdit = QLineEdit()
# 192.168.21.45
# ;后代表没有输入时候占位符号
ipLineEdit.setInputMask('000.000.000.000;_')
macLineEdit.setInputMask('HH:HH:HH:HH:HH:HH;_')
dateLineEdit.setInputMask('0000-00-00')
licenseLineEdit.setInputMask('>AAAAA-AAAAA-AAAAA-AAAAA-AAAAA;#')
formLayout.addRow('数字掩码',ipLineEdit)
formLayout.addRow('Mac掩码', macLineEdit)
formLayout.addRow('日期掩码',dateLineEdit)
formLayout.addRow('许可证掩码',licenseLineEdit)
self.setLayout(formLayout)
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QLineEditMask()
main.show()
sys.exit(app.exec_())
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
import sys
class QLineEditDemo(QWidget):
def __init__(self):
super(QLineEditDemo, self).__init__()
self.initUI()
def initUI(self):
edit1 = QLineEdit()
# 使用int校验器
edit1.setValidator(QIntValidator())
edit1.setMaxLength(4) # 不超过9999
edit1.setAlignment(Qt.AlignRight)
edit1.setFont(QFont('Arial', 20))
edit2 = QLineEdit()
edit2.setValidator(QDoubleValidator(0.99, 99.99, 2))
edit3 = QLineEdit()
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('Hello PyQt5')
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 = QApplication(sys.argv)
main = QLineEditDemo()
main.show()
sys.exit(app.exec_())
from PyQt5.QtWidgets import *
import sys
class QTextEditDemo(QWidget):
def __init__(self):
super(QTextEditDemo, self).__init__()
self.initUI()
def initUI(self):
self.setWindowTitle('QTextEdit控件演示')
self.resize(300, 320)
self.textEdit = QTextEdit()
self.buttonText = QPushButton('显示文本')
self.buttonHTML = QPushButton('显示HTML')
self.buttonToText = QPushButton('获取文本')
self.buttonToHTML = QPushButton('获取HTML')
layout = QVBoxLayout()
layout.addWidget(self.textEdit)
layout.addWidget(self.buttonText)
layout.addWidget(self.buttonToText)
layout.addWidget(self.buttonHTML)
layout.addWidget(self.buttonToHTML)
self.setLayout(layout)
self.buttonText.clicked.connect(self.onClick_ButtonText)
self.buttonHTML.clicked.connect(self.onClick_ButtonHTML)
self.buttonToText.clicked.connect(self.onClick_ButtonToText)
self.buttonToHTML.clicked.connect(self.onClick_ButtonToHTML)
def onClick_ButtonText(self):
self.textEdit.setPlainText('Hello World,世界你好吗?')
def onClick_ButtonToText(self):
print(self.textEdit.toPlainText())
def onClick_ButtonHTML(self):
self.textEdit.setHtml('Hello World')
def onClick_ButtonToHTML(self):
print(self.textEdit.toHtml())
if __name__ == '__main__':
app = QApplication(sys.argv)
main = QTextEditDemo()
main.show()
sys.exit(app.exec_())