python实现模拟时钟代码推荐

python实现模拟时钟代码推荐

# coding=utf8
import sys, pygame, math, random
from pygame.locals import *
from datetime import datetime, date, time
def print_text(font, x, y, text, color=(255,255,255)):
imgtext = font.render(text, true, color)
screen.blit(imgtext, (x,y))
def wrap_angle(angle):
return abs(angle%360)
# main
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption(“clock”)
font = pygame.font.font(none, 36)
orange = 220,180,0
white = 255,255,255
yellow = 255,255,0
pink = 255,100,100
pos_x = 300
pos_y = 250
radius = 250
angle = 360
# repeating loop
while true:
for event in pygame.event.get():
if event.type == quit:
sys.exit()
keys = pygame.key.get_pressed()
if keys[k_escape]:
sys.exit()
screen.fill((0,0,100))
# draw circle
pygame.draw.circle(screen, white, (pos_x,pos_y), radius, 6)
# draw the clock number 1-12
for n in range(1,13):
angle = math.radians(n*(360/12)-90)
x = math.cos(angle)*(radius-20)-10
y = math.sin(angle)*(radius-20)-10
print_text(font, pos_x+x, pos_y+y, str(n))
# get the time of day
today = datetime.today()
hours = today.hour%12
minutes = today.minute
seconds = today.second
# draw the hours hand
hour_angle = wrap_angle(hours*(360/12)-90)
hour_angle = math.radians(hour_angle)
hour_x = math.cos(hour_angle)*(radius-80)
hour_y = math.sin(hour_angle)*(radius-80)
target = (pos_x+hour_x, pos_y+hour_y)
pygame.draw.line(screen, pink, (pos_x,pos_y), target, 12)
# draw the minutes hand
min_angle = wrap_angle(minutes*(360/60)-90)
min_angle = math.radians(min_angle)
min_x = math.cos(min_angle)*(radius-60)
min_y = math.sin(min_angle)*(radius-60)
target = (pos_x+min_x, pos_y+min_y)
pygame.draw.line(screen, orange, (pos_x,pos_y), target, 12)
# draw the seconds hand
sec_angle = wrap_angle(seconds*(360/60)-90)
sec_angle = math.radians(sec_angle)
sec_x = math.cos(sec_angle)*(radius-40)
sec_y = math.sin(sec_angle)*(radius-40)
target = (pos_x+sec_x, pos_y+sec_y)
pygame.draw.line(screen, yellow, (pos_x,pos_y), target, 12)
# draw the center
pygame.draw.circle(screen, white, (pos_x,pos_y), 20)
print_text(font, 0, 0, str(hours) + “:” + str(minutes) + “:” + str(seconds))
pygame.display.update()

再来一个例子

import sys
from pyqt4 import qtgui, qtcore
from pyqt4.qtcore import qt
from pyqt4.qtcore import qpoint
from pyqt4.qtcore import qtimer
from pyqt4.qtcore import qtime
from pyqt4.qtgui import qpainter
from pyqt4.qtgui import qcolor
from pyqt4.qtgui import qpolygon
from pyqt4.qtcore import signal as signal
class clock(qtgui.qwidget):
”’
classdocs
”’
def __init__(self):
”’
constructor
”’
super(clock, self).__init__()
self.hourcolor=qcolor(127, 0, 127);
self.minutecolor=qcolor(0, 127, 127, 191)
self.secondcolor=qcolor(127, 127,0,120)
self.initui()
self.timer = qtimer()
self.timer.timeout.connect(self.update)
self.timer.start(30)
self.show()
def handchange(self):
self.side = min(self.width(), self.height())
self.hand=(max(self.side/200,4), max(self.side/100,8), max(self.side/40,30))
self.hourhand=qpolygon([qpoint(self.hand[0],self.hand[1]),qpoint(-self.hand[0],self.hand[1]),qpoint(0,-self.hand[2])])
self.minutehand=qpolygon([qpoint(self.hand[0],self.hand[1]),qpoint(-self.hand[0],self.hand[1]),qpoint(0,-self.hand[2]*2)])
self.secondhand=qpolygon([qpoint(self.hand[0],self.hand[1]),qpoint(-self.hand[0],self.hand[1]),qpoint(0,-self.hand[2]*3)])
def set_transparency(self, enabled):
if enabled:
self.setautofillbackground(false)
else:
self.setattribute(qt.wa_nosystembackground, false)
#下面这种方式好像不行
# pal=qtgui.qpalette()
# pal.setcolor(qtgui.qpalette.background, qcolor(127, 127,10,120))
# self.setpalette(pal)
self.setattribute(qt.wa_translucentbackground, enabled)
self.repaint()
def initui(self):
self.setgeometry(300, 300, 300, 200)
self.setwindowtitle(‘clock’)
self.handchange()
self.rightbutton=false
# 下面两个配合实现窗体透明和置顶
sizegrip=qtgui.qsizegrip(self)
self.setwindowflags(qt.framelesswindowhint|qt.windowstaysontophint|qt.subwindow )
#self.setmousetracking(true);
self.trans=true
self.set_transparency(true)
quitaction = qtgui.qaction(qtgui.qicon(‘quit.png’), ‘&quit’, self)
self.connect(quitaction,signal(“triggered()”),qtgui.qapp.quit)
backaction = qtgui.qaction( ‘&back’, self)
self.connect(backaction,signal(“triggered()”),self.backclicked)
self.popmenu= qtgui.qmenu()
self.popmenu.addaction(quitaction)
self.popmenu.addaction(backaction)
def resizeevent(self, e):
self.handchange()
def backclicked(self):
if self.trans == true :
self.trans = false
self.set_transparency(false)
else:
self.trans = true
self.set_transparency(true)
def mousereleaseevent(self,e):
if self.rightbutton == true:
self.rightbutton=false
self.popmenu.popup(e.globalpos())
def mousemoveevent(self, e):
if e.buttons() & qt.leftbutton:
self.move(e.globalpos()-self.dragpos)
e.accept()
def mousepressevent(self, e):
if e.button() == qt.leftbutton:
self.dragpos=e.globalpos()-self.framegeometry().topleft()
e.accept()
if e.button() == qt.rightbutton and self.rightbutton == false:
self.rightbutton=true
def paintevent(self, e):
time = qtime.currenttime()
qp = qpainter()
qp.begin(self)
qp.setrenderhint(qpainter.antialiasing) # 开启这个抗锯齿,会很占cpu的!
qp.translate(self.width() / 2, self.height() / 2)
qp.scale(self.side / 200.0, self.side / 200.0)
qp.setpen(qtcore.qt.nopen)
qp.setbrush(self.hourcolor)
qp.save()
qp.rotate(30.0 * ((time.hour() + time.minute()/ 60.0)))
qp.drawconvexpolygon(self.hourhand)
qp.restore()
qp.setpen(self.hourcolor)
for i in range(12):
qp.drawline(88, 0, 96, 0)
qp.rotate(30.0)
qp.setpen(qtcore.qt.nopen)
qp.setbrush(self.minutecolor)
qp.save()
qp.rotate(6.0 * ((time.minute() + (time.second()+time.msec()/1000.0) / 60.0)))
qp.drawconvexpolygon(self.minutehand)
qp.restore()
qp.setpen(self.minutecolor)
for i in range(60):
if (i % 5) is not 0:
qp.drawline(92, 0, 96, 0)
qp.rotate(6.0)
qp.setpen(qtcore.qt.nopen)
qp.setbrush(self.secondcolor)
qp.save()
qp.rotate(6.0*(time.second()+time.msec()/1000.0))
qp.drawconvexpolygon(self.secondhand)
qp.restore()
qp.end()
if __name__ == ‘__main__’:
app = qtgui.qapplication(sys.argv)
clock = clock()
sys.exit(app.exec_())

例三:

主要是模仿了qt自带的clock例子,然后 通过 python的语法 进行实现,
同时添加了秒针 的实现
用到的工具 有qt 4.8;python 2.7 ;pyqt

import sys
from pyqt4.qtcore import *
from pyqt4.qtgui import *
class clock (qwidget):
def __init__(self):
qwidget.__init__(self,windowtitle=”python clock”)
timer = qtimer(self)
self.connect(timer, signal(“timeout()”),self,slot(“update()”))
timer.start(1000)
self.resize(200,200)
def paintevent(self,e):
hourcolorhand = qpolygon([qpoint(7,8),qpoint(-7,8),qpoint(0,-30)])
minutecolorhand = qpolygon([qpoint(7,8),qpoint(-7,8),qpoint(0,-70)])
secondcolorhand = qpolygon([qpoint(3,8),qpoint(-3,8),qpoint(0,-90)])
hourcolor = qcolor(127,0,127)
minutecolor = qcolor(0,127,127,191)
secondcolor = qcolor(0,100,100,100)
painter = qpainter(self);
side = min(self.width(),self.height())
atime =qtime.currenttime()
painter.setrenderhint(qpainter.antialiasing)
painter.translate(self.width()/2,self.height()/2)
painter.scale(side/200,side/200)
painter.setpen(qt.nopen)
painter.setbrush(hourcolor)
painter.save()
painter.rotate(30.0*(atime.hour() + atime.minute()/60.0))
painter.drawconvexpolygon(hourcolorhand)
painter.restore()
painter.setpen(hourcolor)
for i in range(0,12):
painter.drawline(88,0,96,0)
painter.rotate(30.0)
painter.setpen(qt.nopen)
painter.setbrush(minutecolor)
painter.save()
painter.rotate(6.0*(atime.minute()+atime.second()/60.0))
painter.drawconvexpolygon(minutecolorhand)
painter.restore()
painter.setpen(minutecolor)
for i in range(0,60) :
if (i%5)!=0 :
painter.drawline(92,0,96,0)
painter.rotate(6.0)
painter.setpen(qt.nopen)
painter.setbrush(secondcolor)
painter.save()
painter.rotate(6.0 * atime.second())
painter.drawconvexpolygon(secondcolorhand)
painter.restore()
if __name__ == “__main__” :
q = qapplication(sys.argv)
s = clock()
s.show()
q.exec_()

Posted in 未分类