Python PyQt5學(xué)習(xí)之樣式設(shè)置詳解
更新時間:2022年12月02日 10:01:20 作者:SongYuLong的博客
這篇文章主要為大家詳細(xì)介紹了Python PyQt5中樣式設(shè)置的相關(guān)資料,例如為標(biāo)簽添加背景圖片、為按鈕添加背景圖片、設(shè)置窗口透明等,感興趣的可以學(xué)習(xí)一下
為標(biāo)簽添加背景圖片
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
if __name__ == "__main__":
app = QApplication(sys.argv)
win = QMainWindow()
label = QLabel()
label.setToolTip("這是一個文本標(biāo)簽")
label.setStyleSheet("QLabel{border-image:url(./pyqt5/images/python.jpg)};")
label.setFixedWidth(320)
label.setFixedHeight(200)
win.setCentralWidget(label)
win.show()
sys.exit(app.exec_())

為按鈕添加背景圖片
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
if __name__ == "__main__":
app = QApplication(sys.argv)
win = QWidget()
layout = QVBoxLayout()
btn1 = QPushButton("按鈕1")
btn1.setMaximumSize(48, 48)
btn1.setMinimumSize(48, 48)
style = '''
QPushButton {
border-radius: 30px;
background-image: url('./pyqt5/images/left.png');
}
'''
btn1.setStyleSheet(style)
layout.addWidget(btn1)
win.setLayout(layout)
win.show()
sys.exit(app.exec_())

import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
if __name__ == "__main__":
app = QApplication(sys.argv)
win = QWidget()
btn1 = QPushButton("按鈕1")
btn1.setFixedSize(50, 50)
btn1.setObjectName("btn1")
style = '''
#btn1{
border-radius: 30px;
background-image: url('./pyqt5/images/left.png')
}
#btn1:hover{
border-radius: 30px;
background-image: url('./pyqt5/images/leftHover.png')
}
#btn1:Pressed{
border-radius: 30px;
background-image: url('./pyqt5/images/leftPressed.png')
}
'''
btn1.setStyleSheet(style)
layout = QVBoxLayout()
layout.addWidget(btn1)
win.setLayout(layout)
win.show()
sys.exit(app.exec_())

縮放圖片
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
if __name__ == "__main__":
app = QApplication(sys.argv)
win = QWidget()
pic = r"./pyqt5/images/Cloudy_72px.png"
img = QImage(pic)
label1 = QLabel()
label1.setFixedWidth(120)
label1.setFixedHeight(120)
result = img.scaled(label1.width(), label1.height(), Qt.IgnoreAspectRatio, Qt.SmoothTransformation)
label1.setPixmap(QPixmap.fromImage(result))
label2 = QLabel()
label2.setPixmap(QPixmap.fromImage(img))
layout = QVBoxLayout()
layout.addWidget(label2)
layout.addWidget(label1)
win.setLayout(layout)
win.show()
sys.exit(app.exec_())

設(shè)置窗口透明
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
if __name__ == "__main__":
app = QApplication(sys.argv)
win = QMainWindow()
win.setWindowTitle("設(shè)置窗口透明")
win.setWindowOpacity(0.5) # 設(shè)置窗口透明 透明度取值范圍:0.0(全透明)~1.0(不透明),默認(rèn)值1.0
win.show()
sys.exit(app.exec_())

到此這篇關(guān)于Python PyQt5學(xué)習(xí)之樣式設(shè)置詳解的文章就介紹到這了,更多相關(guān)PyQt5設(shè)置樣式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決python 兩個時間戳相減出現(xiàn)結(jié)果錯誤的問題
這篇文章主要介紹了解決python 兩個時間戳相減出現(xiàn)結(jié)果錯誤的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Python+django實(shí)現(xiàn)簡單的文件上傳
這篇文章主要為大家詳細(xì)介紹了Python+django實(shí)現(xiàn)簡單的文件上傳的相關(guān)代碼,感興趣的小伙伴們可以參考一下2016-08-08
Python實(shí)現(xiàn)簡單的HttpServer服務(wù)器示例
本篇文章主要介紹了Python實(shí)現(xiàn)簡單的HttpServer服務(wù)器示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
jupyter notebook插入本地圖片的實(shí)現(xiàn)
這篇文章主要介紹了jupyter notebook插入本地圖片的實(shí)現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
Python實(shí)現(xiàn)一個優(yōu)先級隊(duì)列的方法
這篇文章主要介紹了Python實(shí)現(xiàn)一個優(yōu)先級隊(duì)列的方法,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07

