微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

具有透明背景的 PyQt5 小部件中的 Pygame 表面是否可能?

如何解决具有透明背景的 PyQt5 小部件中的 Pygame 表面是否可能?

PyQt5 小部件中的 Pygame Surface,具有透明背景。 使用嵌入在 PyQt5 窗口中的 PIL pieslices 图像创建的 Pygame Surface,它工作正常,但具有黑色背景。我已经试过了:

  1. surface.fill((255,255,0)) 并且这不适用于任何值。我是一些例子,它确实用某种颜色填充背景,但更改值无论如何都无济于事

  2. surface = surface.convert_alpha() 它只是给出错误 pygame.error: No video mode has been set。据我所知,要修复它,我需要pygame.display.set_mode,但它会在开始时显示 pygame 窗口

  3. 使用 pygame.SRCALPHA 创建表面,例如 surface = pygame.Surface((640,480),pygame.SRCALPHA),然后什么也没有发生

  4. 认 pyqt .setStyleSheet('background-color: transparent')

  5. 还有掩码,我对此一无所知,但代码如下 a = ImageWidget(s2) > a.setMask(QRegion(a.rect(),QRegion.Ellipse)) 也不起作用

代码示例:

import sys,pygame,random
from PIL import Image,ImageDraw,ImageFont
from PyQt5 import QtCore,QtWidgets,QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *


class ImageWidget(QWidget):
    def __init__(self,surface,parent=None):
        super(ImageWidget,self).__init__(parent)
        self.w=surface.get_width()
        self.h=surface.get_height()
        self.data=surface.get_buffer().raw
        self.image = QImage(self.data,self.w,self.h,QImage.Format_RGB32)

    def paintEvent(self,event):
        qp=QtGui.QPainter()
        qp.begin(self)
        qp.drawImage(0,self.image)
        qp.end()

    def update(self,surface):
        self.data = surface.get_buffer().raw
        self.image = QImage(self.data,QImage.Format_RGB32)


class Example(QtWidgets.QMainWindow):

    def __init__(self):
        super().__init__()

        self.initUI()


    def blitRotateCenter(self,surf,image,topleft,angle):

        rotated_image = pygame.transform.rotate(image,angle)
        new_rect = rotated_image.get_rect(center = image.get_rect(topleft = topleft).center)

        surf.blit(rotated_image,new_rect.topleft)

    def blitting(self):
        global changedtime,rolltime
        global anglee,rot
        if rot!=40 and rot>0:
            rolltime = round(rolltime*0.857,4)
            if rolltime<=0.2:
                rolltime==0.2
                print(rolltime)
            rot = round(rot - rolltime,4)
        elif rot<=0:
            self.timer.stop()
        # print(changedtime,rolltime)
        anglee = anglee + rot
        self.blitRotateCenter(self.s2,self.s1,(0,0),anglee)
        self.img.update(self.s2)
        self.btn.setCellWidget(0,ImageWidget(self.s2))
        # print(self.s2.get_at((250,20)))



    def initUI(self):

        BLACK = (  0,0)
        WHITE = (255,255)
        BLUE  = (  0,255)
        GREEN = (  0,0)
        RED   = (255,0)
        GREY  = (128,128,128)

        listo = [(0,30),(31,140),(141,280),(281,359)]
        timevalue = 0


        global anglee,rot
        global rolltime
        anglee = 0
        rot = 7.6
        changedtime = 1
        rolltime = 1

        pygame.init()
        self.s=pygame.Surface((1600,900),pygame.SRCALPHA)
        self.s1 = pygame.Surface((500,500),pygame.SRCALPHA)
        self.s2 = pygame.Surface((500,pygame.SRCALPHA)
        self.img = ImageWidget(self.s2)
        pil_size = 500


        for item in listo:

            pil_image = Image.new("RGBA",(pil_size,pil_size))
            pil_draw = ImageDraw.Draw(pil_image)
            pil_draw.pieslice((0,pil_size,pil_size),item[0],item[1],fill=(random.randint(0,255),random.randint(0,255)))
            # pil_draw.pieslice((0,fill=BLUE)


            mode = pil_image.mode
            size = pil_image.size
            data = pil_image.tobytes()
            image = pygame.image.fromstring(data,size,mode)

            self.s1.blit(image,0))


        self.blitRotateCenter(self.s2,0)



        self.btn = QTableWidget(self)
        self.btn.setRowCount(1)
        self.btn.setColumnCount(1)
        self.btn.setRowHeight(0,800)
        self.btn.setColumnWidth(0,800)
        self.btn.setCellWidget(0,ImageWidget(self.s2))
        self.btn.resize(1500,800)
        self.btn.setStyleSheet("QTableWidget {background-color: transparent; border: 0px; selection-background-color: transparent; color: white;}"
              "QTableWidget::item {background-color: transparent; color: rgba(255,0.8); border: 0px solid transparent;}"
              # "QTableWidget::item:hover {background-color: white; color: black;}"
              "QTableWidget::item:pressed {background-color: white; color: red;}"
              "QHeaderView::section {background-color: transparent; width:0px;}"
              "QHeaderView {background-color: transparent;}"
              "QTableCornerButton::section {background-color: transparent;}"
              "QScrollBar {width:0px}"
              "QTableWidget::item{ selection-background-color: red}")
        self.btn.setShowGrid(False)
        self.btn.setFocusPolicy(Qt.NoFocus)
        self.btn.move(20,20)

        self.setGeometry(0,1600,900)
        self.setwindowTitle('Tooltips')


        self.timer = QTimer(self)
        self.timer.timeout.connect(self.blitting)
        self.timer.start(1)

        self.show()




if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

有什么想法吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。