File: pyedit-products/unzipped/docetc/examples/Non-BMP-Emojis/try-binary.py
""" ======================================================================================= Binary mode fails too: both emojis and valid BMP symbols display and edit wrongly, and the text fetched from the widget and saved has mangled characters that display ain TextEdit as the same garbage glyphs shoen in Tk. ======================================================================================= """ from tkinter import * from tkinter.filedialog import askopenfilename, asksaveasfilename START = '1.0' def load(): # what PyEdit does on Open fn = askopenfilename() ft = open(fn, mode='rb').read() # it's utf8 text text.delete(START, END) # store text string in widget text.insert(END, ft) # or START; text=bytes or str text.mark_set(INSERT, START) # move insert point to top text.see(INSERT) # scroll to top, insert set text.see(INSERT) # no, really: see note above def save(): # what PyEdit does on Save fn = asksaveasfilename() ft = text.get(START, END+'-1c') # extract text as str string ft = ft.encode('utf8', errors='ignore') fo = open(fn, 'wb') fo.write(ft) fo.close() root = Tk() text = Text(root, relief=RIDGE, borderwidth=2) text.pack() Button(root, text='load', command=load).pack() Button(root, text='save', command=save).pack() root.mainloop()