Search code examples
pythonpyqtpyqt4qimage

adding Image to tab


i have encountered another problem maybe you all could help with i cant seem to add a photo to any of my tabs could you help?

here is my code

import sys
import webbrowser
import random
import time
import os
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QWidget, QIcon, QLabel, QPixmap


class UICreator(QWidget):  # |||| CREATOR TAB |||
    def __init__(self, parent=None):
        super(UICreator, self).__init__(parent)

        self.Back = QPushButton("<- Back", self)
        self.Back.resize(50, 25)
        self.Back.move(0, 425)
        self.Creator = QPushButton("YouTube", self)
        self.Creator.resize(100, 40)
        self.Creator.move(25, 50)
        self.CreatorB2 = QPushButton("Twitter", self)
        self.CreatorB2.resize(100, 40)
        self.CreatorB2.move(275, 50)
        self.CreatorL = QLabel("Created By: PapaKliffy", self)
        self.CreatorL.move(20, 350)

MainWindow here

def startUICreatorTab(self):

    self.Creator = UICreator(self)
    self.setWindowTitle("Kliffy's SS Tool V2.0 | Creator Tab")
    self.setCentralWidget(self.Creator)
    self.Creator.Back.clicked.connect(self.startUIWindow)
    self.Creator.Creator.clicked.connect(self.Kliffy)
    self.Creator.CreatorB2.clicked.connect(self.Kliffy2)
    self.show()

Solution

  • You must be override paintEvent

    import sys
    
    from PyQt4.QtCore import QSize
    from PyQt4.QtGui import QApplication, QPainter, QPixmap, QPushButton, QWidget, QLabel
    
    
    class UICreator(QWidget):  # |||| CREATOR TAB |||
        def __init__(self, parent=None):
            super(UICreator, self).__init__(parent)
            self.resize(QSize(450, 450))
            self.Back = QPushButton("<- Back", self)
            self.Back.resize(50, 25)
            self.Back.move(0, 425)
            self.Creator = QPushButton("Youtube", self)
            self.Creator.resize(100, 40)
            self.Creator.move(50, 50)
            self.CreatorB2 = QPushButton("Twitter", self)
            self.CreatorB2.resize(100, 40)
            self.CreatorB2.move(275, 50)
            self.CreatorL = QLabel("Created By: PapaKliffy", self)
            self.CreatorL.move(20, 350)
    
        def paintEvent(self, event):
            painter = QPainter(self)
            painter.drawPixmap(self.rect(), QPixmap("background.jpg"))
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = UICreator()
        w.show()
        sys.exit(app.exec_())
    

    background.png:

    enter image description here

    Output:

    enter image description here