python的Tkinter实现IP地址查询

 

之前见一哥们有个不错的查询ip的API,就忍不住做了个小程序,这几天正在看Tkinter,正好试着做成软件,实用性更强一点。于是有了下面的代码:

import urllib2
import json
from Tkinter import *

root = Tk()
num1 = StringVar()
num2 = StringVar()
num3 = StringVar()
num4 = StringVar()
num5 = StringVar()

f1 = Frame(root, height = 50, width = 100)
f1.pack()

Label(f1, text = '    IP:    ').pack(side = LEFT)
entry = Entry(f1)
entry.pack(side = RIGHT)

f2 = Frame(root)
f2.pack()
def ch():
    ip = entry.get()
    url = 'http://ip.taobao.com/service/getIpInfo.php?ip=%s' % ip
    f = urllib2.urlopen(url).read()
    data = json.loads(f)
    num1.set(data['data']['city'])
    num2.set(data['data']['area'])
    num3.set(data['data']['country'])
    num4.set(data['data']['region'])
    num5.set(data['data']['isp'])

button = Button(f2, command = ch, text = 'CLICK')
button.pack()

f3 = Frame(root)
f3.pack()
show1 = Entry(f3, textvariable = num1)
show1.pack()
show2 = Entry(f3, textvariable = num2)
show2.pack()
show3 = Entry(f3, textvariable = num3)
show3.pack()
show4 = Entry(f3, textvariable = num4)
show4.pack()
show5 = Entry(f3, textvariable = num5)
show5.pack()
num1.set('hi')

root.mainloop()

只是实现了基本功能,但还没做任何美化。界面丑的要命。还需要再学习下。

另外,也是简单的面向过程实现,没有面向对象。

posted on 2013-04-09 22:32  alexkh  阅读(854)  评论(0编辑  收藏  举报