[Python] 用PyQt实现透明桌面时钟小部件! →→→→→进入此内容的聊天室

来自 , 2021-04-04, 写在 Python, 查看 120 次.
URL http://www.code666.cn/view/c2ddc87b
  1. #coding=utf-8
  2. '''
  3. Created on 2012-4-6
  4.  
  5. @author: 大孟
  6. '''
  7.  
  8. import sys
  9. from PyQt4 import  QtGui, QtCore  
  10. from PyQt4.QtCore import Qt
  11. from PyQt4.QtCore import QPoint
  12. from PyQt4.QtCore import QTimer
  13. from PyQt4.QtCore import QTime
  14. from PyQt4.QtGui import QPainter
  15. from PyQt4.QtGui import QColor
  16. from PyQt4.QtGui import QPolygon
  17. from PyQt4.QtCore import SIGNAL as signal
  18.  
  19.  
  20. class Clock(QtGui.QWidget):
  21.     '''
  22.    classdocs
  23.    '''
  24.  
  25.  
  26.     def __init__(self):
  27.         '''
  28.        Constructor
  29.        '''
  30.        
  31.         super(Clock, self).__init__()  
  32.        
  33.         self.hourColor=QColor(127, 0, 127);
  34.         self.minuteColor=QColor(0, 127, 127, 191)
  35.         self.secondColor=QColor(127, 127,0,120)
  36.         self.initUI()
  37.         self.timer = QTimer()
  38.         self.timer.timeout.connect(self.update)
  39.         self.timer.start(30)
  40.         self.show()
  41.        
  42.     def handChange(self):    
  43.        
  44.         self.side = min(self.width(), self.height())
  45.         self.hand=(max(self.side/200,4), max(self.side/100,8), max(self.side/40,30))
  46.         self.hourHand=QPolygon([QPoint(self.hand[0],self.hand[1]),QPoint(-self.hand[0],self.hand[1]),QPoint(0,-self.hand[2])])
  47.         self.minuteHand=QPolygon([QPoint(self.hand[0],self.hand[1]),QPoint(-self.hand[0],self.hand[1]),QPoint(0,-self.hand[2]*2)])
  48.         self.secondHand=QPolygon([QPoint(self.hand[0],self.hand[1]),QPoint(-self.hand[0],self.hand[1]),QPoint(0,-self.hand[2]*3)])
  49.    
  50.     def set_transparency(self, enabled):
  51.         if enabled:
  52.             self.setAutoFillBackground(False)
  53.         else:
  54.             self.setAttribute(Qt.WA_NoSystemBackground, False)
  55.         #下面这种方式好像不行
  56. #        pal=QtGui.QPalette()
  57. #        pal.setColor(QtGui.QPalette.Background, QColor(127, 127,10,120))
  58. #        self.setPalette(pal)
  59.         self.setAttribute(Qt.WA_TranslucentBackground, enabled)
  60.         self.repaint()
  61.    
  62.     def initUI(self):      
  63.  
  64.         self.setGeometry(300, 300, 300, 200)
  65.         self.setWindowTitle('Clock')
  66.         self.handChange()
  67.         self.rightButton=False
  68.         # 下面两个配合实现窗体透明和置顶
  69.         sizeGrip=QtGui.QSizeGrip(self)
  70.         self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.SubWindow )
  71.         #self.setMouseTracking(True);
  72.         self.trans=True
  73.  
  74.         self.set_transparency(True)
  75.  
  76.         quitAction = QtGui.QAction(QtGui.QIcon('quit.png'), '&Quit', self)
  77.         self.connect(quitAction,signal("triggered()"),QtGui.qApp.quit)
  78.         backAction = QtGui.QAction( '&Back', self)
  79.         self.connect(backAction,signal("triggered()"),self.backClicked)
  80.         self.popMenu= QtGui.QMenu()
  81.         self.popMenu.addAction(quitAction)
  82.         self.popMenu.addAction(backAction)
  83.  
  84.        
  85.     def resizeEvent(self, e):  
  86.         self.handChange()
  87.        
  88.     def backClicked(self):
  89.         if self.trans == True :
  90.             self.trans = False  
  91.             self.set_transparency(False)
  92.         else:
  93.             self.trans = True
  94.             self.set_transparency(True)
  95.        
  96.     def mouseReleaseEvent(self,e):
  97.         if self.rightButton == True:
  98.             self.rightButton=False
  99.             self.popMenu.popup(e.globalPos())
  100.  
  101.     def mouseMoveEvent(self, e):
  102.         if e.buttons() & Qt.LeftButton:
  103.             self.move(e.globalPos()-self.dragPos)
  104.             e.accept()
  105.     def mousePressEvent(self, e):
  106.      
  107.         if e.button() == Qt.LeftButton:
  108.             self.dragPos=e.globalPos()-self.frameGeometry().topLeft()
  109.             e.accept()
  110.         if e.button() == Qt.RightButton and self.rightButton == False:
  111.             self.rightButton=True
  112.    
  113.     def paintEvent(self, e):
  114.         time = QTime.currentTime()
  115.         qp = QPainter()
  116.  
  117.         qp.begin(self)
  118.         #qp.setRenderHint(QPainter.Antialiasing)  # 开启这个抗锯齿,会很占cpu的!
  119.         qp.translate(self.width() / 2, self.height() / 2)
  120.         qp.scale(self.side / 200.0, self.side / 200.0)
  121.  
  122.         qp.setPen(QtCore.Qt.NoPen)
  123.         qp.setBrush(self.hourColor)
  124.         qp.save()
  125.         qp.rotate(30.0 * ((time.hour() + time.minute()/ 60.0)))
  126.         qp.drawConvexPolygon(self.hourHand)
  127.         qp.restore()
  128.        
  129.         qp.setPen(self.hourColor)
  130.         for i in range(12):
  131.             qp.drawLine(88, 0, 96, 0)
  132.             qp.rotate(30.0)
  133.        
  134.         qp.setPen(QtCore.Qt.NoPen)
  135.         qp.setBrush(self.minuteColor)
  136.         qp.save()
  137.        
  138.         qp.rotate(6.0 * ((time.minute() + (time.second()+time.msec()/1000.0) / 60.0)))
  139.         qp.drawConvexPolygon(self.minuteHand)
  140.         qp.restore()
  141.        
  142.        
  143.         qp.setPen(self.minuteColor)
  144.         for i in range(60):
  145.             if (i % 5) is not 0:
  146.                 qp.drawLine(92, 0, 96, 0)
  147.             qp.rotate(6.0)
  148.        
  149.         qp.setPen(QtCore.Qt.NoPen)
  150.         qp.setBrush(self.secondColor)
  151.         qp.save()
  152.         qp.rotate(6.0*(time.second()+time.msec()/1000.0))
  153.         qp.drawConvexPolygon(self.secondHand)
  154.         qp.restore()
  155.         qp.end()
  156. if __name__ == '__main__':
  157.     app = QtGui.QApplication(sys.argv)
  158.     clock = Clock()
  159.     sys.exit(app.exec_())
  160. #//python/1177

回复 "用PyQt实现透明桌面时钟小部件!"

这儿你可以回复上面这条便签

captcha