带有文本超链接和图像的 QLabel 示例

以下是 QLabel 的示例,它显示文本,图像和超链接的使用。

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

def window():
   app = QApplication(sys.argv)
   win = QWidget() 
    
   l1 = QLabel()
   l2 = QLabel()
   l3 = QLabel()
   l4 = QLabel()
    
   l1.setText("<h1>Hello World</h1>")
   l4.setText("<b>Hello Stack OverFlow</b>")
   l2.setText("<font color='red'>Welcome To Stack Overflow Documentation</font>")
    
   l1.setAlignment(Qt.AlignCenter)
   l3.setAlignment(Qt.AlignCenter)
   l4.setAlignment(Qt.AlignRight)
   l3.setPixmap(QPixmap("python.jpg"))
    
   vbox = QVBoxLayout()
   vbox.addWidget(l1)
   vbox.addStretch()
   vbox.addWidget(l2)
   vbox.addStretch()
   vbox.addWidget(l3)
   vbox.addStretch()
   vbox.addWidget(l4)
    
   l1.setOpenExternalLinks(True)
   l4.linkActivated.connect(clicked)
   l2.linkHovered.connect(hovered)
   l1.setTextInteractionFlags(Qt.TextSelectableByMouse)
   win.setLayout(vbox)
    
   win.setWindowTitle("PyQt4 QLabel Demo")
   win.show()
   sys.exit(app.exec_())
    
def hovered():
   print ("Come'On Click ME")
def clicked():
   print ("You Clicked Me")
    
if __name__ == '__main__':
   window()

代码输出以下结果:

StackOverflow 文档