7

I have a program using GUI elements and returns the error

cannot concatenate 'str' and 'instance' objects

The Code is:

def PeopleSearch():
    query = SearchTerm
    query = ('what is '+ query)
    string = ("<center><font size = 14> " + query + ' </font></center><br><img src =picture')
    j = 0
    try:
        gs = GoogleSearch(query)
        gs.results_per_page = 100
        results = gs.get_results()

The indentations have been changed. Hmm. SearchTerm is basically from a textbox.

Thomas Fenzl
  • 4,342
  • 1
  • 17
  • 25
Moushumi Mitra
  • 111
  • 2
  • 3
  • 6
  • 5
    Try : `('what is '+ str(query))` – Ashwini Chaudhary Jun 30 '13 at 16:15
  • You can otherwise do an ugly hack and place `print dir(query)` just beneath `query = SearchTerm` that wioll give you all the attributes of the query instance. Then you might see which attribute you should use. – Niclas Nilsson Jun 30 '13 at 16:22
  • 1
    When you say "SearchTerm is basically from a textbox", what does that mean? It's obviously not just the text from a text widget or you wouldn't have this problem. And tkinter doesn't have a "textbox" widget; it has a text widget and an entry widget, and text objects on a canvas. – Bryan Oakley Jun 30 '13 at 16:51

1 Answers1

9

Let me reproduce with a simpler example:

v = 42
query = ('what is ' + v)

You would get:

TypeError: cannot concatenate 'str' and 'int' objects

But now, if you simply call str:

query = ('what is ' + str(v))

That will work. So you only have to make sure str(query) returns what you expect. Be careful, I don't know what kind of object you are manipulating, but you should check if there is any method to get the string representation of it.

Related:

Community
  • 1
  • 1
Maxime Chéramy
  • 17,761
  • 8
  • 54
  • 75
  • please I have same problem but when I use str function I get this error: `"need more than 1 value to unpack" ` – Souad Jul 04 '18 at 09:21
  • @Somar I can't help without code. Ask the question on stackoverflow and send me the link and I'll answer it. – Maxime Chéramy Jul 06 '18 at 08:09