PyQt5 QLabel标签
- 标签显示
- 标签快捷键功能
标签显示
QLabel背景色设置:
palette = QPalette() # 创建调色板
palette.setColor(QPalette.Window, Qt.green) # 设置调色板属性
label.setPalette(palette) # 标签设置Palette
label.setAutoFillBackground(True) # 设为True设置的Palette才会生效
QLabel鼠标单击和鼠标划过事件:
label.linkActivated.connect(self.link_clicked) # 鼠标单击
label.linkHovered.connect(self.link_hovered) # 鼠标划过
QLabel设置是否允许启动浏览器打开超链:
label.setOpenExternalLinks(True)
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class MyLableWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 800, 600)
self.setWindowTitle("QLable Demo")
self.setWindowIcon(QIcon("./pyqt5/close.png"))
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width())/2, (screen.height() - size.height())/2)
self.initUI()
def initUI(self):
label1 = QLabel(self)
label2 = QLabel(self)
label3 = QLabel(self)
label4 = QLabel(self)
label1.setText("这是一个文本标签")
label1.setAutoFillBackground(True)
palette = QPalette()
palette.setColor(QPalette.Window, Qt.green)
label1.setPalette(palette)
label1.setAlignment(Qt.AlignCenter)
label2.setText("<a href='#'>人生苦短我用Python</a>")
label2.linkActivated.connect(self.link_clicked)
label2.linkHovered.connect(self.link_hovered)
label3.setAlignment(Qt.AlignCenter)
label3.setToolTip("这是一个图片标签")
label3.setToolTipDuration(3000)
label3.setPixmap(QPixmap("./pyqt5/images/big_btn.png"))
label4.setText("<A href='https://www.riverbankcomputing.com/static/Docs/PyQt5/api/qtwidgets/qlineedit.html'>PyQt5 Document</a>")
label4.setAlignment(Qt.AlignLeft)
label4.setToolTip("这是一个超链接标签")
label4.setToolTipDuration(3000)
# 允许访问超链接
label4.setOpenExternalLinks(True)
label4.linkActivated.connect(self.link_clicked)# setOpenExternalLinks 设置为True 不会触发linkActivated
label4.linkHovered.connect(self.link_hovered)
vbox = QVBoxLayout()
vbox.addWidget(label1)
vbox.addStretch()
vbox.addWidget(label2)
vbox.addStretch()
vbox.addWidget(label3)
vbox.addStretch()
vbox.addWidget(label4)
self.setLayout(vbox)
def link_hovered(self):
print("鼠标划过标签")
def link_clicked(self):
print("鼠标点击标签")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MyLableWindow()
win.show()
sys.exit(app.exec_())
标签快捷键功能
nameLb1 = QLabel(‘&Name’, self) # 注意必须是’&'符开头
nameEd1 = QLineEdit(self)
nameLb1.setBuddy(nameEd1) # 设置为伙伴关系, 按“Alt+N”可以切换到nameEd1文本框
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class MyLabelWindow(QDialog):
def __init__(self):
super().__init__()
#self.resize(800, 600)
self.setWindowIcon(QIcon("./pyqt5/images/circel_btn_tap.png"))
self.setWindowTitle("QLabel标签快捷键使用")
screen = QDesktopWidget().screenGeometry()
size = self.geometry()
self.move((screen.width() - size.width())/2, (screen.height() - size.height())/2)
self.initUI()
def initUI(self):
nameLb1 = QLabel('&Name', self)
nameEd1 = QLineEdit(self)
nameLb1.setBuddy(nameEd1) # 设置为伙伴关系, 按“Alt+N”可以切换到nameEd1文本框
nameLb2 = QLabel('&Password', self)
nameEd2 = QLineEdit(self)
nameLb2.setBuddy(nameEd2)
btnOk = QPushButton('&Ok')
btnCancel = QPushButton('&Cancel')
mLayout = QGridLayout(self)
mLayout.addWidget(nameLb1, 0, 0)
mLayout.addWidget(nameEd1, 0, 1, 1,2)
mLayout.addWidget(nameLb2, 1, 0)
mLayout.addWidget(nameEd2, 1, 1, 1,2)
mLayout.addWidget(btnOk, 2, 1)
mLayout.addWidget(btnCancel, 2, 2)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MyLabelWindow()
win.show()
sys.exit(app.exec_())