我正在开发一个Tkinter应用程序,用户可以在点击一个按钮之前输入几个字段,这个按钮应该可以写他在TreeView小部件中写的东西。在mac上运行时效果很好,但在Windows上,在TreeView上调用insert()时内置.UnicodeDecodeError:'utf-8'编解码器无法解码2570位的字节0xe9:无效的连续字节”。你知道吗
我试图对insert方法中传递的字符串调用encode(“utf-8”),但这不起作用。你知道吗
for i in range(0, len(self.commandList)):
resumeTree.insert("", i, "", values = (self.commandList[i][0].encode("utf-8"), self.commandList[i][1].encode("utf-8"), self.commandList[i][2].encode("utf-8")), text = i)
我的输入可能包含特殊字符,但错误与我尝试时相同:
for i in range(0, len(self.commandList)):
resumeTree.insert("", i, "", values = ("a", "b", "c"), text = i)
以下是我的小部件的定义:
resumeTree = ttk.Treeview(resumeFrame)
resumeTree['show'] = 'headings'
resumeTree["columns"]=("type","poids", "prix")
resumeTree.column("type", minwidth = 15, width = 200)
resumeTree.column("poids", minwidth = 75, width = 50)
resumeTree.column("prix", minwidth = 75, width = 50)
resumeTree.heading("type", text = "Type", anchor = tk.W)
resumeTree.heading("poids", text = "Poids (g)", anchor = tk.W)
resumeTree.heading("prix", text = "Prix total (euro)", anchor = tk.W)
resumeTree.Scrollable = True
这是错误的回溯
File "C:\Users\Camille\Documents\GestionVentesClassique\main.py", line 165, in <module>
File "C:\Users\Camille\Documents\GestionVentesClassique\main.py", line 162, in main
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\tkinter\__init__.py", line 1283, in mainloop
self.tk.mainloop(n)
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\tkinter\__init__.py", line 1709, in __call__
self.widget._report_exception()
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\tkinter\__init__.py", line 1452, in _report_exception
root.report_callback_exception(exc, val, tb)
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\tkinter\__init__.py", line 2098, in report_callback_exception
traceback.print_exception(exc, val, tb)
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\traceback.py", line 104, in print_exception
type(value), value, tb, limit=limit).format(chain=chain):
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\traceback.py", line 508, in __init__
capture_locals=capture_locals)
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\traceback.py", line 363, in extract
f.line
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\traceback.py", line 285, in line
self._line = linecache.getline(self.filename, self.lineno).strip()
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\linecache.py", line 16, in getline
lines = getlines(filename, module_globals)
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\linecache.py", line 47, in getlines
return updatecache(filename, module_globals)
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\linecache.py", line 137, in updatecache
lines = fp.readlines()
File "C:\Users\Camille\AppData\Local\Programs\Python\Python37-32\Lib\codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
builtins.UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 2582: invalid continuation byte
python3将字符串作为文本处理,而tkinter将为您提供解码的文本字符串并接收解码的文本字符串,无论其中包含什么字符,您不必担心tkinter小部件之间的编码/解码。你知道吗
因此,在传递给任何tkinter调用或设置之前对字符串进行编码或解码的任何尝试都是错误的-只需保留文本。你知道吗
也就是说,我创建了这样一个小部件,并且确实可以向它注入重音字符。但是,如果在您放置时发送一个固定的三阶参数(“iid”),则会出现错误-
resumeTree.insert("", i, "", ...)
第二个空字符串(""
)将触发TclError
异常(而不是UnicodeDecode
)-the docs say this valus is optional, but that it should be unique for the widget如果我根本不通过它,小部件就可以正常构建。你知道吗
当tkitner试图在Windows(我在Linux上)中显示原始错误消息时,发生的可能是tkitner本身的一个错误。将“iid”参数保留在调用之外(或为其使用唯一值),它应该可以工作:
当然,删除任何对文本值进行“解码”或“编码”的尝试—如果要将数据输出到文本文件,则应添加“编码”参数,然后在调用
open(...)
时使用它。如果您的数据被保存到数据库、呈现为html或其他您不必担心的内容,库将选择最佳的默认编码。你知道吗相关问题 更多 >
编程相关推荐