class GBTranslator(Frame): def __init__(self, master): super().__init__(master) self.textl = TweakableText(self, height=10, width=40) self.textl.pack(side='left', fill='both', padx=5, pady=5) self.textr = TweakableText(self, height=10, width=40) self.textr.pack(side='right', fill='both', padx=5, pady=5) frame = Frame(self) frame.pack(fill='both') Button( frame, text='=文本解码为GB2312=>', command=self.encode_gb).pack( side='top', pady=5, fill='x') Button( frame, text='<=GB2312编码为文本=', command=self.decode_gb).pack( side='top', pady=(0, 5), fill='x') def encode_gb(self): raw = self.textl.get('0.0', 'end').split('\n')[:-1] output = [str(line.encode('GB2312')) if line else '' for line in raw] self.textr.set_content('\n'.join(output)) valid_b_seq = re.compile(r"""^(b"[^"]+")|(b'[^']+')""") def decode_gb(self): raw = self.textr.get('0.0', 'end').split('\n')[:-1] output = [] for line in raw: if self.valid_b_seq.match(line): try: output.append(eval(line).decode('GB2312')) except: output.append('') else: output.append('') self.textl.set_content('\n'.join(output))
def __init__(self, master): super().__init__(master) frame = Frame(self) frame.pack(side='bottom', fill='both') Button(frame, text='=文本解码为GB2312=>', command=self.encode_gb).pack(side='top', pady=5, fill='x') Button(frame, text='<=GB2312编码为文本=', command=self.decode_gb).pack(side='top', pady=(0, 5), fill='x') self.textl = TweakableText(self, height=10, width=40) self.textl.pack(side='left', fill='both', padx=5, pady=5) self.textr = TweakableText(self, height=10, width=40) self.textr.pack(side='right', fill='both', padx=5, pady=5)
def patched_insert(text_widget, index, chars, *args): wb = get_workbench() if not text_widget.is_read_only(): publish_insert(wb, text_widget, text_widget.index(tk.INSERT), index, chars) TweakableText.insert(text_widget, index, chars, *args)
def patched_delete(text_widget, index1, index2=None): wb = get_workbench() if not text_widget.is_read_only(): publish_delete(wb, text_widget, text_widget.index(tk.INSERT), index1, index2) TweakableText.delete(text_widget, index1, index2)