#!/usr/bin/env Python3#-*-Coding:utf-8-*-
#时钟
fromTurtleImport* fromDatetimeImport*defSkip (STEP): Penup () forward (step) Pendown ()defMkhand (name, length):#register the Turtle shape and set the needle turtleReset () Skip (-length*0.1) Begin_poly ()#start recording Brush coordinatesForward (length*1.1)#The brush moves forward length*1.1End_poly ()#End record Brush coordinatesHandform = Get_poly ()#record brush start and end coordinate positions (one tuple of data)Register_shape (name, Handform)#Register this shape defInit ():GlobalSechand, Minhand, Hurhand, printer mode ("logo")#Reset Turtle pointing North #set up three needle turtle and initializeMkhand ("Sechand", 125) Mkhand ("Minhand", 130) Mkhand ("Hurhand", 90) Sechand=Turtle () sechand.shape ("Sechand") Minhand=Turtle () minhand.shape ("Minhand") Hurhand=Turtle () hurhand.shape ("Hurhand") forHandinchSechand, Minhand, HurHand:hand.shapesize (1, 1, 3) hand.speed (0)#Create output Text turtlePrinter =Turtle () printer.hideturtle ( )#Hide BrushesPrinter.penup ()defSetupclock (RADIUS):#Create a table's outer frameReset () pensize (7)#Brush Size forIinchRange (60): Skip (RADIUS)#The brush is lifted, moving forward "radius" specific ifI% 5 = =0:forward (20)#if it can be divisible by 5, move forward.Skip (-radius-20)#brush back to original position Else: Dot (5)#draw a 5-pixel dotSkip (-radius)#brush back to original positionRight (6)#6 radians to the right of each cycle defWeek (t): Week= ["Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday","Sunday"] returnWeek[t.weekday ()]#returns the current day of the week defDate (t): Y=t.year M=T.month D=T.dayreturn "%s%d%d"% (Y, M, D)#returns the current date defTick ():#draw the dynamic display of the needlet =Datetime.today () Second= T.second + t.microsecond*0.000001#accurate to microsecondsminute = T.minute + second/60.0#the exact secondshour = T.hour + minute/60.0#accurate to minuteSechand.setheading (6*second)#second hand set angle 360 degrees, one lap 60 seconds 360/60 = 6Minhand.setheading (6*minute)#minute hand set angle 360 degrees, one lap 60 Minutes 360/60 = 6Hurhand.setheading (30*hour)#the angle set by the hour hand is 360 degrees, one lap 12 hours 360/12 =Tracer (False)#cancels the animation, the word hits directly on the canvasPrinter.forward (65) Printer.write (Week (t), align="Center", Font=("Courier", 14,"Bold")) Printer.back (130) Printer.write (Date (t), align="Center", Font=("Courier", 14,"Bold") ) Printer.home () tracer (True)#Turn on animationOnTimer (Tick,100)#continue calling tick after 100ms defMain (): Tracer (False) Init ()#draw the needle.Setupclock (160)#draw the dial.Tracer (True) Tick ()#let the hands move and the words write .Mainloop ()if __name__=="__main__": Main ()
#!/usr/bin/env Python3#-*-Coding:utf-8-*-
#键盘值查询
fromTkinterImport*Root=Tk ()#create a framework in which to respond to eventsFrame=frame (root,width=256,height=256) defCallBack (event):Print(Event.keysym) frame.bind ("<KeyPress>", CallBack) frame.pack ()#current frame is selected, meaning keyboard trigger, only valid for this frameFrame.focus_set () mainloop ( )
Python Learning--interactive graphic programming example three