-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscreen-shawty.py
More file actions
102 lines (77 loc) · 2.78 KB
/
screen-shawty.py
File metadata and controls
102 lines (77 loc) · 2.78 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
is_running = False # Is the app polling keyboard input?
#################
r'''
S .'| _ _ .-. .-
C < | /\ \\ // .|\ \ / /
R | | __`\\ //\\ // .' |_\ \ / /
e _ | | .'--. .:--.'.\`// \'/.' |\ \ / /
e .' | | |/.'-. \ / | \ |\| |/'--. .-' \ \ / /
.N | /| / | | `" __ | | ' | | \ ` /
.'.'| |//| | | | .'.''| | | | \ /
.'.'.-' / | | | | / / | |_ | '.' / /
.' \_.' | '. | '.\ \._,\ '/ | /|`-' /
'---' '---'`--' `" `'-' '..'
'''
#################
import keyboard
from sys import platform
from time import time
from time import sleep
from pathlib import Path
# This is hacky, but has to be done to handle display scaling
# This should be handled upstream by PIL
# It also may need to be called BEFORE the import of PIL.ImageGrab
if platform == 'win32':
from ctypes import windll
user32 = windll.user32
user32.SetProcessDPIAware()
from PIL import ImageGrab
import tkinter as tk
#################
def start_stop(ON = 'green', OFF = 'red'):
global is_running
if toggle_button['text'] == 'Start':
is_running = True
toggle_button.config(text = "Stop", bg = OFF)
else:
is_running = False
toggle_button.config(text = "Start", bg = ON)
def take_screen_shot():
# time() returns a float, but we need to use it in a filename
current_time = str(time()).replace('.','_')
# Literally ~/Pictures/ (we'll see if it exists later...)
picture_dir = Path.home().joinpath('Pictures')
# State of the art error handling!
if not picture_dir.exists():
try:
picture_dir.mkdir(parents = True, exist_ok = True)
except Exception as e:
print(e) #TODO: implement logging module
save_path = picture_dir.joinpath(f'Screen Shot {current_time}.png')
screen_shot = ImageGrab.grab()
screen_shot.save(save_path)
# We sleep here not to be lazy but to anticipate button_up
sleep(0.2)
def shot_loop():
if is_running:
if keyboard.is_pressed('print screen'):
take_screen_shot()
main_frame.after(POLL_DELAY, shot_loop)
#################
VERSION = '0.03b' # TODO: __init__
POLL_DELAY = 150 # ~6.66 polls for keyboard per second.
HEIGHT = 350
WIDTH = 350
main_frame = tk.Tk()
main_frame.title(f"screen-shawty {VERSION}")
main_frame.geometry(f"{WIDTH}x{HEIGHT}")
app = tk.Frame(main_frame)
app.pack(expand = True, fill = 'both')
toggle_button = tk.Button(
app, text = "Start", bg = 'green',
font = (1), command = start_stop
)
toggle_button.pack(expand = True, fill = 'both')
main_frame.after(POLL_DELAY, shot_loop)
main_frame.mainloop()