python3 tkinter Geometry Dash save strings tools

gui picture
#!/usr/bin/python3 import tkinter import tkinter.scrolledtext from base64 import urlsafe_b64decode, urlsafe_b64encode from gzip import compress, decompress dotdotdot=lambda string:string[:40]+'...' if len(string)>43 else string class TextBoxMenu(tkinter.Menu): def __init__(self,textBox): super().__init__(textBox,tearoff=0) self.add_command(label='select all',command=self.menu_selectAll) self.add_command(label='copy',command=self.menu_copy) self.add_command(label='paste',command=self.menu_paste) self.add_command(label='delete',command=self.menu_delete) self.add_command(label='cut',command=self.menu_cut) self.add_command(label='clear',command=self.menu_clear) self.bind('<FocusOut>',self.popDownMenu) self.textBox=textBox textBox.bind('<Button-3>',self.popUpMenu) def popUpMenu(self,event): self.textBox.focus_set() self.tk_popup(event.x_root,event.y_root) self.grab_release() def popDownMenu(self,event): self.unpost() self.grab_release() def menu_selectAll(self): self.textBox.tag_add('sel','1.0','end-1c') def menu_copy(self): try: text=self.textBox.get('sel.first','sel.last') except tkinter.TclError: return if text: self.clipboard_clear() self.clipboard_append(text) def menu_paste(self): try: text=self.clipboard_get() except tkinter.TclError: return if text: try: self.textBox.replace('sel.first','sel.last',text) except tkinter.TclError: self.textBox.insert('insert',text) def menu_delete(self): try: text=self.textBox.delete('sel.first','sel.last') except tkinter.TclError: pass def menu_cut(self): try: text=self.textBox.get('sel.first','sel.last') except tkinter.TclError: return if text: self.clipboard_clear() self.clipboard_append(text) self.textBox.delete('sel.first','sel.last') def menu_clear(self): self.textBox.delete('1.0','end') class MainWindow(tkinter.Tk): def __init__(self): super().__init__(className='geometry dash save strings tools') self.encodedShowHideVar = tkinter.StringVar(self, 'hide') self.encodedShowHideVisible = True self.encodedShowHideBtn = tkinter.Button(self, textvariable=self.encodedShowHideVar, command=self.encodedShowHide) self.encodedShowHideBtn.grid(row=0, column=0, sticky='w') self.headEncodedLabel = tkinter.Label(self, text='encoded:', anchor='w') self.headEncodedLabel.grid(row=0, column=1, sticky='nesw', columnspan=3) self.textEncoded = tkinter.scrolledtext.ScrolledText(self) self.textEncodedMenu=TextBoxMenu(self.textEncoded) self.textEncoded.grid(row=1, column=0, sticky='nesw', columnspan=4) tkinter.Grid.rowconfigure(self, 1, weight=1) self.btnUp = tkinter.Button(self, text='\u2191', command=self.encode) self.btnUp.grid(row=2, column=0, sticky='w') self.btnDown = tkinter.Button(self, text='\u2193', command=self.decode) self.btnDown.grid(row=2, column=1, sticky='w') self.useGzipVar = tkinter.StringVar(self, 'use gzip [no]') self.useGzip = False self.btnGzip = tkinter.Button(self, textvariable=self.useGzipVar, command=self.toggleGzip) self.btnGzip.grid(row=2, column=2, sticky='w') self.statusTextVar = tkinter.StringVar(self, 'N/A') self.statusLabel = tkinter.Label(self, textvariable=self.statusTextVar, anchor='w') self.statusLabel.grid(row=2, column=3, sticky='ew') tkinter.Grid.columnconfigure(self, 3, weight=1) self.decodedShowHideVar = tkinter.StringVar(self, 'hide') self.decodedShowHideVisible = True self.decodedShowHideBtn = tkinter.Button(self, textvariable=self.decodedShowHideVar, command=self.decodedShowHide) self.decodedShowHideBtn.grid(row=3, column=0, sticky='w') self.headDecodedLabel = tkinter.Label(self, text='decoded:', anchor='w') self.headDecodedLabel.grid(row=3, column=1, sticky='nesw', columnspan=3) self.textDecoded = tkinter.scrolledtext.ScrolledText(self) self.textDecodedMenu=TextBoxMenu(self.textDecoded) self.textDecoded.grid(row=4, column=0, sticky='nesw', columnspan=4) tkinter.Grid.rowconfigure(self, 4, weight=1) def encode(self): self.textEncoded.delete('1.0', 'end') try: a = self.textDecoded.get('1.0', 'end-1c').encode() if self.useGzip: a = compress(a) self.textEncoded.insert('end', urlsafe_b64encode(a).decode()) status = 'success' except BaseException as error: status = dotdotdot(repr(error)) self.statusTextVar.set(status) def decode(self): self.textDecoded.delete('1.0', 'end') try: a = urlsafe_b64decode(self.textEncoded.get('1.0', 'end-1c').encode()) if self.useGzip: a = decompress(a) self.textDecoded.insert('end', a.decode()) status = 'success' except BaseException as error: status = dotdotdot(repr(error)) self.statusTextVar.set(status) def showHide(self, visible: bool, visibleVar: tkinter.StringVar, widget: tkinter.Widget, row: int): if (visible): widget.grid_remove() tkinter.Grid.rowconfigure(self, row, weight=0) visibleVar.set('show') return False widget.grid() tkinter.Grid.rowconfigure(self, row, weight=1) visibleVar.set('hide') return True def encodedShowHide(self): self.textEncodedMenu.popDownMenu(None) self.encodedShowHideVisible = self.showHide(self.encodedShowHideVisible, self.encodedShowHideVar, self.textEncoded, 1) def decodedShowHide(self): self.textDecodedMenu.popDownMenu(None) self.decodedShowHideVisible = self.showHide(self.decodedShowHideVisible, self.decodedShowHideVar, self.textDecoded, 4) def toggleGzip(self): if self.useGzip: self.useGzipVar.set('use gzip [no]') self.useGzip = False else: self.useGzipVar.set('use gzip [yes]') self.useGzip = True MainWindow().mainloop()

No comments:

Post a Comment