-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget.py
119 lines (83 loc) · 3.43 KB
/
widget.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
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
from turtle import st
from ttkbootstrap import style
from TkTrans import TransparentWindow
import ttkbootstrap as ttk
from ttkbootstrap import Style
import json
class Widget(TransparentWindow):
def __init__(self, jsondata: dict = {}, root=ttk.Window(), percent=50, theme="dark", *args, **kwargs):
if theme == "dark":
self.bkg = (34, 34, 34)
self.app = Style(theme='darkly').master
elif theme == 'light':
self.bkg = 'white'
else:
self.bkg = theme
self.style = style
super().__init__(root, percent=percent, bg=self.bkg, *args, **kwargs)
self.app = self.root
self.app.overrideredirect(True)
self.bind('<Button-1>', self.get_pos)
self.configfile = "widgetconfig"
self.widgetname = type(self).__name__
self.jsondata = jsondata
self.data = self.open_config()
self.xwin = self.data["xwin"]
self.ywin = self.data["ywin"]
self.app.geometry(f'+{self.xwin}+{self.ywin}')
self.app.bind("<Alt-F4>", self.exit)
def open_config(self):
try:
with open(self.configfile+'.json', 'r') as f:
data = json.load(f)
if data is None:
raise FileNotFoundError
except FileNotFoundError:
data = {self.widgetname: {
"xwin":0,
"ywin":0}
}
with open(self.configfile+'.json', 'w') as f:
json.dump(data, f)
if not self.widgetname in data:
data[self.widgetname] = {"xwin": 0, "ywin": 0}
with open(self.configfile+'.json', 'w') as f:
json.dump(data, f)
return data[self.widgetname]
def update_config_file(self):
with open(self.configfile+'.json', 'r') as f:
content = json.load(f)
with open(self.configfile+'.json', 'w') as f:
content[self.widgetname] = self.data
json.dump(content, f)
def update_position(self, newx: int, newy: int):
self.data["xwin"] = newx
self.data["ywin"] = newy
self.update_config_file()
def get_pos(self, event):
xwin = self.app.winfo_x()
ywin = self.app.winfo_y()
startx = event.x_root
starty = event.y_root
ywin = ywin - starty
xwin = xwin - startx
def move_window(event):
self.app.config(cursor="fleur")
new_x = event.x_root + xwin
new_y = event.y_root + ywin
self.app.geometry(f'+{new_x}+{new_y}')
self.update_position(new_x, new_y)
def release_window(event):
self.app.config(cursor="arrow")
event.widget.bind('<B1-Motion>', move_window)
event.widget.bind('<ButtonRelease-1>', release_window)
def exit(self, *args, **kwargs):
self.app.destroy()
exit()
if __name__ == "__main__":
class Test1(Widget):
def __init__(self, *args, **kwargs):
super().__init__( *args, **kwargs)
self.close = ttk.Button(self, text="X", command=exit)
self.close.pack(padx=20, pady=20)
Test1().mainloop()