【发布时间】: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 -*-)。 -
两件事:脚本的整个长度无关紧要,它应该是minimal reproducible example,其中一两行可能就足够了。此外,只需在线搜索错误消息即可。如有类似问题,请先阅读理解!
标签: python tkinter encoding utf-8