无法连接'str'与'实例'对象
我正在编写一个程序,这个程序可以让你在一个文本框里输入文字,然后它会在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()
1 个回答
6
错误出现在这一行
b = b + InputStrings
因为 InputStrings 是一个 StringVar 对象,而 b 是一个普通的字符串,所以你不能把它们直接加在一起。你可能是想用
b = b + OutStrings
OutStrings 是通过 InputStrings.get() 创建的字符串,所以你可以随意把它和其他字符串加在一起。在这里,“连接”其实就是指“字符串相加”。