搜索中的 Tkinter 非 ascii 错误
Tkinter non-ascii error in search
我正在尝试在 Tkinter 中实现搜索。在 list_box 元素(填充列表框的列表变量)中没有非 ascii 字符的情况下一切正常。但是,在搜索中输入非 ascii 字符 returns 错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "ex2_af.py", line 20, in <lambda>
self.search_var.trace("w", lambda name, index, mode: self.update_list())
File "ex2_af.py", line 41, in update_list
if search_term.lower() in item.lower():
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: ordinal not in range(128)
代码如下:
# -*- coding: utf-8 -*-
from Tkinter import *
# First create application class
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.create_widgets()
# Create main GUI window
def create_widgets(self):
self.search_var = StringVar()
self.search_var.trace("w", lambda name, index, mode: self.update_list())
self.entry = Entry(self, textvariable=self.search_var, width=13)
self.lbox = Listbox(self, width=45, height=15)
self.entry.grid(row=0, column=0, padx=10, pady=3)
self.lbox.grid(row=1, column=0, padx=10, pady=3)
# Function for updating the list/doing the search.
# It needs to be called here to populate the listbox.
self.update_list()
def update_list(self):
search_term = self.search_var.get()
# Last element, 'Čelo' represents non-ascii part.
# You may add any non-ascii to see what I'm talking about.
# How to access this element?
lbox_list = ['Adam', 'Lucy', 'Barry', 'Bob',
'James', 'Frank', 'Susan', 'Amanda', 'Christie', 'Čelo']
self.lbox.delete(0, END)
for item in lbox_list:
if search_term.lower() in item.lower():
self.lbox.insert(END, item)
root = Tk()
app = Application(master=root)
app.mainloop()
比较 str
(lbox_list
的项目)和 unicode
(用户输入)隐式尝试使用默认值将 str
对象转换为 unicode
编码(ascii,除非另外配置)。
>>> u'Čelo' in 'Čelo'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)
>>> u'Čelo' in u'Čelo'
True
您可以通过将列表定义为 unicode 列表来避免这种隐式解码来解决您的问题:
lbox_list = [
u'Adam', u'Lucy', u'Barry', u'Bob',
u'James', u'Frank', u'Susan', u'Amanda', u'Christie',
u'Čelo'
]
我正在尝试在 Tkinter 中实现搜索。在 list_box 元素(填充列表框的列表变量)中没有非 ascii 字符的情况下一切正常。但是,在搜索中输入非 ascii 字符 returns 错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "ex2_af.py", line 20, in <lambda>
self.search_var.trace("w", lambda name, index, mode: self.update_list())
File "ex2_af.py", line 41, in update_list
if search_term.lower() in item.lower():
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 0: ordinal not in range(128)
代码如下:
# -*- coding: utf-8 -*-
from Tkinter import *
# First create application class
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.create_widgets()
# Create main GUI window
def create_widgets(self):
self.search_var = StringVar()
self.search_var.trace("w", lambda name, index, mode: self.update_list())
self.entry = Entry(self, textvariable=self.search_var, width=13)
self.lbox = Listbox(self, width=45, height=15)
self.entry.grid(row=0, column=0, padx=10, pady=3)
self.lbox.grid(row=1, column=0, padx=10, pady=3)
# Function for updating the list/doing the search.
# It needs to be called here to populate the listbox.
self.update_list()
def update_list(self):
search_term = self.search_var.get()
# Last element, 'Čelo' represents non-ascii part.
# You may add any non-ascii to see what I'm talking about.
# How to access this element?
lbox_list = ['Adam', 'Lucy', 'Barry', 'Bob',
'James', 'Frank', 'Susan', 'Amanda', 'Christie', 'Čelo']
self.lbox.delete(0, END)
for item in lbox_list:
if search_term.lower() in item.lower():
self.lbox.insert(END, item)
root = Tk()
app = Application(master=root)
app.mainloop()
比较 str
(lbox_list
的项目)和 unicode
(用户输入)隐式尝试使用默认值将 str
对象转换为 unicode
编码(ascii,除非另外配置)。
>>> u'Čelo' in 'Čelo'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position 0: ordinal not in range(128)
>>> u'Čelo' in u'Čelo'
True
您可以通过将列表定义为 unicode 列表来避免这种隐式解码来解决您的问题:
lbox_list = [
u'Adam', u'Lucy', u'Barry', u'Bob',
u'James', u'Frank', u'Susan', u'Amanda', u'Christie',
u'Čelo'
]