-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasynctkcore.py
More file actions
136 lines (115 loc) · 4.37 KB
/
asynctkcore.py
File metadata and controls
136 lines (115 loc) · 4.37 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import tkinter as tk
from tkinter import ttk
import asyncio
import traceback
class AlphaAnimation:
def fade_animation_init(self):
self.target_alpha = 0
self.current_alpha = 0
self.attributes("-alpha", 0)
def get_alpha(self):
return self.target_alpha
def set_alpha(self, alpha, force=False):
if force:
self.target_alpha = self.current_alpha = alpha
self.attributes("-alpha", round(self.current_alpha, 4))
else:
self.target_alpha = round(alpha, 4)
def fade_animation(self):
if self.current_alpha != self.target_alpha:
if abs(self.current_alpha - self.target_alpha) > 0.01:
if self.current_alpha > self.target_alpha:
self.current_alpha -= max(0.005, (self.current_alpha - self.target_alpha) / 5)
else:
self.current_alpha += max(0.005, (self.target_alpha - self.current_alpha) / 5)
else:
self.current_alpha = self.target_alpha
# print("done",self.target_alpha)
self.attributes("-alpha", round(self.current_alpha, 4))
def reset_alpha(self):
self.current_alpha = 0
self.attributes("-alpha", 0)
class AsyncTk(tk.Tk, AlphaAnimation):
def __init__(self, loop, interval=1 / 61):
super().__init__()
self.loop = loop
self.protocol("WM_DELETE_WINDOW", self.close)
self.tasks = []
self.tasks.append(self.loop.create_task(self.updater(interval)))
self.fade_animation_init()
self.callback_queue = list()
async def updater(self, interval):
while True:
self.update()
self.fade_animation()
if self.callback_queue:
for callback in self.callback_queue:
self.tasks.append(self.loop.create_task(AsyncTk.patch_callback(callback)))
self.callback_queue = list()
await asyncio.sleep(interval)
@staticmethod
async def patch_callback(task):
callback, x = task
try:
if asyncio.iscoroutinefunction(callback):
await callback(*x)
else:
callback(*x)
except Exception:
traceback.print_exc()
def append_callback(self, callback, *x):
self.callback_queue.append((callback, x))
def close(self):
for task in self.tasks:
task.cancel()
self.loop.stop()
self.destroy()
class AsyncToplevel(tk.Toplevel, AlphaAnimation):
def __init__(self, root, interval=1 / 61):
super().__init__(root)
self.loop = root.loop
self.protocol("WM_DELETE_WINDOW", self.close)
self.tasks = []
self.tasks.append(self.loop.create_task(self.updater(interval)))
self.fade_animation_init()
async def updater(self, interval):
#self.update()
while True:
self.fade_animation()
await asyncio.sleep(interval)
def close(self):
for task in self.tasks:
task.cancel()
self.destroy()
class OnHover:
def __init__(self, widget, field, value):
self.widget = widget
self.field = field
self.target_value = value
self.restore_value = self.widget.cget(self.field)
self.widget.bind("<Enter>", self.enter)
self.widget.bind("<Leave>", self.leave)
self.widget.bind("<ButtonPress>", self.leave)
def enter(self, event=None):
self.widget.config(**{self.field: self.target_value})
def leave(self, event=None):
self.widget.config(**{self.field: self.restore_value})
def destroy(self):
self.widget.unbind("<Enter>")
self.widget.unbind("<Leave>")
self.widget.unbind("<ButtonPress>")
class Dropdown:
dd_value = None
dd_options = None
dd = None
def create(self, root, options, command=None):
self.dd_options = options
self.dd_value = tk.StringVar(root)
self.dd_value.set(options[0]) # default value
if command: self.dd = tk.OptionMenu(root, self.dd_value, *options, command=lambda x,c=self.dd_options: command(c,x))
else: self.dd = tk.OptionMenu(root, self.dd_value, *options)
self.dd.config(highlightthickness = 0)
def get(self):
self.dd_value.get()
def set(self,value):
self.dd_value.set(value)