1.气温查询方法
# -*- coding: utf-8 -*-
import json
import urllib2
import json
import urllib2
#打开存储城市编码和城市的文档
f = open('ChinaWeatherCity.txt')
f = open('ChinaWeatherCity.txt')
# 定义一个字典,用来存储城市和城市编码
citys = {}
str_temp = ''
citys = {}
str_temp = ''
#进行汉字编码的改变
def changeUncode(variable):
variable = variable.decode("utf8")
return variable
#定义方法用于得到城市的Code
def getCity(cityName):
for line in f.readlines():
line = line.strip('\n')
lineCity = line.split('=')
citys[lineCity[1]] = lineCity[0]
return citys.get(changeUncode(cityName))
#定义方法用于得到输入城市的天气情况
def getCityWeather(cityName):
cityCode = getCity(cityName)
print cityCode
try:
if cityCode:
def changeUncode(variable):
variable = variable.decode("utf8")
return variable
#定义方法用于得到城市的Code
def getCity(cityName):
for line in f.readlines():
line = line.strip('\n')
lineCity = line.split('=')
citys[lineCity[1]] = lineCity[0]
return citys.get(changeUncode(cityName))
#定义方法用于得到输入城市的天气情况
def getCityWeather(cityName):
cityCode = getCity(cityName)
print cityCode
try:
if cityCode:
#解析url
content = urllib2.urlopen(url).read()
content = urllib2.urlopen(url).read()
#转为json格式
data = json.loads(content)
data = json.loads(content)
#得到data中weatherinfo信息
result = data['weatherinfo']
result = data['weatherinfo']
#转化格式
str_temp = ('%s=%s~%s')%(result['weather'],result['temp1'],result['temp2'])
return str_temp
else:
print 'Can not Search City!!'
return str_temp
except:
print 'can not find city'
2.Python GUI 程序
str_temp = ('%s=%s~%s')%(result['weather'],result['temp1'],result['temp2'])
return str_temp
else:
print 'Can not Search City!!'
return str_temp
except:
print 'can not find city'
2.Python GUI 程序
# -*- coding: utf-8 -*-
from Tkinter import *
from com.allen.weather.W_Method import changeUncode, getCityWeather
#Button点击事件
def
getTemperator():
entryVar1 = entry1.get()
print changeUncode(entryVar1)
weatherVar = getCityWeather(entryVar1)
print 'start GUI'
#print weatherVar
weatherVars = weatherVar.split('=');
#print weatherVars[0]
#print weatherVars[1]
entryVar2.set(str(weatherVars[0]))
entryVar3.set(str(weatherVars[1]))
#GUI端布局处理
root = Tk()
# 改变root的大小为150x150
root.geometry('150x150+0+0')
print root.pack_slaves()
# 创建三个Label分别使用不同的fill属性
Label(root,text = 'Please input City Name:').pack(fill = Y)
entryVar1 = StringVar()
entry1 = Entry(root,textvariable=entryVar1)
entry1.pack()
Label(root,text = 'City Weather:').pack(fill = BOTH)
entryVar2=StringVar()
entry2 = Entry(root,textvariable=entryVar2)
entry2.pack()
Label(root,text = 'City Temperator:').pack(fill = X)
entryVar3=StringVar()
entry3 = Entry(root,textvariable=entryVar3)
entry3.pack()
Button(root,text='search weather',command=
getTemperator).pack()
print root.pack_slaves()
root.mainloop()