【问题标题】:SyntaxError: Non-UTF-8 code starting with '\x92' in file D:\AIAssistant\build\gui.py on line 92, but no encoding declared;SyntaxError:第 92 行文件 D:\AIAssistant\build\gui.py 中以 '\x92' 开头的非 UTF-8 代码,但未声明编码;
【发布时间】:2022-01-25 11:52:00
【问题描述】:

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

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

我正在使用这个脚本:


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()

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

提前致谢!

【问题讨论】:

  • 您是否尝试按照链接中的建议进行操作?声明编码?也无法在 Python 3.8 上重现(虽然它似乎与版本无关,但更多与编码有关,特别是因为文件是自动生成的)
  • 您使用字符(U+2019,右单引号),而\x92是Windows代码页中的代码cp1252,@987654328 @(以及更多其他人)。使用 utf-8 编码保存您的脚本(如果您使用的是已弃用的 Python 版本 2,请使用 # -*- coding: utf-8 -*-)。
  • 这能回答你的问题吗? SyntaxError: Non-UTF-8 code starting with '\x91'
  • 两件事:脚本的整个长度无关紧要,它应该是minimal reproducible example,其中一两行可能就足够了。此外,只需在线搜索错误消息即可。如有类似问题,请先阅读理解!

标签: python tkinter encoding utf-8


【解决方案1】:

Python 3 要求源文件以 UTF-8 编码。如果使用不同的编码,则必须声明为link in the error message 描述。

确保以 UTF-8 保存源文件,或声明编码。在 Windows 10(西欧或美国本地化版本)上,如果不是 UTF-8,该文件可能是 Windows-1252 编码的。在文件顶部添加# coding: cp1252 注释将在这种情况下声明编码,但如果您能弄清楚如何在编辑器中更改编码,最好只使用 UTF-8。

【讨论】:

    猜你喜欢
    • 2022-08-02
    • 1970-01-01
    • 2023-03-29
    • 2018-05-03
    • 2020-11-09
    • 2023-04-01
    • 1970-01-01
    • 2014-05-30
    • 1970-01-01
    相关资源
    最近更新 更多