新手,一个简短的Pyhton程序,报错。哪位老司机能解决?

simon_gong 2018-08-10 10:23:52
有请各位老司机指教!

1.程序功能:“根据用户输入的IP地址,自动查询IP所对应的城市和国家。”

2.程序代码:
class FindLocation(object):
def __init__(self):
self.gi = pygeoip.GeoIP("./GeoLiteCity.dat")
self.root = tkinter.Tk()
self.root.title("全球定位IP位置(离线版)")
self.ip_input = tkinter.Entry(self.root, width=30)
self.display_info = tkinter.Listbox(self.root, width=50)
self.result_button = tkinter.Button(self.root, command=self.find_position, text="查询")


def gui_arrange(self):
self.ip_input.pack()
self.display_info.pack()
self.result_button.pack()

def find_position(self):
self.ip_addr = self.ip_input
aim = self.gi.record_by_name(self.ip_addr)
try:
city = aim["city"]
country = aim["country_name"]
region_code = aim["region_code"]
longitude = aim["longitude"]
latitude = aim["latitude"]
except:
pass

the_ip_info = ["所在纬度:"+str(latitude), "所在经度:"+str(longitude), "地域代号:"+str(region_code), "所在城市:"+str(city), "所在国家地区:"+str(country), "需要查询的ip:"+str(self.ip_addr)]

for item in range(10):
self.display_info.insert(0, "")

for item in the_ip_info:
self.display_info.insert(0, item)

return the_ip_info

def main():
FL = FindLocation()
FL.gui_arrange()
tkinter.mainloop()
pass


if __name__ == "__main__":
main()





3.程序报错:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "D:/codeing python3/gdal t.py", line 24, in find_position
aim = self.gi.record_by_name(self.ip_addr)
File "C:\ProgramData\Anaconda3\lib\site-packages\pygeoip\__init__.py", line 561, in record_by_name
addr = self._gethostbyname(hostname)
File "C:\ProgramData\Anaconda3\lib\site-packages\pygeoip\__init__.py", line 394, in _gethostbyname
return socket.gethostbyname(hostname)
TypeError: gethostbyname() argument 1 must be str, bytes or bytearray, not Entry

4.请问老司机2个问题:
(1)出错是什么问题?
(2)请直接帮修改一下。
...全文
188 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈年椰子 2018-08-10
  • 打赏
  • 举报
回复
self.ip_addr = self.ip_input
改为

self.ip_addr = self.ip_input.get()
陈年椰子 2018-08-10
  • 打赏
  • 举报
回复
你要传输入的IP地址(字符串)给 pygeoip , 你原来程序传的是 输入控件对象地址, 是一个LONG值。
simon_gong 2018-08-10
  • 打赏
  • 举报
回复
@陈年椰子,感谢老师傅指点哈哈:)

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧