Amazing-Python-Scripts

Форк
0
85 строк · 1.9 Кб
1
import tkinter as tk
2
import tkinter.font as TkFont
3
from datetime import datetime
4

5

6
def timer():
7
    global work
8
    if work:
9
        now = str(txt_var.get())
10
        m, s = map(int, now.split(":"))
11
        m = int(m)
12
        s = int(s)
13
        if (s < 59):
14
            s += 1
15
        elif (s == 59):
16
            s = 0
17
            if (m < 59):
18
                m += 1
19
            elif (m == 59):
20
                m = 0
21
        if (m < 10):
22
            m = str(0)+str(m)
23
        else:
24
            m = str(m)
25
        if (s < 10):
26
            s = str(0)+str(s)
27
        else:
28
            s = str(s)
29
        now = m+":"+s
30

31
        txt_var.set(now)
32
        if work:
33
            root.after(1000, timer)
34
# start function
35

36

37
def start():
38
    global work
39
    if not work:
40
        work = True
41
        timer()
42

43
# stop function
44

45

46
def pause():
47
    global work
48
    work = False
49

50
# reset function
51

52

53
def reset():
54
    global work
55
    if not work:
56
        txt_var.set('0:00')
57

58

59
if __name__ == "__main__":
60
    work = False
61

62
    root = tk.Tk()
63
    root.geometry("500x221")  # width x height
64
    root.title("My StopWatch")
65

66
    txt_var = tk.StringVar()
67
    txt_var.set('0:00')  # initial display of string
68
    root.config(background="lavender")
69

70
    fontstyle = TkFont.Font(family="Helvetica", size=60,)
71
    tk.Label(root, textvariable=txt_var, font=fontstyle,).pack()
72

73
    # creating the buttons for start,stop and reset
74
    T = tk.Text(root, height=0.7, width=9)
75
    T.pack()
76
    T.insert(tk.END, " mm : ss ")
77
    tk.Button(root, text="Start", command=start,
78
              bg='misty rose').pack(fill='x')
79
    tk.Button(root, text='Pause', command=pause,
80
              bg='misty rose').pack(fill='x')
81
    tk.Button(root, text='Reset', command=reset,
82
              bg='misty rose').pack(fill='x')
83
    tk.Button(root, text='Exit', command=root.destroy,
84
              bg='misty rose').pack(fill='x')
85
    root.mainloop()
86

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.