将图像添加到选项卡

时间:2016-12-24 00:27:43

标签: python pyqt pyqt4 qimage

我遇到了另一个问题,也许你们都可以提供帮助我似乎无法将照片添加到我的任何标签中你能帮忙吗?

这是我的代码

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

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()

1 个答案:

答案 0 :(得分:1)

您必须覆盖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

<强>输出:

enter image description here