from PyQt5 import QtCore, QtGui,QtWidgetsfrom PyQt5.QtGui import QFont
class AnalogClock(QtWidgets.QWidget): hourHand = QtGui.QPolygon([ QtCore.QPoint(10, 8), QtCore.QPoint(-10, 8), QtCore.QPoint(0, -60) ]) minuteHand = QtGui.QPolygon([ QtCore.QPoint(8, 8), QtCore.QPoint(-8, 8), QtCore.QPoint(0, -70) ]) secondHand = QtGui.QPolygon([ QtCore.QPoint(4, 8), QtCore.QPoint(-4, 8), QtCore.QPoint(0, -90) ]) hourColor = QtGui.QColor(255, 0, 0) minuteColor = QtGui.QColor(0, 255, 0) secondColor = QtGui.QColor(0, 0, 255) black_=QtGui.QColor(0, 0, 0) white_=QtGui.QColor(255, 255, 255) y=0 aa=1 def __init__(self, parent=None): super(AnalogClock, self).__init__(parent) timer = QtCore.QTimer(self) timer.timeout.connect(self.update) timer.start(500) # 每1000ms 调用self.update, self.update会调用self.paintEvent self.setWindowTitle("Analog Clock") self.setStyleSheet("background-color: rgb(0, 0, 0);") self.resize(800, 480) self.font = QFont("宋体", 5) self.font.setBold(True)
def paintEvent(self, event): side = min(self.width(), self.height()) time = QtCore.QTime.currentTime()#获取系统当前时间 painter = QtGui.QPainter(self) painter.setRenderHint(QtGui.QPainter.Antialiasing)#抗锯齿 painter.translate(self.width() / 2, self.height() / 3) painter.scale(side / 200.0, side / 200.0) painter.setPen(QtGui.QColor(0, 0, 0)) #painter.drawEllipse(-100,-100,200,200)#画圆。参数是外接矩形左上点和长宽 #painter.drawEllipse(-10,-10,20,20) #painter.drawLine(92, 0, 96, 0) painter.rotate(45.0) list_=[40,30,20,10,0,10,20,30,40,0,0] painter.setPen(AnalogClock.white_) painter.setFont(self.font) cnt_=0 self.y+=self.aa if(self.y>=100): self.aa=-1 if(self.y<=-100): self.aa=1 print(self.y) for j in range(91): painter.setPen(AnalogClock.white_) if(j%5)!=0 : painter.drawLine(92, 0, 96, 0) else: if(j%5)==0 and (j%10!=0): a=0 x=100+cnt_*0.5 str_='{0}'.format(list_[cnt_]) painter.drawText(x,0,str_) cnt_+=1 if(cnt_>=len(list_)-1): cnt_=len(list_)-1 painter.drawLine(90, 0, 96, 0) else: painter.drawLine(88, 0, 96, 0) painter.rotate(1.0)
painter.setPen(QtCore.Qt.NoPen) painter.setBrush(AnalogClock.hourColor) painter.save() painter.setPen(QtCore.Qt.NoPen) painter.setBrush(AnalogClock.minuteColor) painter.save() painter.setPen(QtCore.Qt.NoPen) painter.setBrush(AnalogClock.secondColor) painter.save() #painter.rotate(6.0 * time.second()) #painter.drawConvexPolygon(AnalogClock.secondHand) #painter.restore() painter.setPen(QtGui.QColor(0, 0, 0)) painter.drawEllipse(-5,-5,10,10)#画圆。参数是外接矩形左上点和长宽
if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) clock = AnalogClock() clock.show() sys.exit(app.exec_())
class AnalogClock(QtWidgets.QWidget): hourHand = QtGui.QPolygon([ QtCore.QPoint(10, 8), QtCore.QPoint(-10, 8), QtCore.QPoint(0, -60) ]) minuteHand = QtGui.QPolygon([ QtCore.QPoint(8, 8), QtCore.QPoint(-8, 8), QtCore.QPoint(0, -70) ]) secondHand = QtGui.QPolygon([ QtCore.QPoint(4, 8), QtCore.QPoint(-4, 8), QtCore.QPoint(0, -90) ]) hourColor = QtGui.QColor(255, 0, 0) minuteColor = QtGui.QColor(0, 255, 0) secondColor = QtGui.QColor(0, 0, 255) black_=QtGui.QColor(0, 0, 0) white_=QtGui.QColor(255, 255, 255) y=0 aa=1 def __init__(self, parent=None): super(AnalogClock, self).__init__(parent) timer = QtCore.QTimer(self) timer.timeout.connect(self.update) timer.start(500) # 每1000ms 调用self.update, self.update会调用self.paintEvent self.setWindowTitle("Analog Clock") self.setStyleSheet("background-color: rgb(0, 0, 0);") self.resize(800, 480) self.font = QFont("宋体", 5) self.font.setBold(True)
def paintEvent(self, event): side = min(self.width(), self.height()) time = QtCore.QTime.currentTime()#获取系统当前时间 painter = QtGui.QPainter(self) painter.setRenderHint(QtGui.QPainter.Antialiasing)#抗锯齿 painter.translate(self.width() / 2, self.height() / 3) painter.scale(side / 200.0, side / 200.0) painter.setPen(QtGui.QColor(0, 0, 0)) #painter.drawEllipse(-100,-100,200,200)#画圆。参数是外接矩形左上点和长宽 #painter.drawEllipse(-10,-10,20,20) #painter.drawLine(92, 0, 96, 0) painter.rotate(45.0) list_=[40,30,20,10,0,10,20,30,40,0,0] painter.setPen(AnalogClock.white_) painter.setFont(self.font) cnt_=0 self.y+=self.aa if(self.y>=100): self.aa=-1 if(self.y<=-100): self.aa=1 print(self.y) for j in range(91): painter.setPen(AnalogClock.white_) if(j%5)!=0 : painter.drawLine(92, 0, 96, 0) else: if(j%5)==0 and (j%10!=0): a=0 x=100+cnt_*0.5 str_='{0}'.format(list_[cnt_]) painter.drawText(x,0,str_) cnt_+=1 if(cnt_>=len(list_)-1): cnt_=len(list_)-1 painter.drawLine(90, 0, 96, 0) else: painter.drawLine(88, 0, 96, 0) painter.rotate(1.0)
painter.setPen(QtCore.Qt.NoPen) painter.setBrush(AnalogClock.hourColor) painter.save() painter.setPen(QtCore.Qt.NoPen) painter.setBrush(AnalogClock.minuteColor) painter.save() painter.setPen(QtCore.Qt.NoPen) painter.setBrush(AnalogClock.secondColor) painter.save() #painter.rotate(6.0 * time.second()) #painter.drawConvexPolygon(AnalogClock.secondHand) #painter.restore() painter.setPen(QtGui.QColor(0, 0, 0)) painter.drawEllipse(-5,-5,10,10)#画圆。参数是外接矩形左上点和长宽
if __name__ == '__main__': import sys app = QtWidgets.QApplication(sys.argv) clock = AnalogClock() clock.show() sys.exit(app.exec_())