Use Python to get the current weather conditions in each city

Source: Internet
Author: User

This project, made for several days, mainly in the Tkinter GUI interface has been stuck, in the online material is not much, and finally directly give up a little more complex things, directly to do a simple point of the interface.

Program function:

1, can query the weather situation and display time of different cities, every 60 seconds to refresh the weather situation,

2, you can freely choose the city, choose to get the weather of the city immediately after

Key code
# _*_ Coding:utf-8 _*_import requestsimport timedef weather_log (stu): #获取实时天气情况写入到文本 cu_time=time.strftime ("%Y-%m-%d %h:%m:%s ", Time.localtime ()) Fp=open (' Weather.log ', ' a ') Fp.write (' {} {} '. Format (cu_time,stu)) Fp.close () #把文本中城市 The relationship to the city ID one by one is stored in the DIC dictionary f=open (' cityid.txt ', ' r ') Dic={}for line in F:v=line.strip (). Split (', ') dic[v[1]]=v[0]f.close () d EF update_weather: #通过小米天气API获取天气状况 if City in Dic:cityid=dic[city] Temp=requests.get ("Http://wea Therapi.market.xiaomi.com/wtr-v2/temp/realtime?cityid= "+cityid) temp.encoding= ' Utf-8 ' Tem=temp.json () [' Weath Erinfo ' [' temp '] sd=temp.json () [' Weatherinfo '] [' SD '] w=temp.json () [' Weatherinfo '] [' WD ']+temp.json () [' Weather        Info ' [' WS '] weather=temp.json () [' Weatherinfo '] [' weather '] update_time=temp.json () [' Weatherinfo '] [' time '] Stu= ' {0} temp at the moment: {1} {2}    {3} Weather update time: {4}\n '. Format (city,tem,weather,w,update_time) Weather_log (stu) else:print ("Please make sure the city is correct")Return (Tem,sd,w,weather) 

These are just key codes, and the full code and Cityid files can be downloaded in the links below.
Cityid file formats such as:

The key program is very simple, is through Cityid this file to generate Cityid and city name corresponding dictionary, and then through the Millet weather API to get the city's weather information, the requested address is returned in JSON format data, so directly with the requests library JSON method access can be, There is no need to use the standard library's JSON library.

Thinking and summarizing
    • The project more pit is in the use of Tkinter, really tkinter from the introduction to give up. But also understand some of Tkinter's operating mechanisms, understand the tkinter of the three ways of layout, pack (), place (), grid (), this starring the use of place ().
    • Multi-threaded Python is used to control the main thread of the window, the child threads that control the time, and the child threads that control the weather updates.
    • Learn to use the Sleep.time () interval to update the weather conditions.
    • However, the main idea of the small project is the process-oriented, the code to think of where to write, there is no reasonable planning.
    • For object-oriented programming is still very unfamiliar, the next thing to learn in this area. Source of this project: http://down.51cto.com/data/2444907

Use Python to get the current weather conditions in each city

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.