[PyQt] Delaying splash screen

Hans-Peter Jansen hpj at urpla.net
Fri Sep 9 12:10:34 BST 2011


Am Thursday 08 September 2011 00:32:17 schrieb Muhammad Bashir Al-Noimi:
> On 07/09/2011 11:27 م, Hans-Peter Jansen wrote:
> On Wednesday 07 September 2011, 14:04:02 admin at mbnoimi.net wrote:
>
> Hi guys,
>
>  I wrote a tiny class for delaying splash screen in C++ but when I
> tired to re-write it in python it didn't work!
>
>  I got "AttributeError TeSplashScreen object has no attribut QFrate"
> although TeSplashScreen inherited from QFrame
>
>  Could you please help me, I'm still a newbie in PyQt and Python.
>
>  tesplashscreen.py
>
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
>
> Hmm, that * imports are easily avoided.
>  Thanks a lot Pete, You mean I don't need to use both import lines?

Well, do it sanely with (the proposed standard of):

from PyQt4 import QtCore, QtGui

You will learn something about the structure of Qt, that is also helpful 
in understanding the basic concepts.

> import sys
> class TeSplashScreen(QFrame):
>     """
>     Splash doc
>     """
>
>     app = QApplication(sys.argv)
>     sPixmap = QPixmap(10,  10)
>     sMessage = QString()
>     messages = ["nothing"]
>     sAlignment = 0
>     sColor = QColor()
>
>     def __init__(self,  pixmap):
>
> You need to call the base class c'tor here, e.g.:
>
>           super(QFrame, self).__init__()
>
> Most of your code below has issues with missing self. references,
> which will result in access to undefined variables and instances,
> that are prematurely garbarge collected.
>  I did as you mentioned exactly.
>  I got new issue because I don't know how can I use QStringList
> class, I read in some article that it's unavailable in PyQt

Huch

Python 2.6.2 (r262:71600, May 25 2011, 11:48:28) 
[GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from PyQt4 import QtCore
>>> sl = QtCore.QStringList()
>>> map(sl.append, ("a", "b", "c", "d", "e"))
[None, None, None, None, None]
>>> print sl.join("..")
a..b..c..d..e

> so I 
> tried to use python lists instead but I got an exception at for loop
>
> TypeError object of type int has no len()

You seem to supply an int as second argument to showSplash.

>  here's the modified class:
>
> from PyQt4.QtCore import *
> from PyQt4.QtGui import *
> import sys
> class TeSplashScreen(QFrame):
>     """
>     Splash doc
>     """
>
>     app = QApplication(sys.argv)
>     sPixmap = QPixmap(10,  10)
>     sMessage = QString()
> #    messages = ["nothing"]
>     sAlignment = 0
>     sColor = QColor()
>
>     def __init__(self,  pixmap):
>         super(QFrame,  self).__init__()
>         self.sPixmap =  pixmap
>        
> self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint)
> self.setAttribute(Qt.WA_TranslucentBackground)
>         self.setFixedSize(self.sPixmap.size())
>
>     def clearMessage(self):
>         self.sMessage.clear()
>         self.repaint()
>
>     def showMessage(self,  theMessage, theAlignment, theColor):
>         self.sMessage  = theMessage
>         self.sAlignment = theAlignment
>         self.sColor  = theColor
>         self.repaint()
>
>     def paintEvent(self, pe):
>         aTextRect = QRect(self.rect())
>         aTextRect.setRect(aTextRect.x()+5, aTextRect.y()+5,
> aTextRect.width()-10, aTextRect.height()-10) aPainter =
> QPainter(self)
>         aPainter.drawPixmap(self.rect(), self.sPixmap)
>         aPainter.setPen(self.sColor)
>         aPainter.drawText(aTextRect, self.sAlignment, self.sMessage)
>
>     def showSplash(self,  delay, messages, alignment, color):
>         delay = 0
> #        self.messages = messages
>         alignment = 0
>         color = QColor(color)
>         class SleeperThread(QThread):
>             msecs = 0
>             QThread.msleep(msecs)
>         aSplashScreen = TeSplashScreen(self.sPixmap)
>         aSplashScreen.show
>         for i in range(len(messages)):
>             aSplashScreen.showMessage(messages[i], alignment, color)
>             SleeperThread.msleep(delay)

Don't expect this to work correctly. Does it? If you want to delay the 
messages, do it with QTimer triggered updates.

Pete


More information about the PyQt mailing list