設置 背景顏色和背景圖片
首先設置autoFillBackground屬性為真
然后定義一個QPalette對象
設置QPalette對象的背景屬性(顏色或圖片)
最后設置QWidget對象的Palette
實例:
1 # -*- coding: utf-8 -*- 2 import sys 3 from PyQt4 import QtGui 4 from PyQt4.QtGui import * 5 from PyQt4.QtCore import * 6 7 8 class Icon(QtGui.QWidget): 9 def __init__(self, parent=None): 10 QtGui.QWidget.__init__(self, parent) 11 palette1 = QtGui.QPalette(self) 12 palette1.setColor(self.backgroundRole(), QColor(192,253,123)) # 設置背景顏色 13 # palette1.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap('../../../Document/images/17_big.jpg'))) # 設置背景圖片 14 self.setPalette(palette1) 15 self.setAutoFillBackground(True) # 不設置也可以 16 self.setGeometry(300, 300, 250, 150) 17 self.setWindowTitle('Icon') 18 self.setWindowIcon(QtGui.QIcon('../../../Document/images/firefox.png')) 19 mylayout = QVBoxLayout() 20 self.setLayout(mylayout) 21 22 23 24 25 app = QtGui.QApplication(sys.argv) 26 icon = Icon() 27 icon.show() 28 sys.exit(app.exec_())
效果:
---------------------------------------------------------------------------------------
QPalette 調色板 與QPainter 畫板區別
1 # -*- coding: UTF8 -*- 2 from PyQt4 import QtGui 3 from PyQt4.QtGui import * 4 import sys 5 6 ''' 7 調色板: palette 鋪平整個背景 (小於窗體有多個圖片) 8 png 如果有圖層,背景為黑色,可圖層覆蓋 9 ''' 10 class Icon(QtGui.QWidget): 11 def __init__(self, parent=None): 12 13 QtGui.QWidget.__init__(self, parent) 14 self.resize(256, 256) 15 self.setWindowTitle('Icon') 16 mylayout = QVBoxLayout() 17 self.setLayout(mylayout) 18 19 def paintEvent(self, event): 20 palette1 = QtGui.QPalette(self) 21 palette1.setColor(self.backgroundRole(), QColor(192, 253, 123)) # 設置背景顏色 22 palette1.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap('1.png'))) # 設置背景圖片 23 self.setPalette(palette1) 24 25 26 app = QtGui.QApplication(sys.argv) 27 icon = Icon() 28 icon.show() 29 sys.exit(app.exec_())
效果:
1 # -*- coding: UTF8 -*- 2 from PyQt4.QtGui import * 3 from PyQt4.QtCore import * 4 5 ''' 6 繪制QPainter 窗體 顯示一個 7 QPainter默認只能在paintEvent里面調用 否則:QPainter::begin: Paint device returned engine == 0, type: 1 8 ''' 9 class MyForm(QWidget): 10 def __init__(self,parent=None): 11 super(MyForm, self).__init__(parent) 12 self.resize(256, 256) 13 14 def paintEvent(self, event): 15 painter = QPainter() 16 painter.begin(self) 17 painter.setRenderHint(QPainter.Antialiasing) 18 painter.setPen(Qt.NoPen) 19 painter.drawPixmap(0, 0, 256, 256, QPixmap("1.png")) 20 painter.end() 21 22 23 app = QApplication([]) 24 form = MyForm() 25 form.show() 26 app.exec_()
效果:
--------------------------------------------------------------------------------------------------------------------------
不規則窗體的顯示
pyqt 的顯示不規則圖片主要注意兩點
1. 圖片的遮蓋物mask
pixmap.setMask() etMask()的作用是為調用它的控件增加一個遮罩,遮住所選區域以外的部分使之看起來是透明的, 它的參數可為一個QBitmap對象或一個QRegion對象,此處調用QPixmap的mask()函數獲得圖片自身的遮罩,為一個QBitmap對象
2. 如何激活paintEvent
paintEvent 每次初始化窗體只調用一次,導致chang不規則圖片的時候遮蓋物不修改,所以要每修改次圖片就激活一次paintEvent事件
激活的方法是更新窗體或重新繪制
通過Qtimer 每隔*/秒 self.connect(self.timer,SIGNAL("timeout()"),self.timeChang) 觸發update
1 self.update() 2 self.repaint()
代碼如下:
1 # -*- coding: UTF8 -*- 2 from PyQt4.QtGui import * 3 from PyQt4.QtCore import * 4 import sys 5 6 class ShapeWidget(QWidget): 7 def __init__(self,parent=None): 8 super(ShapeWidget,self).__init__(parent) 9 self.i = 1 10 self.mypix() 11 self.timer = QTimer() 12 self.timer.setInterval(500) # 500秒 13 self.timer.timeout.connect(self.timeChange) 14 self.timer.start() 15 16 # 顯示不規則 pic 17 def mypix(self): 18 self.update() 19 if self.i == 5: 20 self.i = 1 21 self.mypic = {1: 'Left_Button.png', 2: "Up_Button.png", 3: 'Right_Button.png', 4: 'Down_Button.png'} 22 self.pix = QPixmap(self.mypic[self.i], "0", Qt.AvoidDither | Qt.ThresholdDither | Qt.ThresholdAlphaDither) # 23 self.resize(self.pix.size()) 24 self.setMask(self.pix.mask()) #setMask()的作用是為調用它的控件增加一個遮罩,遮住所選區域以外的部分使之看起來是透明的, 它的參數可為一個QBitmap對象或一個QRegion對象,此處調用QPixmap的mask()函數獲得圖片自身的遮罩,為一個QBitmap對象。實例中使用的是png格式的圖片,它的透明部分實際上即是一個遮罩。 25 self.dragPosition=None 26 27 #重定義鼠標按下響應函數mousePressEvent(QMouseEvent)和鼠標移動響應函數mouseMoveEvent(QMouseEvent),使不規則窗體能響應鼠標事件,隨意拖動。 28 def mousePressEvent(self, event): 29 if event.button() == Qt.LeftButton: 30 self.dragPosition = event.globalPos()-self.frameGeometry().topLeft() #保存當前鼠標點所在的位置相對於窗體左上角的偏移值dragPosition, 31 event.accept() 32 if event.button() == Qt.RightButton: 33 self.close() 34 35 def mouseMoveEvent(self, event): 36 if event.buttons() & Qt.LeftButton: 37 self.move(event.globalPos()-self.dragPosition) # 當左鍵移動窗體修改偏移值 38 event.accept() 39 # 如何激活 paintEvent 呢? 一般 paintEvent 在窗體首次繪制加載, 要重新加載paintEvent 需要重新加載窗口使用 self.update() or self.repaint() 40 def paintEvent(self, event): 41 painter = QPainter(self) 42 painter.drawPixmap(0, 0, self.pix.width(),self.pix.height(),self.pix) 43 # 鼠標雙擊事件 44 def mouseDoubleClickEvent(self, event): 45 if event.button() == 1: 46 self.i+=1 47 self.mypix() 48 49 #每**秒修改paint 50 def timeChange(self): 51 self.i+=1 52 self.mypix() 53 54 if __name__ == '__main__': 55 app=QApplication(sys.argv) 56 form=ShapeWidget() 57 form.show() 58 app.exec_()
不規則資源圖片:
效果:
---------------------------------------------------------------------------------------------
加載Gif動畫效果
1 # -*- coding: utf-8 -*- 2 import sys 3 from PyQt4 import QtCore,QtGui 4 class loadingGif(QtGui.QWidget): 5 def __init__(self,parent=None): 6 super(loadingGif, self).__init__(parent) 7 self.label = QtGui.QLabel('', self) 8 self.setFixedSize(200,200) 9 self.setWindowFlags(QtCore.Qt.Dialog|QtCore.Qt.CustomizeWindowHint) 10 self.movie = QtGui.QMovie("loading.gif") 11 self.label.setMovie(self.movie) 12 self.movie.start() 13 14 if __name__ == '__main__': 15 app = QtGui.QApplication(sys.argv) 16 mw = loadingGif() 17 mw.show() 18 sys.exit(app.exec_())
效果: