fork download
  1. # import all functions from the tkinter
  2. from tkinter import * from tkinter import messagebox
  3.  
  4. # function to find weather details
  5. # of any city using openweathermap api
  6. def tell_weather() :
  7.  
  8. # import required modules
  9. import requests, json
  10.  
  11. # enter your api key here
  12. api_key = "Your_API_key"
  13.  
  14. # base_url variable to store url
  15. base_url = "http://a...content-available-to-author-only...p.org/data/2.5/weather?"
  16.  
  17.  
  18. # take a city name from city_field entry box
  19. city_name = city_field.get()
  20.  
  21. # complete_url variable to store complete url address
  22. complete_url = base_url + "appid =" + api_key
  23. + "&q =" + city_name
  24.  
  25. # get method of requests module
  26. # return response object
  27. response = requests.get(complete_url)
  28.  
  29. # json method of response object convert
  30. # json format data into python format data
  31. x = response.json()
  32.  
  33. # now x contains list of nested dictionaries
  34. # we know dictionary contains key value pair
  35. # check the value of "cod" key is equal to "404"
  36. # or not if not that means city is found
  37. # otherwise city is not found
  38. if x["cod"] != "404" :
  39.  
  40. # store the value of "main" key in variable y
  41. y = x["main"]
  42.  
  43. # store the value corresponding to the "temp" key of y
  44. current_temperature = y["temp"]
  45.  
  46. # store the value corresponding to the "pressure" key of y
  47. current_pressure = y["pressure"]
  48.  
  49. # store the value corresponding to the "humidity" key of y
  50. current_humidiy = y["humidity"]
  51.  
  52. # store the value of "weather" key in variable z
  53. z = x["weather"]
  54.  
  55. # store the value corresponding to the "description" key
  56. # at the 0th index of z
  57. weather_description = z[0]["description"]
  58.  
  59. # insert method inserting the
  60. # value in the text entry box.
  61. temp_field.insert(15, str(current_temperature) + " Kelvin")
  62. atm_field.insert(10, str(current_pressure) + " hPa")
  63. humid_field.insert(15, str(current_humidiy) + " %")
  64. desc_field.insert(10, str(weather_description) )
  65.  
  66. # if city is not found
  67. else :
  68.  
  69. # message dialog box appear which
  70. # shows given Error meassgae
  71. messagebox.showerror("Error", "City Not Found \n"
  72. "Please enter valid city name")
  73.  
  74. # clear the content of city_field entry box
  75. city_field.delete(0, END)
  76.  
  77.  
  78. # Function for clearing the
  79. # contents of all text entry boxes
  80. def clear_all() :
  81. city_field.delete(0, END)
  82. temp_field.delete(0, END)
  83. atm_field.delete(0, END)
  84. humid_field.delete(0, END)
  85. desc_field.delete(0, END)
  86.  
  87. # set focus on the city_field entry box
  88. city_field.focus_set()
  89.  
  90.  
  91. # Driver code
  92. if __name__ == "__main__" :
  93.  
  94. # Create a GUI window
  95. root = Tk()
  96.  
  97. # set the name of tkinter GUI window
  98. root.title("Gui Application")
  99.  
  100. # Set the background colour of GUI window
  101. root.configure(background = "light green")
  102.  
  103. # Set the configuration of GUI window
  104. root.geometry("425x175")
  105.  
  106. # Create a Weather Gui Application label
  107. headlabel = Label(root, text = "Weather Gui Application",
  108. fg = 'black', bg = 'red')
  109.  
  110. # Create a City name : label
  111. label1 = Label(root, text = "City name : ",
  112. fg = 'black', bg = 'dark green')
  113.  
  114. # Create a City name : label
  115. label2 = Label(root, text = "Temperature :",
  116. fg = 'black', bg = 'dark green')
  117.  
  118. # Create a atm pressure : label
  119. label3 = Label(root, text = "atm pressure :",
  120. fg = 'black', bg = 'dark green')
  121.  
  122. # Create a humidity : label
  123. label4 = Label(root, text = "humidity :",
  124. fg = 'black', bg = 'dark green')
  125.  
  126. # Create a description :label
  127. label5 = Label(root, text = "description :",
  128. fg = 'black', bg = 'dark green')
  129.  
  130.  
  131. # grid method is used for placing
  132. # the widgets at respective positions
  133. # in table like structure .
  134. headlabel.grid(row = 0, column = 1)
  135. label1.grid(row = 1, column = 0, sticky ="E")
  136. label2.grid(row = 3, column = 0, sticky ="E")
  137. label3.grid(row = 4, column = 0, sticky ="E")
  138. label4.grid(row = 5, column = 0, sticky ="E")
  139. label5.grid(row = 6, column = 0, sticky ="E")
  140.  
  141.  
  142. # Create a text entry box
  143. # for filling or typing the information.
  144. city_field = Entry(root)
  145. temp_field = Entry(root)
  146. atm_field = Entry(root)
  147. humid_field = Entry(root)
  148. desc_field = Entry(root)
  149.  
  150. # grid method is used for placing
  151. # the widgets at respective positions
  152. # in table like structure .
  153. # ipadx keyword argument set width of entry space .
  154. city_field.grid(row = 1, column = 1, ipadx ="100")
  155. temp_field.grid(row = 3, column = 1, ipadx ="100")
  156. atm_field.grid(row = 4, column = 1, ipadx ="100")
  157. humid_field.grid(row = 5, column = 1, ipadx ="100")
  158. desc_field.grid(row = 6, column = 1, ipadx ="100")
  159.  
  160. # Create a Submit Button and attached
  161. # to tell_weather function
  162. button1 = Button(root, text = "Submit", bg = "red",
  163. fg = "black", command = tell_weather)
  164.  
  165. # Create a Clear Button and attached
  166. # to clear_all function
  167. button2 = Button(root, text = "Clear", bg = "red",
  168. fg = "black", command = clear_all)
  169.  
  170. # grid method is used for placing
  171. # the widgets at respective positions
  172. # in table like structure .
  173. button1.grid(row = 2, column = 1)
  174. button2.grid(row = 7, column = 1)
  175.  
  176. # Start the GUI
  177. root.mainloop()
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Traceback (most recent call last):
  File "/usr/lib/python3.7/py_compile.py", line 143, in compile
    _optimize=optimize)
  File "<frozen importlib._bootstrap_external>", line 791, in source_to_code
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "./prog.py", line 2
    from tkinter import * from tkinter import messagebox 
                             ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.7/py_compile.py", line 147, in compile
    raise py_exc
py_compile.PyCompileError:   File "./prog.py", line 2
    from tkinter import * from tkinter import messagebox
                             ^
SyntaxError: invalid syntax

stdout
Standard output is empty