国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Pyqt設置背景顏色和背景圖片、QPalette調色板與QPainter畫板區別、不規則圖片

2019-11-14 17:24:25
字體:
來源:轉載
供稿:網友

設置 背景顏色和背景圖片

首先設置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 = 110         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     # 顯示不規則 pic17     def mypix(self):18         self.update()19         if self.i == 5:20             self.i = 121         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=None26 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+=147             self.mypix()48 49     #每**秒修改paint50     def timeChange(self):51         self.i+=152         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_())

效果:

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 英山县| 彰化县| 哈尔滨市| 晋中市| 麻城市| 开化县| 双辽市| 仙居县| 仁寿县| 庆阳市| 湘潭市| 丰都县| 泸定县| 新化县| 化德县| 缙云县| 禄劝| 扬中市| 大同市| 太保市| 峨眉山市| 尖扎县| 东至县| 丰都县| 桃园市| 兴山县| 军事| 普兰店市| 武隆县| 新津县| 平果县| 东乡族自治县| 安庆市| 昆明市| 韩城市| 轮台县| 雅江县| 淮滨县| 乌兰县| 延川县| 峨眉山市|