Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- import time
- screen = turtle.Screen()
- screen.title("Секундомер")
- elapsed = 0.0
- running = False
- last_update = 0.0
- display = turtle.Turtle()
- display.hideturtle()
- display.penup()
- display.goto(0, 50)
- def format_time(t):
- h = int(t // 3600)
- m = int((t % 3600) // 60)
- s = int(t % 60)
- return f"{h:02d}:{m:02d}:{s:02d}"
- def update_clock():
- global elapsed, last_update
- if running:
- now = time.time()
- elapsed += now - last_update
- last_update = now
- display.clear()
- display.write(format_time(elapsed), align="center", font=("Arial", 24, "normal"))
- screen.ontimer(update_clock, 100)
- def start(x=None, y=None):
- global running, last_update
- if not running:
- running = True
- last_update = time.time()
- update_clock()
- def pause(x=None, y=None):
- global running, elapsed
- if running:
- running = False
- display.clear()
- display.write(format_time(elapsed), align="center", font=("Arial", 24, "normal"))
- def stop(x=None, y=None):
- global running, elapsed
- running = False
- elapsed = 0.0
- display.clear()
- display.write(format_time(elapsed), align="center", font=("Arial", 24, "normal"))
- def make_button(label, pos, callback):
- btn = turtle.Turtle()
- btn.hideturtle()
- btn.penup()
- btn.shape("square")
- btn.shapesize(stretch_wid=1, stretch_len=4)
- btn.fillcolor("lightgray")
- btn.goto(pos)
- btn.showturtle()
- btn.write(label, align="center", font=("Arial", 12, "normal"))
- btn.onclick(callback)
- return btn
- make_button("START", (-100, -50), start)
- make_button("PAUSE", (0, -50), pause)
- make_button("STOP", (100, -50), stop)
- display.write("00:00:00", align="center", font=("Arial", 24, "normal"))
- screen.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment