SyntaxError: 文件 D:\AIAssistant\build\gui.py 第 92 行中以 '\x92' 开头的非 UTF-8 代码,但未声明编码;

JKR*_*JKR 2 python encoding tkinter utf-8

所以我正在运行 python GUI 脚本,但它给了我以下错误:

\n

SyntaxError: Non-UTF-8 code starting with \'\\x92\' in file D:\\AIAssistant\\build\\gui.py on line 92, but no encoding declared; see https://python.org/dev/peps/pep-0263/ for details

\n

我正在使用这个脚本:

\n
\nwindow = Tk()\n\nwindow.geometry("1440x1024")\nwindow.configure(bg = "#FFFFFF")\n\n\ncanvas = Canvas(\n    window,\n    bg = "#FFFFFF",\n    height = 1024,\n    width = 1440,\n    bd = 0,\n    highlightthickness = 0,\n    relief = "ridge"\n)\n\ncanvas.place(x = 0, y = 0)\ncanvas.create_rectangle(\n    28.0,\n    22.0,\n    534.0,\n    140.0,\n    fill="#06FF96",\n    outline="")\n\ncanvas.create_rectangle(\n    28.0,\n    367.0,\n    534.0,\n    485.0,\n    fill="#06FF96",\n    outline="")\n\ncanvas.create_rectangle(\n    28.0,\n    702.0,\n    534.0,\n    820.0,\n    fill="#06FF96",\n    outline="")\n\ncanvas.create_rectangle(\n    951.0,\n    165.0,\n    1412.0,\n    283.0,\n    fill="#00C5F1",\n    outline="")\n\ncanvas.create_rectangle(\n    951.0,\n    498.0,\n    1412.0,\n    616.0,\n    fill="#00C5F1",\n    outline="")\n\ncanvas.create_text(\n    55.0,\n    52.0,\n    anchor="nw",\n    text="Hi there! How can I help?",\n    fill="#000000",\n    font=("RobotoCondensed Light", 40 * -1)\n)\n\ncanvas.create_text(\n    53.0,\n    400.0,\n    anchor="nw",\n    text="I\xe2\x80\x99m 3y/o on this March. ",\n    fill="#000000",\n    font=("RobotoCondensed Light", 40 * -1)\n)\n\ncanvas.create_text(\n    55.0,\n    735.0,\n    anchor="nw",\n    text="I\xe2\x80\x99m 3y/o on this March. ",\n    fill="#000000",\n    font=("RobotoCondensed Light", 40 * -1)\n)\n\ncanvas.create_text(\n    971.0,\n    199.0,\n    anchor="nw",\n    text="Open Google",\n    fill="#000000",\n    font=("RobotoCondensed Light", 40 * -1)\n)\n\ncanvas.create_text(\n    979.0,\n    531.0,\n    anchor="nw",\n    text="Open Google",\n    fill="#000000",\n    font=("RobotoCondensed Light", 40 * -1)\n)\n\nentry_image_1 = PhotoImage(\n    file=relative_to_assets("entry_1.png"))\nentry_bg_1 = canvas.create_image(\n    607.0,\n    957.5,\n    image=entry_image_1\n)\nentry_1 = Entry(\n    bd=0,\n    bg="#FFFFFF",\n    highlightthickness=0\n)\nentry_1.place(\n    x=55.0,\n    y=920.0,\n    width=1104.0,\n    height=73.0\n)\n\nbutton_image_1 = PhotoImage(\n    file=relative_to_assets("button_1.png"))\nbutton_1 = Button(\n    image=button_image_1,\n    borderwidth=0,\n    highlightthickness=0,\n    command=lambda: print("button_1 clicked"),\n    relief="flat"\n)\nbutton_1.place(\n    x=1176.0,\n    y=919.0,\n    width=118.0,\n    height=75.0\n)\nwindow.resizable(False, False)\nwindow.mainloop()\n
Run Code Online (Sandbox Code Playgroud)\n

我也搜索了这个问题,但没有找到正确的解决方案。\n最重要的是我正在使用 Python 3.10.1。该文件是由 TKdesigner 生成的,我的操作系统是 Windows 10。

\n

提前致谢!

\n

Har*_*rup 6

按原样使用下面的内容并将其插入到代码的顶部。

# coding: utf8
Run Code Online (Sandbox Code Playgroud)

注意:它必须位于顶部。