Home > front end >  Tkinter trouble with strings
Tkinter trouble with strings

Time:04-12

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:

  1. you don't need the == True part. any returns True or False and thats enough for the if condition.
  2. i think you have an issue with your list comprehension. w for w in book_database[i] should be encapsulated with square brackets [
  • Related