I am trying to design a basic app which is able to match an input word to words from books in a database. However when I run the following code which is part of a function that is fed into the output of the there are never any matches.
relevant_books = ''
if any(str(input.get('1.0', 'end')) in w for w in book_database[i]) ==True:
relevant_books = book_database[i]
I have tried various different methods of string matching but none of them work. I think its because there is something different about tkinter inputs to regular strings?
CodePudding user response:
The text widget automatically adds a newline to the very end of the data in the widget. When you call input.get('1.0', 'end')
you're going to get a string that includes this trailing newline.
To get all of the data except for this trailing newline use input.get('1.0', 'end-1c')
. The -1c
means to get the data all of the way to the end minus one character.
CodePudding user response:
- you don't need the
== True
part. any returnsTrue
orFalse
and thats enough for theif
condition. - i think you have an issue with your list comprehension.
w for w in book_database[i]
should be encapsulated with square brackets[