Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
9
Help us understand the problem. What is going on with this article?
@suto3

Pyside の透過PNG表示のサンプル

More than 5 years have passed since last update.

Pyside のウィンドウに透過PNGの画像ファイルを貼り付けて、非矩形のウィンドウを作成するサンプルです。

ImageViewer.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
from PySide import QtCore, QtGui


class ImageViewer(QtGui.QWidget):
    def __init__(self):
        super(ImageViewer, self).__init__()
        self.initUI()

    def initUI(self):
        '''
        ユーザーインターフェースの初期化
        '''
        # 枠なしWindow
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        #半透明
        self.setWindowOpacity(0.8)

        self.setWindowTitle('ImageViewer')

        self.imageLabel = QtGui.QLabel()
        self.imageLabel.setBackgroundRole(QtGui.QPalette.Base)
        self.imageLabel.setSizePolicy(
            QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed)

        self.imageLabel.setScaledContents(False)

        self.filenameLabel = QtGui.QLabel()

        self.imageLabel.setToolTip("コンテキストメニューで操作してください。")

        # Debug用
        #self.button = QtGui.QPushButton('open')
        #self.button.clicked.connect(self.open)

        #self.quit = QtGui.QPushButton('quit')
        #self.quit.clicked.connect(self.close)

        layout = QtGui.QVBoxLayout()
        layout.setContentsMargins(0, 0, 0, 0)
        layout.addWidget(self.imageLabel)

        # Debug用
        #layout.addWidget(self.filenameLabel)
        #layout.addWidget(self.button)
        #layout.addWidget(self.quit)

        self.setLayout(layout)
        self.setGeometry(50, 50, 250, 250)

    def contextMenuEvent(self, event):
        '''
        コンテキストメニュー
        '''
        menu = QtGui.QMenu(self)
        menu.addAction('Open', self.open)
        menu.addAction('Quit', self.close)
        menu.exec_(event.globalPos())

    def open(self):
        (path, ftype) = QtGui.QFileDialog.getOpenFileName(self, "Open File")
        image = QtGui.QImage(path)
        pixmap = QtGui.QPixmap.fromImage(image)

        self.filenameLabel.setText(os.path.basename(path))

        self.imageLabel.setPixmap(pixmap)
        self.resize(image.size())
        mask = QtGui.QPixmap.fromImage(image.createAlphaMask())
        self.setMask(mask)


def main():
    app = QtGui.QApplication(sys.argv)

    iv = ImageViewer()
    iv.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    import sys
    main()


# EOF


Windows7 での実行例

元のPNGファイルの透過部分が完全に透明処理されている

pyside-imageview-screenshot.PNG

9
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
suto3

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
9
Help us understand the problem. What is going on with this article?