Python implementation analog Clock code recommendation

Source: Internet
Author: User
Python implementation analog Clock code recommendation

# coding=utf8import sys, pygame, math, randomfrom pygame.locals import *from datetime import datetime, date, Time def pri  Nt_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) # mainpygame.init () screen = Pygame.display.set_mode ((600,500)) Pygame.display.set_caption ("CLOCK") Font = Pygame.font.Font (None, *) orange = 220,180,0white = 255,255,255yellow = 255,255,0pink = 255,100,100 pos_x = 300pos_y = 250radius = 250angle = * # repeating Loopwhile 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 = ma Th.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 (ho Ur_angle) hour_x = Math.Cos (hour_angle) * (radius-80) hour_y = Math.sin (hour_angle) * (radius-80) target = (pos_x+hour_x, p os_y+hour_y) pygame.draw.line (screen, Pink, (pos_x,pos_y), Target, each) # 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_a Ngle) * (radius-60) target = (pos_x+min_x, pos_y+min_y) pygame.draw.line (screen, Orange, (pos_x,pos_y), Target,) # DR Aw the seconds hand sec_angle = Wrap_angle (seconds* (360/60) -90) Sec_angle = Math.radians (sec_angle) sec_x = Math.Cos (SE C_angle) * (radius-40) sec_y = Math.sin (sec_angle) * (radius-40) target = (pos_x+sec_x, pos_y+sec_y) pygame.draw.line (scre En, Yellow, (pos_x,pos_y), Target, () # Draw the Center pygame.draw.circle (screen, white, (pos_x,pos_y), 0) print_text (font, 0, str (hours ) + ":" + str (minutes) + ":" + str (seconds)) Pygame.display.update ()

One more example.

Import sysfrom PyQt4 import Qtgui, qtcore from pyqt4.qtcore import qtfrom pyqt4.qtcore import qpointfrom pyqt4.qtcore impo RT qtimerfrom Pyqt4.qtcore Import qtime from Pyqt4.qtgui import qpainterfrom Pyqt4.qtgui import qcolor from Pyqt4.qtgui im Port Qpolygonfrom pyqt4.qtcore Import SIGNAL as Signalclass 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 = QTim ER () self.timer.timeout.connect (self.update) Self.timer.start (+) self.show () def handchange (self): SELF.S    ide = 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_n Osystembackground, 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 i Nitui (self): self.setgeometry (-) self.setwindowtitle (' Clock ') self.handchange () self.right Button=false # The following two mates implement the form transparent and pinned 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 (' &amp ; Back ', self) self.connect (backaction,signal ("triggered ()"), self.backclicked) self.popmenu= Qtgui.qmenu () self.po Pmenu.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:se Lf.trans = True Self.set_transparency (TRUE) def mousereleaseevent (self,e): if Self.rightbutton = = True:se Lf.rightbutton=false Self.popMenu.popup (E.globalpos ()) def mousemoveevent (self, E): If E.buttons () & QT.LEFTB Utton:self.move (E.globalpos ()-self.dragpos) e.accept () def mousepressevent (self, E): if E.button () = = Qt.le Ftbutton:self.dragpos=e.globalpos ()-self.framegeometry (). TopLeft () e.accept () if e.button () = = Qt.RightBut Ton and Self.rightbutton = = False:self.rightbutton=true def paintevent (self, e): Time = Qtime.currenttime () QP = Qpainter () qp.    Begin (self) qp.setrenderhint (qpainter.antialiasing) # turn on this antialiasing and it will be CPU-bound! Qp.translate (Self.width ()/2, Self.height ()/2) Qp.scale (self.side/200.0, self.side/200.0) Qp.setpen (QtCore.Q T.nopen) Qp.setbrush (Self.hourcolor) Qp.save () qp.rotate (30.0 * ((Time.hour () + Time.minute ()/60.0)) Qp.draw Convexpolygon (Self.hourhand) Qp.restore () Qp.setpen (Self.hourcolor) for I in range: Qp.drawline (88, 0, 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: if (i% 5) are not 0:qp.drawline (0, 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.re Store () qp.end () if __name__ = = ' __main__ ': app = qtgui.qapplication (sys.argv) clock = Clock () sys.exit (App.exec_ () )

Example three:

The main is to imitate the QT comes with the clock example, and then through the Python syntax to implement,
The implementation of the second hand is also added
The tools used are QT 4.8;python 2.7;p YQT

Import sysfrom 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 (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 = QPolygo N ([Qpoint (3,8), Qpoint ( -3,8), Qpoint (0,-90)]) Hourcolor = Qcolor (127,0,127) Minutecolor = Qcolor (0,127,127,191) sec    Ondcolor = Qcolor (0,100,100,100) painter = Qpainter (self); Side = Min (Self.width (), Self.height ()) Atime =qtime.currenttime () painter.setrenderhint (qpainter.antialiasing) PA Inter.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 (M Inutecolor) Painter.save () painter.rotate (6.0* (Atime.minute () +atime.second ()/60.0)) Painter.drawconvexpolygon (min Utecolorhand) Painter.restore () Painter.setpen (Minutecolor) for I in Range (0,60): if (i%5)!=0:painte R.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_ ()
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.