多大数应用程序启动时都会在程序完全启动时显示一个启动画面,在程序完全启动后消失。程序启动画面可以显示一些有关产品的信息,让用户在等待程序启动的同时了解有关产品的功能,也是一个宣传的方式。QSplashScreen类提供了在程序启动过程中显示的启动画面的功能当运行程序时,在显示屏的中央出现一个启动画面,经过一段时间,应用程序完成初始化工作后,启动画面隐去,出现程序的主窗口界面
SplashScreen.py
1 # -*- coding:utf8 -*- 2 from PyQt4 import QtGui,QtCore 3 from PyQt4.QtGui import * 4 from PyQt4.QtCore import * 5 import sys,time 6 from clock import DigiClock 7 class SplashScreen(QSplashScreen): 8 def __init__(self): 9 super(SplashScreen, self).__init__(QPixmap("clock.png")) #启动程序的图片 10 11 #效果 fade =1 淡入 fade= 2 淡出, t sleep 时间 毫秒 12 def effect(self): 13 self.setWindowOpacity(0) 14 t = 0 15 while t <= 50: 16 newOpacity = self.windowOpacity() + 0.02 #设置淡入 17 if newOpacity > 1: 18 break 19 20 self.setWindowOpacity(newOpacity) 21 self.show() 22 t -= 1 23 time.sleep(0.04) 24 25 time.sleep(1) 26 t = 0 27 while t <= 50: 28 newOpacity = self.windowOpacity() - 0.02 #设置淡出 29 if newOpacity < 0: 30 break 31 32 self.setWindowOpacity(newOpacity) 33 t += 1 34 time.sleep(0.04) 35 36 37 if __name__ == "__main__": 38 app = QtGui.QApplication(sys.argv) 39 splash = SplashScreen() 40 splash.effect() 41 app.processEvents() ##设置启动画面不影响其他效果 42 window = DigiClock() #程序的主类 43 window.show() 44 splash.finish(window) #启动画面完成启动 45 sys.exit(app.exec_())
clock.py
1 # -*- coding: utf-8 -*- 2 #时钟 3 from PyQt4.QtGui import * 4 from PyQt4.QtCore import * 5 import sys 6 7 class DigiClock(QLCDNumber): 8 def __init__(self,parent=None): 9 super(DigiClock,self).__init__(parent) 10 11 p=self.palette() 12 p.setColor(QPalette.Window,Qt.red) 13 self.setPalette(p) 14 15 self.setNumDigits(19) 16 self.dragPosition=None 17 18 self.setWindowFlags(Qt.FramelessWindowHint) 19 self.setWindowOpacity(0.5) # 1表示100%显示, 0.5 透明度50% 20 21 timer=QTimer(self) 22 self.connect(timer,SIGNAL("timeout()"),self.showTime) 23 timer.start(1000) 24 25 self.showTime() 26 self.resize(500,60) 27 28 def mousePressEvent(self,event): 29 if event.button()==Qt.LeftButton: 30 self.dragPosition=event.globalPos()-self.frameGeometry().topLeft() 31 event.accept() 32 if event.button()==Qt.RightButton: 33 self.close() 34 35 def mouseMoveEvent(self,event): 36 if event.buttons() & Qt.LeftButton: 37 self.move(event.globalPos()-self.dragPosition) 38 event.accept() 39 40 def showTime(self): 41 time=QTime.currentTime() 42 date=QDate.currentDate() 43 text= date.toString("yyyy-MM-dd")+" "+time.toString("hh:mm:ss") 44 self.display(text) 45 46 if __name__ == ‘__main__‘: 47 app=QApplication(sys.argv) 48 form=DigiClock() 49 form.show() 50 app.exec_()
clock.png
效果:
时间: 2024-10-03 23:04:22