@PySimpleGUI/

timer.py

Python

A stopwatch style timer

fork
loading
Files
  • main.py

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.4683765892011793","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"aW1wb3J0IFB5U2ltcGxlR1VJV2ViIGFzIHNnCmltcG9ydCB0aW1lCnByaW50KCdTdGFydGluZyB1cC4uLicpICAgIyBUaGlzIHNlZW1zIHRvIGJlIG5lY2Nlc3Nhcnkgc29tZXRpbWVzCgojIC0tLS0tLS0tLS0tLS0tLS0gIENyZWF0ZSBGb3JtICAtLS0tLS0tLS0tLS0tLS0tCmxheW91dCA9IFtbc2cuVGV4dCgnJywgYmFja2dyb3VuZF9jb2xvcj0nYmxhY2snKV0sCiAgICBbc2cuVGV4dCgnMDA6MDAnLCBzaXplPSgzMCwgMSksIGZvbnQ9KCdIZWx2ZXRpY2EnLCAzMCksIGp1c3RpZmljYXRpb249J2NlbnRlcicsIHRleHRfY29sb3I9J3doaXRlJywga2V5PSd0ZXh0JywgYmFja2dyb3VuZF9jb2xvcj0nYmxhY2snKV0sCiAgICBbc2cuVGV4dCgnJywgYmFja2dyb3VuZF9jb2xvcj0nYmxhY2snKV0sCiAgICBbc2cuQnV0dG9uKCdQYXVzZScsIGtleT0nYnV0dG9uJywgYnV0dG9uX2NvbG9yPSgnd2hpdGUnLCAnIzAwMTQ4MCcpLCBwYWQ9KCg4MCw0KSwyKSksCiAgICAgc2cuQnV0dG9uKCdSZXNldCcsIGJ1dHRvbl9jb2xvcj0oJ3doaXRlJywgJyMwMDczMzknKSwga2V5PSdSZXNldCcpLAogICAgIHNnLkV4aXQoYnV0dG9uX2NvbG9yPSgnd2hpdGUnLCAnIzhCMUExQScpLCBrZXk9J0V4aXQnLCApXSwgXQoKCndpbmRvdyA9IHNnLldpbmRvdygnUnVubmluZyBUaW1lcicsIGJhY2tncm91bmRfY29sb3I9J2JsYWNrJywgZm9udD0nSGVsdmV0aWNhIDE4Jywgd2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90aW1lID0gaW50KHJvdW5kKHRpbWUudGltZSgpICogMTAwKSkKICAgICAgICBlbGVtZW50ID0gd2luZG93LkZpbmRFbGVtZW50KCdidXR0b24nKQogICAgICAgIGVsZW1lbnQuVXBkYXRlKHRleHQ9J1J1bicpCiAgICBlbGlmIGV2ZW50ID09ICdSdW4nOgogICAgICAgIHBhdXNlZCA9IEZhbHNlCiAgICAgICAgc3RhcnRfdGltZSA9IHN0YXJ0X3RpbWUgKyBpbnQocm91bmQodGltZS50aW1lKCkgKiAxMDApKSAtIHBhdXNlZF90aW1lCiAgICAgICAgZWxlbWVudCA9IHdpbmRvdy5GaW5kRWxlbWVudCgnYnV0dG9uJykKICAgICAgICBlbGVtZW50LlVwZGF0ZSh0ZXh0PSdQYXVzZScpCgogICAgIyAtLS0tLS0tLS0gRGlzcGxheSB0aW1lciBpbiB3aW5kb3cgLS0tLS0tLS0KICAgIHdpbmRvdy5GaW5kRWxlbWVudCgndGV4dCcpLlVwZGF0ZSgnezowMmR9Ons6MDJkfS57OjAyZH0nLmZvcm1hdCgoY3VycmVudF90aW1lIC8vIDEwMCkgLy8gNjAsCiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIChjdXJyZW50X3RpbWUgLy8gMTAwKSAlIDYwLAogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBjdXJyZW50X3RpbWUgJSAxMDApKQojIC0tLS0tLS0tLSBBZnRlciBsb29wIC0tLS0tLS0tCndpbmRvdy5DbG9zZSgpCg=="},"asBuffer":null},"loaded":true}}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import PySimpleGUIWeb as sg
import time
print('Starting up...')   # This seems to be neccessary sometimes

# ----------------  Create Form  ----------------
layout = [[sg.Text('', background_color='black')],
    [sg.Text('00:00', size=(30, 1), font=('Helvetica', 30), justification='center', text_color='white', key='text', background_color='black')],
    [sg.Text('', background_color='black')],
    [sg.Button('Pause', key='button', button_color=('white', '#001480'), pad=((80,4),2)),
     sg.Button('Reset', button_color=('white', '#007339'), key='Reset'),
     sg.Exit(button_color=('white', '#8B1A1A'), key='Exit', )], ]


window = sg.Window('Running Timer', background_color='black', font='Helvetica 18', web_multiple_instance=False).Layout(layout)

# ----------------  Event Loop  ----------------
current_time = 0
paused = False
start_time = int(round(time.time() * 100))

while (True):
    # --------- Read and update window --------
    if not paused:
        event, values = window.Read(timeout=0)
        current_time = int(round(time.time() * 100)) - start_time
    else:
        event, values = window.Read()
    if event == 'button':
        event = window.FindElement(event).GetText()
    # --------- Do Button Operations --------
    if event is None or event == 'Exit':        # ALWAYS give a way out of program
        break
    elif event is 'Reset':
        start_time = int(round(time.time() * 100))
        current_time = 0
        paused_time = start_time
    elif event == 'Pause':
        paused = True
        paused_time = int(round(time.time() * 100))
        element = window.FindElement('button')
        element.Update(text='Run')
    elif event == 'Run':
        paused = False
        start_time = start_time + int(round(time.time() * 100)) - paused_time
        element = window.FindElement('button')
        element.Update(text='Pause')

    # --------- Display timer in window --------
    window.FindElement('text').Update('{:02d}:{:02d}.{:02d}'.format((current_time // 100) // 60,
                                                                  (current_time // 100) % 60,
                                                                  current_time % 100))
# --------- After loop --------
window.Close()