新手,一个简短的Pyhton程序,报错。哪位老司机能解决?
有请各位老司机指教!
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)请直接帮修改一下。
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)请直接帮修改一下。
...全文
相关推荐
python扩展需要安装的VC++环境(python2.x 和 python3.x 两个版本)
在安装pyhton扩展时,会报错:unable to find vcvarsall.bat。 是因为扩展使用C/C++编写,缺少编译需要的环境,而python2.x和3.x使用的版本也不一样,此资源
解决python中import文件夹下面py文件报错问题
如下所示:在需要导入的那个文件夹里面新建一个 __init__.py文件,哪怕这个文件是空的文件也可以.补充知识:python中import其他目录下的文件出现问题的解决方法在使用python进行编程
pyhton搭建oracle数据库模块以及连接实例
**安装cx_Oracle库**: - `cx_Oracle`是Python的一个扩展库,用于提供与Oracle数据库交互的功能。
centos7环境下安装python和superset
其次,安装 Pyhton 的包管理工具 pip 和虚拟环境工具 virtualenv。
解决python 虚拟环境删除包无法加载的问题
### 解决Python虚拟环境中删除包后无法加载的问题在日常的Python开发过程中,尤其是在使用Docker容器化的开发环境中,开发者可能会遇到这样的问题:当在Python虚拟环境中删除了一个已安装的库或包之后


