-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·93 lines (65 loc) · 2.42 KB
/
main.py
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
#!/usr/bin/python
import tkinter as tk
from PIL import Image, ImageGrab
from pynput import keyboard
import thumbs
import wm
import configparser
import os
class Previewer(tk.Frame):
def __init__(self, parent, ini):
super().__init__(parent)
self.conf = ini['Previewer']
self.thumb_conf = ini['Thumbnail']
# Falback to use until a screenshot is pulled from a workspace
fallback_bg = Image.new('RGB', (1, 1), self.conf['fallback-bg'])
# Create and pack a label for each workspace
self.previews = {w: tk.Label(self) for w in wm.workspaces()}
[p.pack(side=self.conf['side']) for p in self.previews.values()]
# Put a placeholder in each label
[self.update(p, fallback_bg) for p in self.previews]
def update(self, name, image):
img = thumbs.generate(image, name, self.thumb_conf)
self.previews[name].image = img
self.previews[name].configure(image=img)
class App(tk.Tk):
def __init__(self):
super().__init__()
ini = configparser.ConfigParser()
configPath = os.path.join(os.path.dirname(__file__), 'config.ini')
ini.read(configPath)
# Config for this widget
self.conf = ini['BWP']
self.overrideredirect(True)
self.geometry(f"+{self.conf['window-x']}+{self.conf['window-y']}")
self.preview = Previewer(self, ini)
self.preview.pack(fill=tk.BOTH)
# Hide window until mod is pressed
self.withdraw()
# Start updating
self.update()
def update(self):
# Schedule again after a second
self.after(self.conf['update-interval'], self.update)
# Grab desktop number
workspace = wm.current_workspace()
# Grab screenshot
image = ImageGrab.grab()
# Crop to region of interest
image = image.crop(wm.current_bounds())
# Update preview
self.preview.update(workspace, image)
def show(self):
self.deiconify()
self.lift()
def hide(self):
self.withdraw()
HOTKEY = keyboard.Key.cmd
if __name__ == "__main__":
a = App()
# Run a function if the key specified is the HOTKEY
check = lambda func : lambda key : func() if key == HOTKEY else 0
listener = keyboard.Listener(on_press=check(a.show),
on_release=check(a.hide))
listener.start()
a.mainloop()