将图像添加到选项卡

2024-10-05 14:25:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我遇到了另一个问题,也许你们都能帮我,我似乎不能添加照片到我的任何标签,你能帮忙吗?

这是我的密码

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)

这里是主窗口

^{pr2}$

Tags: importselfmoveinitback照片parentcreator
1条回答
网友
1楼 · 发布于 2024-10-05 14:25:22

必须重写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_())

背景.png:

enter image description here

输出:

enter image description here

相关问题 更多 >