Pyqt text animation (learned from official examples)

Source: Internet
Author: User

#-*-Coding: UTF-8 -*-

# Python: 2.x

_ Author _ = 'admin'

From pyqt4.qtgui import *

From pyqt4.qtcore import *

From pyqt4.qt import *

 

Class wiggglywidget (qwidget ):

Def _ init _ (self, parnet = none ):

Super (wiggglywidget, self). _ init _ (parnet)

Self. setbackgroundrole (qpalette. midlight) # setbackgroundrole () function tells qwidget to use the black color of the color palette as the background color instead of the default window color.

Self. setautofillbackground (true)

"""

When the component is enlarged, QT uses the default color of this setting to fill in any new areas, because the paintevent () event has no chance to draw these new areas.

We must also call the setautofillbackground (true) function to enable this mechanism. (By default, child components inherit the background color from the parent component .)

"""

Newfont = self. Font () # create text

Newfont = qfont () # Same as self. Font ()

Newfont. setpointsize (newfont. pointsize () + 20) # setpointsize () sets the height of characters to be displayed in a specified number of pixels on the screen. pointsize () returns the font of the pixel size.

Self. setfont (newfont) # setfont () use the default font of the application

 

Self. Timer = qbasictimer () # The qbasictimer class provides timer events for the object. We recommend that you use a more advanced qtimer class.

Self. Text =''

 

Self. Step = 0

Self. Timer. Start (60, self)

Def paintevent (self, event ):

Sinetable = (0, 38, 71, 92,100, 92, 71, 38, 0,-38,-71,-92,-100,-92,-71,-38)

Metrics = qfontmetrics (self. Font () # The qfontmetrics class provides the font metrics.

X = (self. Width ()-metrics. Width (self. Text)/2 # qfontmetrics. Width () has two methods

# Width (self, qchar)

# Width (self, qstring, int length =-1)

Y = (self. Height () + metrics. ascent ()-metrics. descent ()/2 # height of the font returned by ascent (), minimum of the font returned by descent ()

Color = qcolor ()

Painter = qpainter (Self)

For I, CH in enumerate (self. text ):

Index = (self. Step + I) % 16

Color. sethsv (15-index) * 16,255,191) # qcolor. sethsv (self, Int, int alpha = 255)

"""

Original article: sets a HSV color value; H is the hue, S is the saturation, V is the value and a is the Alpha component of the HSV color.

The saturation, value and alpha-channel values must be in the range 0-255, and the hue value must be greater than-1.

Set a HSV color value, h for the color, saturation is, V and value is the Alpha component of the HSV color.

Saturation, value, and alpha channel value must be in the range of 0 to 25 5, and the color value must be greater than 1.

"""

Painter. setpen (color)

Painter. drawtext (X, Y-(sinetable [Index] * metrics. Height ()/400), ch)

X + = metrics. Width (CH)

 

Def timerevent (self, e ):

If E. timerid () = self. Timer. timerid ():

Self. Step + = 1

Self. Update ()

Else:

Super (wiggglywidget, self). timerevent (E)

 

Class DIA (qdialog ):

Def _ init _ (self, parent = none ):

Super (Dia, self). _ init _ (parent)

 

Self. With1 = wiggglywidget ()

Lineedit = qlineedit ()

Layout = qvboxlayout ()

Layout. addwidget (self. With1)

Layout. addwidget (lineedit)

Self. setlayout (layout)

Print Dir (self. With1)

Lineedit. textchanged. Connect ()

Lineedit. settext ('Hello world! ')

Self. setwindowtitle ('withh1 ')

Self. Resize (360,145)

Def text1 (self, newtext ):

Self. Text = newtext

If _ name _ = '_ main __':

 

Import sys

 

APP = qapplication (SYS. argv)

Dialog = DIA ()

Dialog. Show ()

Sys.exit(app.exe C _())

:

Pyqt text animation (learned from official examples)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.