RSS Feed

transparent windows howto

July 11, 2008 by Oğuz Yarımtepe

I have been dealing with the qt programming using Python. Till now i mainl created simple GUI working as a on screen display or making simple jobs. I was trying to creade an on screen display. I needed to create transparent window application. After searching and trying here is a simple python code that uses qt4 library that creates a window which includes a transparent png image inside.


import sys
from PyQt4 import QtGui, Qt, QtCore

class Transparent(QtGui.QWidget):

def __init__(self):
QtGui.QWidget.__init__(self)
self.setAttribute(Qt.Qt.WA_NoSystemBackground)
self.setAutoFillBackground(True)

pixmap = QtGui.QPixmap("test.png")
width = pixmap.width()
height = pixmap.height()

self.setWindowTitle("Status")
self.resize(width, height)

self.label = QtGui.QLabel(self)
self.label.setPixmap(QtGui.QPixmap("test.png"))

self.setMask(pixmap.mask())

def paintEvent(self,event):
self.setAttribute(Qt.Qt.WA_NoSystemBackground)

if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
x = Transparent()
x.show()
app.exec_()


7 Comments »

  1. Hi,

    Just stumbled across your site here whilst looking at creating a transparent window — I want to create a simple application that can sit just above the root window and display system stats. (If it works out, I’ll probably release the code for everyone too).

    There’s one slight problem with your above code — it’s not indented. With C, Perl or other languages, that wouldn’t matter too much… but with Python — it’s imperative that the code is properly indented, since Python relies on this for parsing code blocks.

    WordPress provides a tag for displaying code... could you perhaps re-post the above using that, or alternatively, attach a plaintext file one can download?

    Regards,
    Stuart Longland

  2. Oğuz Yarımtepe says:

    You are right. But it seems this template is not supporting the code tag. I edited the entry but still it is not intended.

    So here is the link that you can check the source intended: http://pastebin.com/f45bb76e0

  3. david says:

    i’ve been looking for something like this for a few days and have been getting wrapped up a whole win32api UpdateLayeredWindow trainwreck. i wish i had come across this simple solution earlier. thanks!

    also, to get the right indentation you could just look at the html source. (or you could use “pre” tags.)

  4. Joseph Le Brech says:

    for indention I google for a htmlencoder and paste my code in there, it encodes it so that the indents are hard coded, then wrap it in a PRE tag and CODE tag.

    Thanks for the code by the way I’m going to use it to make a callcentre script prompter 🙂

  5. Strahinja says:

    Nice one, that actually worked !!! Thanks !

  6. Bhuvana says:

    Could u help me for creating a transparent window with frame. something like glassy appearance…

  7. Oğuz Yarımtepe says:

    I haven’t been dealing with the qt programming for a while. Sorry.

Leave a Reply

Your email address will not be published. Required fields are marked *