Python/pyqt5/CODE/qt04x範例程式/qt0406_QLabel.py
2024-06-27 15:41:10 +08:00

55 lines
2.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
class QLabelDemo(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QLabel範例")
label1, label2, label3 = QLabel(self), QLabel(self), QLabel(self)
#label1, label2, label3 = [QLabel(self) for i in range(3)]
label1.setText("這是一個文字標籤") #設定標籤外顯文字
label1.setAlignment(Qt.AlignCenter) #置中顯示
label2.setToolTip('這是一個圖片標籤') #設定氣泡說明
label2.setPixmap(QPixmap("LINE_friends.gif")) #底圖設入標籤之中
#label2.setPixmap(QPixmap("LINE_friends.gif").scaled(400,400,Qt.KeepAspectRatio))
# ------ 開始:設定動畫 ------ #
# movie = QMovie("LINE_friends.gif") #創建QMovie物件載入動畫圖檔
# movie.setSpeed(100) #設定播放速度100為原速200為2倍速依此類推
# movie.setScaledSize(QSize().scaled(400,400,Qt.KeepAspectRatio)) #設定大小
# movie.start() #開始播放,記得加這個指令,否則看不到圖案
# label2.setMovie(movie) #動畫物件嵌入label2標籤內
# ------ 結束:設定動畫 ------ #
label3.setText("<A href='https://www.yuntech.edu.tw/'>歡迎連結雲科大首頁</a>")
label3.setAlignment(Qt.AlignRight) #靠右顯示
label3.setToolTip('這是一個超連結標籤')
vbox = QVBoxLayout() #創建垂直式佈局排版管理員
vbox.addWidget(label1) #利用.addWidget()逐一把元件加入版面,佈局管理員將自動排版
vbox.addStretch() #加入一個空的可伸展的透明框;有加或未加此指令有何差別,請同學觀察效果
vbox.addWidget(label2)
vbox.addStretch()
vbox.addWidget(label3)
self.setLayout(vbox) #透過.setLayout()把佈局管理員排版好的內容設入主視窗
label3.setOpenExternalLinks(True) #開放允許存取超連結,預設是不允許
label3.linkActivated.connect(link_clicked) #點擊label3產生事件將連結至link_clicked()作處理
label3.linkHovered.connect(link_hovered) #滑鼠飛掠過label3產生事件將連結至link_hovered()作處理
def link_hovered():
print("滑鼠滑過標籤當下所觸發事件")
def link_clicked():
print("滑鼠點擊標籤當下所觸發事件" )
if __name__ == "__main__":
app = QApplication([])
win = QLabelDemo()
win.show()
app.exec_()