So I'm running a python GUI script, but it gives me the following error:
SyntaxError: Non-UTF-8 code starting with '\x92' in file D:\AIAssistant\build\gui.py on line 92, but no encoding declared; see for details
And I'm using this script:
window = Tk()
window.geometry("1440x1024")
window.configure(bg = "#FFFFFF")
canvas = Canvas( window, bg = "#FFFFFF", height = 1024, width = 1440, bd = 0, highlightthickness = 0, relief = "ridge"
)
canvas.place(x = 0, y = 0)
canvas.create_rectangle( 28.0, 22.0, 534.0, 140.0, fill="#06FF96", outline="")
canvas.create_rectangle( 28.0, 367.0, 534.0, 485.0, fill="#06FF96", outline="")
canvas.create_rectangle( 28.0, 702.0, 534.0, 820.0, fill="#06FF96", outline="")
canvas.create_rectangle( 951.0, 165.0, 1412.0, 283.0, fill="#00C5F1", outline="")
canvas.create_rectangle( 951.0, 498.0, 1412.0, 616.0, fill="#00C5F1", outline="")
canvas.create_text( 55.0, 52.0, anchor="nw", text="Hi there! How can I help?", fill="#000000", font=("RobotoCondensed Light", 40 * -1)
)
canvas.create_text( 53.0, 400.0, anchor="nw", text="I’m 3y/o on this March. ", fill="#000000", font=("RobotoCondensed Light", 40 * -1)
)
canvas.create_text( 55.0, 735.0, anchor="nw", text="I’m 3y/o on this March. ", fill="#000000", font=("RobotoCondensed Light", 40 * -1)
)
canvas.create_text( 971.0, 199.0, anchor="nw", text="Open Google", fill="#000000", font=("RobotoCondensed Light", 40 * -1)
)
canvas.create_text( 979.0, 531.0, anchor="nw", text="Open Google", fill="#000000", font=("RobotoCondensed Light", 40 * -1)
)
entry_image_1 = PhotoImage( file=relative_to_assets("entry_1.png"))
entry_bg_1 = canvas.create_image( 607.0, 957.5, image=entry_image_1
)
entry_1 = Entry( bd=0, bg="#FFFFFF", highlightthickness=0
)
entry_1.place( x=55.0, y=920.0, width=1104.0, height=73.0
)
button_image_1 = PhotoImage( file=relative_to_assets("button_1.png"))
button_1 = Button( image=button_image_1, borderwidth=0, highlightthickness=0, command=lambda: print("button_1 clicked"), relief="flat"
)
button_1.place( x=1176.0, y=919.0, width=118.0, height=75.0
)
window.resizable(False, False)
window.mainloop()I searched for this question too but didn't find the right solution. And the most important thing is I'm using Python 3.10.1. The file was generated by TKdesigner and my os system is Windows 10.
Thanks in advance!
43 Answers
Use the below as it is and insert it on top of the code.
# coding: utf8NOTE: It must be at the top.
1Python 3 expects source files to be encoded in UTF-8. If a different encoding is used, it must be declared as the link in the error message describes.
Make sure to save the source file in UTF-8, or declare the encoding. On Windows 10 (Western European or US localized version), the file is likely Windows-1252-encoded if not UTF-8. Adding a # coding: cp1252 comment at the top of the file will declare the encoding in this case, but it's better to just use UTF-8 if you can figure out how to change the encoding in your editor.
Adding this as tee first line worked for me : # coding: cp1252
1