Movatterモバイル変換


[0]ホーム

URL:


Open In App

Prerequisites : Introduction to tkinter |Find current weather of any city
Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with tkinter outputs the fastest and easiest way to create the GUI applications. Now, it’s upto the imagination or necessity of developer, what he/she want to develop using this toolkit.
To create a tkinter : 
 

  • Importing the module – tkinter
  • Create the main window (container)
  • Add any number of widgets to the main window.
  • Apply the event Trigger on the widgets.


Let’s create a GUI based simple Real-time Weather detection application which can give Weather details of any cities.
Modules required: 
 

tkinterrequestsjson


Below is the implementation : 
 

Python3
# import all functions from the tkinterfromtkinterimport*fromtkinterimportmessagebox# function to find weather details# of any city using openweathermap apideftell_weather():# import required modulesimportrequests,json# enter your api key hereapi_key="Your_API_key"# base_url variable to store urlbase_url="http://api.openweathermap.org/data/2.5/weather"# take a city name from city_field entry boxcity_name=city_field.get()# complete_url variable to store complete url addresscomplete_url=base_url+"appid ="+api_key+"&q ="+city_name# get method of requests module# return response objectresponse=requests.get(complete_url)# json method of response object convert# json format data into python format datax=response.json()# now x contains list of  nested dictionaries# we know dictionary contains key value pair# check the value of "cod" key is equal to "404"# or not if not that means city is found# otherwise city is not foundifx["cod"]!="404":# store the value of "main" key in variable yy=x["main"]# store the value corresponding to the "temp" key of ycurrent_temperature=y["temp"]# store the value corresponding to the "pressure" key of ycurrent_pressure=y["pressure"]# store the value corresponding to the "humidity" key of ycurrent_humidity=y["humidity"]# store the value of "weather" key in variable zz=x["weather"]# store the value corresponding to the "description" key# at the 0th index of zweather_description=z[0]["description"]# insert method inserting the# value in the text entry box.temp_field.insert(15,str(current_temperature)+" Kelvin")atm_field.insert(10,str(current_pressure)+" hPa")humid_field.insert(15,str(current_humidiy)+" %")desc_field.insert(10,str(weather_description))# if city is not foundelse:# message dialog box appear which# shows given Error messagemessagebox.showerror("Error","City Not Found\n""Please enter valid city name")# clear the content of city_field entry boxcity_field.delete(0,END)# Function for clearing the# contents of all text entry boxesdefclear_all():city_field.delete(0,END)temp_field.delete(0,END)atm_field.delete(0,END)humid_field.delete(0,END)desc_field.delete(0,END)# set focus on the city_field entry boxcity_field.focus_set()# Driver codeif__name__=="__main__":# Create a GUI windowroot=Tk()# set the name of tkinter GUI windowroot.title("Gui Application")# Set the background colour of GUI windowroot.configure(background="light green")# Set the configuration of GUI windowroot.geometry("425x175")# Create a Weather Gui Application labelheadlabel=Label(root,text="Weather Gui Application",fg='black',bg='red')# Create a City name : labellabel1=Label(root,text="City name : ",fg='black',bg='dark green')# Create a City name : labellabel2=Label(root,text="Temperature :",fg='black',bg='dark green')# Create a atm pressure : labellabel3=Label(root,text="atm pressure :",fg='black',bg='dark green')# Create a humidity : labellabel4=Label(root,text="humidity :",fg='black',bg='dark green')# Create a description :labellabel5=Label(root,text="description  :",fg='black',bg='dark green')# grid method is used for placing# the widgets at respective positions# in table like structure .headlabel.grid(row=0,column=1)label1.grid(row=1,column=0,sticky="E")label2.grid(row=3,column=0,sticky="E")label3.grid(row=4,column=0,sticky="E")label4.grid(row=5,column=0,sticky="E")label5.grid(row=6,column=0,sticky="E")# Create a text entry box# for filling or typing the information.city_field=Entry(root)temp_field=Entry(root)atm_field=Entry(root)humid_field=Entry(root)desc_field=Entry(root)# grid method is used for placing# the widgets at respective positions# in table like structure .# ipadx keyword argument set width of entry space .city_field.grid(row=1,column=1,ipadx="100")temp_field.grid(row=3,column=1,ipadx="100")atm_field.grid(row=4,column=1,ipadx="100")humid_field.grid(row=5,column=1,ipadx="100")desc_field.grid(row=6,column=1,ipadx="100")# Create a Submit Button and attached# to tell_weather functionbutton1=Button(root,text="Submit",bg="red",fg="black",command=tell_weather)# Create a Clear Button and attached# to clear_all functionbutton2=Button(root,text="Clear",bg="red",fg="black",command=clear_all)# grid method is used for placing# the widgets at respective positions# in table like structure .button1.grid(row=2,column=1)button2.grid(row=7,column=1)# Start the GUIroot.mainloop()

Output : 
 

Output-2


 

Output-3


 


Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp