如何使用非英语文字而不会遇到任何Unicode错误?
我的GUI应用程序中有很多文本标签,这些标签不是英语。因此,我遇到了Unicode错误。
leaveBtn = Button(top_frame_label, text= u"G�rev Y�k�n� Ay�r".decode(errors='replace') , width = 15)
^
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xf6 in position 1: invalid start byte
在我写的第一个代码中,我没有遇到任何错误,直到我使用具有许多具有非英语文字的字符串的数组。因为它是一个数组,然后在使用它们之前,我对它们进行了编码,然后在使用它们时将它们解码。 (我添加了代码)我没有其他任何Unicode问题。
from tkinter import *
root = Tk()
root.state('zoomed')
teleTitle_f_tab2 = ["Takım no: ","Paket no: ","Zaman: ",
"G.Y. Basıncı: ","T. Basıncı: ",
"Nem: ",
"G.Y. Yüksekliği: ","T. Yüksekliği: ",
"İrtifa Farkı: ","İniş Hızı: ","Sıcaklık: ","Pil Gerilimi: ",
"G.Y. Latitude: ","G.Y. Longitude: ","G.Y. Altitude: ",
"T. Latitude: ","T. Longitude: ","T. Altitude: ",
"Durum: ",
"Pitch: ","Roll: ","Yaw: ",
"Dönüş Sayısı: ","Video Aktarım Bilgisi: ", " "]
for i in range(0,25):
teleTitle_f_tab2[i] = teleTitle_f_tab2[i].encode('UTF-8')
data = 0
for col in range(5):
for rw in range(5):
textData = Label(root, text= teleTitle_f_tab2[data].decode(), anchor = "w",width= 28)
textData.grid(row=rw, column=col, padx=5, pady=10)
data = data +1
root.mainloop()
然后,我将代码移至另一个.py文件,因为它们太混乱了。现在我遇到了Unicode错误。首先,我在每个文本之前写过u“”
,但这无济于事。然后,我尝试了.decode(errors ='replact')
在我的第一个文本中具有非英语文字,如上所述。但这也无济于事。而且我也尝试了u“ ascii_text” .encode('utf-8')。decode()
,但它也行不通。每次相同的错误。我现在该怎么办?我在按钮,框架标题或标签中有这样的文字。
编辑:该代码在Python中运行正确,但在Visual Studio中不起作用。
i have so many text label in my gui app that are not english. so I am getting unicode error.
leaveBtn = Button(top_frame_label, text= u"G�rev Y�k�n� Ay�r".decode(errors='replace') , width = 15)
^
SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xf6 in position 1: invalid start byte
in the first code I wrote I didn't get any error until I use an array that has so many strings that have non-english literals. because it is an array before using them I encoded them and then decoded them when I use them. (I added the code) I haven't had any other unicode issues.
from tkinter import *
root = Tk()
root.state('zoomed')
teleTitle_f_tab2 = ["Takım no: ","Paket no: ","Zaman: ",
"G.Y. Basıncı: ","T. Basıncı: ",
"Nem: ",
"G.Y. Yüksekliği: ","T. Yüksekliği: ",
"İrtifa Farkı: ","İniş Hızı: ","Sıcaklık: ","Pil Gerilimi: ",
"G.Y. Latitude: ","G.Y. Longitude: ","G.Y. Altitude: ",
"T. Latitude: ","T. Longitude: ","T. Altitude: ",
"Durum: ",
"Pitch: ","Roll: ","Yaw: ",
"Dönüş Sayısı: ","Video Aktarım Bilgisi: ", " "]
for i in range(0,25):
teleTitle_f_tab2[i] = teleTitle_f_tab2[i].encode('UTF-8')
data = 0
for col in range(5):
for rw in range(5):
textData = Label(root, text= teleTitle_f_tab2[data].decode(), anchor = "w",width= 28)
textData.grid(row=rw, column=col, padx=5, pady=10)
data = data +1
root.mainloop()
then I moved my code to another .py file because they were so messy. and now I'm getting unicode errors. First, I wrote before every text u""
but it couldn't help. then i tried .decode(errors='replace')
in my first text that has non-english literal as you can see above. but it couldn't help too. and also I tried u"ascii_text".encode('UTF-8').decode()
but it doesn't work too. every time same error. what can I do now? I have so much text like this in buttons, frame titles, or labels.
edit: the code ran in python idle properly but doesn't work in visual studio.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我进行了以下测试
并获得以下输出。似乎并不需要编码。
I ran the following test
And got the following output. It does not seem the encode is necessary.
我在搜索时找到了一个解决方案。我希望如果您遇到同样的问题,这会为您提供帮助。该解决方案只是将文件保存为编码:Unicode(UTF-8无签名)。
utf-8
I found a solution while searching. I hope it would help you if you have the same problem. the solution is simply to save your file as an encoding: Unicode (UTF-8 Without signature).
UTF-8 without BOM