# -*- coding: utf-8 -*-
# python:2.x
__author__ = ‘Administrator‘
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PyQt4.Qt import *
class WigglyWidget(QWidget):
def __init__(self,parnet=None):
super(WigglyWidget, self).__init__(parnet)
self.setBackgroundRole(QPalette.Midlight)#setBackgroundRole() 函数告诉QWidget使用调色盘的黑色作为背景颜色,而不是默认的窗口(window)颜色;midlight刷颜色
self.setAutoFillBackground(True)
"""
当组件被放大时,Qt就用这个设定的默认的颜色来填充任何 新区域,因为这个时候paintEvent()事件还没有机会来绘制这些新的区域。
我们还必须调用 setAutoFillBackground(true)函数来使能这个机制。(默认情况下,子组件会从父组件中继承背景色。)
"""
newFont=self.font()#应该创建文字
newFont=QFont()#和self.font()一样
newFont.setPointSize(newFont.pointSize()+20)#setPointSize()设置字符的高度显示在屏幕上指定数量的像素,pointSize()返回像素大小的字体
self.setFont(newFont)#setFont()使用应用程序的默认字体
self.timer=QBasicTimer()#QBasicTimer类为对象提供计时器事件,建议使用更高级的qtimer类
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())#QFontMetrics类提供了字体度量信息
x=(self.width()-metrics.width(self.text))/2#QFontMetrics.width()有2个方法
# width (self, QChar)
# width (self, QString, int length = -1)
y=(self.height()+metrics.ascent()-metrics.descent())/2#ascent()返回的字体的高度,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, int, int alpha = 255)
"""
原文: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.
翻译:设置一个HSV颜色值;H为色调,饱和度的是,V和价值是HSV颜色的alpha分量。
饱和,价值和alpha通道值必须在0-255范围,和色调值必须大于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(WigglyWidget,self).timerEvent(e)
class Dia(QDialog):
def __init__(self,parent=None):
super(Dia,self).__init__(parent)
self.with1=WigglyWidget()
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(‘With1‘)
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.exec_())
如图: