paintEvent函数未将jpg图像显示为背景

时间:2019-03-12 01:58:04

标签: python-2.7 pyqt5

我的问题是,如何在paintEvent()中放置使用jpg图像的背景? 我尝试使用QLabel,但不起作用。我也尝试在paintEvent内设置标签,但似乎不正确,它没有显示任何错误,并且我尝试删除了paintEvent,图像的文件名也正确,但是图像未出现。

我也尝试遵循此方法:Drawing points on QPixmap on QWidget (pyqt5)

                import sys
            import turtle
            from PyQt5 import QtCore, QtWidgets
            from PyQt5.QtWidgets import QMainWindow,  QWidget, QApplication, QFrame, QPushButton, QLabel
            from PyQt5.QtCore import QSize , Qt, QObject, QRect, QPropertyAnimation, QByteArray, QSettings, QTimer, pyqtSlot
            from PyQt5.QtWidgets import QAction 
            from PyQt5.QtGui import QPen, QColor, QMovie, QIcon, QPixmap
            from itertools import cycle
            from PyQt5 import QtGui

            class Main(QMainWindow,QtWidgets.QWidget):
                def __init__(self):
                    QMainWindow.__init__(self)
                    self.setMinimumSize(QSize(1000, 760))    
                    self.setWindowTitle("GUI OOP STOPLIGHT")   


                    self._image = QtGui.QPixmap("bg.jpg")

                    #self.addshape("skateboard1.gif")        
                    self.button=QPushButton("Press to cross \nimmediately",self)
                    self.button.move(873,185)
                    self.button.clicked.connect(self.col_switch)

                    self.RRR= cycle([
                        QtGui.QColor(255,0,0),
                        QtGui.QColor(145,20,30),
                        QtGui.QColor(145,20,30),
                        QtGui.QColor(145,20,30),])  

                    self.YYY = cycle([
                        QtGui.QColor(90,90,10),
                        QtGui.QColor(255,255,0),
                        QtGui.QColor(90,90,10),
                        QtGui.QColor(255,255,0)])

                    self.GGG= cycle([
                        QtGui.QColor(0,90,30),
                        QtGui.QColor(0,90,30),
                        QtGui.QColor(51,255,51),
                        QtGui.QColor(0,90,30),])

                    self.Cros=cycle([
                        QtGui.QColor(51,255,51),
                        QtGui.QColor(51,255,51),
                        QtGui.QColor(255,0,0),
                        QtGui.QColor(255,0,0),])

                    self.lt_st1=next(self.RRR)
                    self.lt_st2=next(self.YYY)
                    self.lt_st3=next(self.GGG)
                    self.cross=next(self.Cros)
                    timer = QtCore.QTimer(self, timeout=self.col_switch)
                    tym = 0
                    if tym == 0 :
                        timer.start(3000)
                        tym = 1
                    elif tym == 1 :
                        timer.start(1000)
                        tym = 2
                    elif tym == 2:
                        timer.start(3000)
                        tym = 3
                    elif tym == 3:
                        timer.start(3000)
                        tym=0

                def col_switch(self): 
                    self.lt_st1=next(self.RRR)
                    self.lt_st2=next(self.YYY)
                    self.lt_st3=next(self.GGG)
                    self.cross=next(self.Cros)
                    self.update()         

                def paintEvent(self,event):      
                    Box=QtGui.QPainter(self)
                    Box.setBrush(QColor("Gray"))
                    Box.setPen(QPen(Qt.black,3, Qt.SolidLine))
                    Box.drawRect(740,15,120,335)

                    Rd=QtGui.QPainter(self)
                    Rd.setBrush(self.lt_st1)
                    Rd.setPen(QPen(Qt.black,3, Qt.SolidLine))
                    Rd.drawEllipse(750,25,100,100)

                    Yl=QtGui.QPainter(self)
                    Yl.setBrush(self.lt_st2)
                    Yl.setPen(QPen(Qt.black,3, Qt.SolidLine))
                    Yl.drawEllipse(750,130,100,100)

                    Gr=QtGui.QPainter(self)
                    Gr.setBrush(self.lt_st3)
                    Gr.setPen(QPen(Qt.black,3, Qt.SolidLine))
                    Gr.drawEllipse(750,235,100,100)

                    Box2=QtGui.QPainter(self)
                    Box2.setBrush(QColor("Gray"))
                    Box2.setPen(QPen(Qt.black,3, Qt.SolidLine))
                    Box2.drawRect(861,215,120,135)

                    CrG=QtGui.QPainter(self)
                    CrG.setBrush(self.cross)
                    CrG.setPen(QPen(Qt.black,3, Qt.SolidLine))
                    CrG.drawRect(878,240,90,90)

                    painter = QtGui.QPainter(self)
                    painter.drawPixmap(self.rect(), self._image)

            if __name__ == "__main__":
                app = QtWidgets.QApplication(sys.argv)
                prog = Main()
                prog.show() 
            sys.exit( app.exec_() )

0 个答案:

没有答案