I have a sample of my program:
import re
import tkinter as T
_nonbmp = re.compile(r'[\U00010000-\U0010FFFF]')
def _surrogatepair(match):
char = match.group()
assert ord(char) > 0xffff
encoded = char.encode('utf-16-le')
return (
chr(int.from_bytes(encoded[:2], 'little')) +
chr(int.from_bytes(encoded[2:], 'little')))
def createSurrogate(data):
return _nonbmp.sub(_surrogatepair, data)
def server_send(event=None):
data = entry_field.get()
server.send(data, False)
entry_text.set("")
def insertEmoji(code):
entry_field.insert("insert", code)
def emoji():
win = T.Tk()
win.title("Emojis")
emoji1 = T.Button(win, text=createSurrogate("\U0001F602"), command=lambda: insertEmoji("\U0001F602"))
emoji1.pack()
win.mainloop()
main = T.Tk()
main.title("SDT_Client")
entry_text = T.StringVar()
entry_text.set("")
messages_frame = T.Frame(main)
scrollbar = T.Scrollbar(messages_frame)
msg_list = T.Listbox(messages_frame, height=15, width=100, yscrollcommand=scrollbar.set)
scrollbar.pack(side=T.RIGHT, fill=T.Y)
msg_list.pack(side=T.LEFT, fill=T.BOTH)
msg_list.pack()
messages_frame.pack()
entry_field = T.Entry(main, textvariable=entry_text)
entry_field.bind("<Return>", server_send)
entry_field.pack()
send_button = T.Button(main, text="Send", command=server_send)
send_button.pack()
emojiButton = T.Button(main, text="Emojis", command=emoji)
emojiButton.pack()
To summarize i have an entry with a send button underneath and an emoji button further down. When the send button is clicked the contents of the entry is sent to a server. When the emoji button is clicked a new window is open with more buttons with emojis on display. When one of those buttons is clicked the emoji is inserted into the entry field. But when i try to send a message including an emoji i get the following error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\....\nClient.py", line 36, in server_send
data = entry_field.get()
File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\tkinter\__init__.py", line 2682, in get
return self.tk.call(self._w, 'get')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 0: invalid continuation byte
I am aware that the problem is occurring in the line:
data = entry_field.get()
But i am unable to think of a solution.