Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Tkinter non-ascii error while using search

I'm trying to implement non-ascii in my search. Everything works fine without non-ascii characters inside generic list (lbox_list). Since teamtreehouse.com does not parse non-ascii in a good manner, add your non-ascii and you will see what I am talking about. Here is the code:

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

        # Just a generic list to populate the listbox
        #Everything is searchable except the last term.
        #Add your non-ascii and you will se what's the thing.

        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()
root.title('Filter Listbox Test')
app = Application(master=root)
print 'Starting mainloop()'
app.mainloop()

2 Answers

Kenneth Love (and anybody who is reading this), you may see the solution here: http://stackoverflow.com/questions/27753064/tkinter-non-ascii-error-in-search

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I had no problems running this on Python 3.4 (after changing Tkinter to tkinter and fixing the print statement).

What error are you getting?