我知道这会被标记为重复的帖子,因为有一些关于此错误的问题。我已经完成了这些,但不知道如何解决它。请帮我。这是错误消息。
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "iptrace.py", line 21, in mbt
tkMessageBox.showinfo(tget, tget+" lives in "+jd["city"]+", "+jd["region"]+" "+jd["country"])
TypeError: cannot concatenate 'str' and 'NoneType' objects
以下是代码:
from Tkinter import *
import tkMessageBox
import json
import urllib
import sys
def wmi():
uip = urllib.urlopen("http://www.networksecuritytoolkit.org/nst/tools/ip.php").read()
tkMessageBox.showinfo("Whats my IP", "Your IP is "+uip)
def mbt():
global ew1
tget = ew1.get().strip()
jd = json.load(urllib.urlopen("http://ipinfo.io/"+tget+"/geo"))
if tget == "":
tkMessageBox.showerror(tget, "Type a IP Please")
else:
tkMessageBox.showinfo(tget, tget+" lives in "+jd["city"]+", "+jd["region"]+" "+jd["country"])
if __name__ == "__main__":
root = Tk()
root.title("-|IP2Location|-")
textFrame = Frame(root)
entryLabel = Label(textFrame)
entryLabel["text"] = "IP :"
entryLabel.pack(side=LEFT)
ew1 = Entry(textFrame)
ew1["width"] = 24
ew1.pack(side=LEFT)
textFrame.pack()
bmi = Button(root, text="Whats my IP", command=wmi)
bmi.pack()
bs = Button(root, text="Submit", command=mbt)
bs.pack()
def enterPress(event):
mbt()
root.bind("<Return>", enterPress)
def enterPress(event):
exit()
sys.exit(0)
root.bind("<Escape>", enterPress)
root.mainloop()
答案 0 :(得分:1)
错误,如Traceback中所述,位于:
else:
tkMessageBox.showinfo(tget, tget+" lives in \
"+jd["city"]+", "+jd["region"]+" "+jd["country"])
因此导致问题的原因是您使用+
符号表示两种不同的类型,一种是string
,另一种是NoneType
(即没有值)。
因此,您需要做的是使用您尝试在该语句中连接的str(var)
将预定义变量更改为字符串。只有这样它才会毫无问题地运行。