'Tkinter trouble with strings
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?
Solution 1:[1]
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.
Solution 2:[2]
- you don't need the
== Truepart. any returnsTrueorFalseand thats enough for theifcondition. - i think you have an issue with your list comprehension.
w for w in book_database[i]should be encapsulated with square brackets[
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bryan Oakley |
| Solution 2 | Elad L. |
