1 #!/usr/bin/python3
2
3 import tkinter
4 import tkinter.scrolledtext
5 from base64 import urlsafe_b64decode, urlsafe_b64encode
6 from gzip import compress, decompress
7
8 dotdotdot=lambda string:string[:40]+'...' if len(string)>43 else string
9
10 class TextBoxMenu(tkinter.Menu):
11 def __init__(self,textBox):
12 super().__init__(textBox,tearoff=0)
13 self.add_command(label='select all',command=self.menu_selectAll)
14 self.add_command(label='copy',command=self.menu_copy)
15 self.add_command(label='paste',command=self.menu_paste)
16 self.add_command(label='delete',command=self.menu_delete)
17 self.add_command(label='cut',command=self.menu_cut)
18 self.add_command(label='clear',command=self.menu_clear)
19 self.bind('<FocusOut>',self.popDownMenu)
20 self.textBox=textBox
21 textBox.bind('<Button-3>',self.popUpMenu)
22
23 def popUpMenu(self,event):
24 self.textBox.focus_set()
25 self.tk_popup(event.x_root,event.y_root)
26 self.grab_release()
27
28 def popDownMenu(self,event):
29 self.unpost()
30 self.grab_release()
31
32 def menu_selectAll(self):
33 self.textBox.tag_add('sel','1.0','end-1c')
34
35 def menu_copy(self):
36 try:
37 text=self.textBox.get('sel.first','sel.last')
38 except tkinter.TclError:
39 return
40 if text:
41 self.clipboard_clear()
42 self.clipboard_append(text)
43
44 def menu_paste(self):
45 try:
46 text=self.clipboard_get()
47 except tkinter.TclError:
48 return
49 if text:
50 try:
51 self.textBox.replace('sel.first','sel.last',text)
52 except tkinter.TclError:
53 self.textBox.insert('insert',text)
54
55 def menu_delete(self):
56 try:
57 text=self.textBox.delete('sel.first','sel.last')
58 except tkinter.TclError:
59 pass
60
61 def menu_cut(self):
62 try:
63 text=self.textBox.get('sel.first','sel.last')
64 except tkinter.TclError:
65 return
66 if text:
67 self.clipboard_clear()
68 self.clipboard_append(text)
69 self.textBox.delete('sel.first','sel.last')
70
71 def menu_clear(self):
72 self.textBox.delete('1.0','end')
73
74 class MainWindow(tkinter.Tk):
75 def __init__(self):
76 super().__init__(className='geometry dash save strings tools')
77
78 self.encodedShowHideVar = tkinter.StringVar(self, 'hide')
79 self.encodedShowHideVisible = True
80 self.encodedShowHideBtn = tkinter.Button(self, textvariable=self.encodedShowHideVar, command=self.encodedShowHide)
81 self.encodedShowHideBtn.grid(row=0, column=0, sticky='w')
82
83 self.headEncodedLabel = tkinter.Label(self, text='encoded:', anchor='w')
84 self.headEncodedLabel.grid(row=0, column=1, sticky='nesw', columnspan=3)
85
86 self.textEncoded = tkinter.scrolledtext.ScrolledText(self)
87 self.textEncodedMenu=TextBoxMenu(self.textEncoded)
88 self.textEncoded.grid(row=1, column=0, sticky='nesw', columnspan=4)
89 tkinter.Grid.rowconfigure(self, 1, weight=1)
90
91 self.btnUp = tkinter.Button(self, text='\u2191', command=self.encode)
92 self.btnUp.grid(row=2, column=0, sticky='w')
93
94 self.btnDown = tkinter.Button(self, text='\u2193', command=self.decode)
95 self.btnDown.grid(row=2, column=1, sticky='w')
96
97 self.useGzipVar = tkinter.StringVar(self, 'use gzip [no]')
98 self.useGzip = False
99 self.btnGzip = tkinter.Button(self, textvariable=self.useGzipVar, command=self.toggleGzip)
100 self.btnGzip.grid(row=2, column=2, sticky='w')
101
102 self.statusTextVar = tkinter.StringVar(self, 'N/A')
103 self.statusLabel = tkinter.Label(self, textvariable=self.statusTextVar, anchor='w')
104 self.statusLabel.grid(row=2, column=3, sticky='ew')
105 tkinter.Grid.columnconfigure(self, 3, weight=1)
106
107 self.decodedShowHideVar = tkinter.StringVar(self, 'hide')
108 self.decodedShowHideVisible = True
109 self.decodedShowHideBtn = tkinter.Button(self, textvariable=self.decodedShowHideVar, command=self.decodedShowHide)
110 self.decodedShowHideBtn.grid(row=3, column=0, sticky='w')
111
112 self.headDecodedLabel = tkinter.Label(self, text='decoded:', anchor='w')
113 self.headDecodedLabel.grid(row=3, column=1, sticky='nesw', columnspan=3)
114
115 self.textDecoded = tkinter.scrolledtext.ScrolledText(self)
116 self.textDecodedMenu=TextBoxMenu(self.textDecoded)
117 self.textDecoded.grid(row=4, column=0, sticky='nesw', columnspan=4)
118 tkinter.Grid.rowconfigure(self, 4, weight=1)
119
120 def encode(self):
121 self.textEncoded.delete('1.0', 'end')
122 try:
123 a = self.textDecoded.get('1.0', 'end-1c').encode()
124 if self.useGzip:
125 a = compress(a)
126 self.textEncoded.insert('end', urlsafe_b64encode(a).decode())
127 status = 'success'
128 except BaseException as error:
129 status = dotdotdot(repr(error))
130 self.statusTextVar.set(status)
131
132 def decode(self):
133 self.textDecoded.delete('1.0', 'end')
134 try:
135 a = urlsafe_b64decode(self.textEncoded.get('1.0', 'end-1c').encode())
136 if self.useGzip:
137 a = decompress(a)
138 self.textDecoded.insert('end', a.decode())
139 status = 'success'
140 except BaseException as error:
141 status = dotdotdot(repr(error))
142 self.statusTextVar.set(status)
143
144 def showHide(self, visible: bool, visibleVar: tkinter.StringVar, widget: tkinter.Widget, row: int):
145 if (visible):
146 widget.grid_remove()
147 tkinter.Grid.rowconfigure(self, row, weight=0)
148 visibleVar.set('show')
149 return False
150 widget.grid()
151 tkinter.Grid.rowconfigure(self, row, weight=1)
152 visibleVar.set('hide')
153 return True
154
155 def encodedShowHide(self):
156 self.textEncodedMenu.popDownMenu(None)
157 self.encodedShowHideVisible = self.showHide(self.encodedShowHideVisible, self.encodedShowHideVar, self.textEncoded, 1)
158
159 def decodedShowHide(self):
160 self.textDecodedMenu.popDownMenu(None)
161 self.decodedShowHideVisible = self.showHide(self.decodedShowHideVisible, self.decodedShowHideVar, self.textDecoded, 4)
162
163 def toggleGzip(self):
164 if self.useGzip:
165 self.useGzipVar.set('use gzip [no]')
166 self.useGzip = False
167 else:
168 self.useGzipVar.set('use gzip [yes]')
169 self.useGzip = True
170
171 MainWindow().mainloop()
python3 tkinter Geometry Dash save strings tools
Subscribe to:
Posts (Atom)
No comments:
Post a Comment