我正在编写一个程序,如果您将文本放入文本框中,它会在 Google.com 上搜索内容,但会返回错误:
TypeError: cannot concatenate 'str' and 'instance' objects
这是代码:
InputStrings = StringVar()
Entry(root, textvariable = InputStrings).pack()
def OutputText():
OutStrings = InputStrings.get()
b = "https://www.google.it/search?q="
if InputStrings:
b = b + InputStrings
webbrowser.open(b)
root.withdraw()
root.quit()
请您参考如下方法:
错误在行
b = b + InputStrings
由于 InputStrings 是一个 StringVar 对象,而 b 是一个字符串,您不能将它们相加。你可能打算使用
b = b + OutStrings
由于 OutStrings 是您通过 InputStrings.get() 创建的字符串,因此您可以随意将其添加到另一个字符串。在这种情况下,“连接”本质上意味着“字符串添加”。
