该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
1.程序功能:“根据用户输入的IP地址,自动查询IP所对应的城市和国家”
2.原代码:
/usr/bin/python3
import tkinter
import pygeoip
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_arrang(self):
self.ip_input.pack()
self.display_info.pack()
self.result_button.pack()
#根据ip查找地理位置
def find_position(self):
#获取输入信息
self.ip_addr = self.ip_input.get()
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.addr)]
#清空回显列表可见部分,类似clear命令
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_arrang()
#主程序执行
tkinter.mainloop()
pass
if __name__== "__main__":
main()
代码报错:
Traceback (most recent call last):
File "D:/dear/internet5.py", line 62, in
main()
File "D:/dear/internet5.py", line 55, in main
FL = FindLocation()
File "D:/dear/internet5.py", line 6, in __init__
self.gi = pygeoip.GeoIP("./GeoLiteCity.dat")
File "D:\dear\venv\lib\site-packages\pygeoip\__init__.py", line 118, in __init__
self._fp = codecs.open(filename, 'rb', ENCODING)
File "D:\Program Files\Python\Python37\lib\codecs.py", line 898, in open
file = builtins.open(filename, mode, buffering)
FileNotFoundError: [Errno 2] No such file or directory: './GeoLiteCity.dat'
我是一个新手,请大神看看具体是什么问题,谢谢了!