-1

I am trying to set the background image of weather app I tried to use canvas for that but the image doesn't expand as the window and it expands only when I click search to see the results , the labels come out messy as although I used grid

can someone help me to fix this please

here is the code

import requests
import pandas as pd
import json
import requests
import tkinter as tk
from PIL import Image, ImageTk
root=tk.Tk()
root.minsize(width=1025, height=522)
def get_entry(*args):
    api_address='http://api.openweathermap.org/data/2.5/weathe?appid=4f825a5771554e0c8fcccb7be76aab11&q='
    city=entry_1.get()
    url = api_address + city
    json_data = requests.get(url).json()
    format_add = json_data['weather'][0]['description']
    temp=json_data['main']['temp']
    press=json_data['main']['pressure']
    wind=json_data['wind']['speed']

displaying results through labels:

    label_2=tk.Label(canvas, text='the weather in '+city+' : '+format_add)
    label_3=tk.Label(canvas, text='the temperature :' +str(temp))
    label_4=tk.Label(canvas, text='the pressure:' +str(press))
    label_5=tk.Label(canvas, text='the wind speed:' +str(wind))
    label_2.grid(row=5,column=0,sticky='e',padx=20,pady=20)
    label_3.grid(row=6,column=0,sticky='e',padx=20,pady=20)
    label_4.grid(row=7,column=0,sticky='e',padx=20,pady=20)
    label_5.grid(row=8,column=0,sticky='e',padx=20,pady=20)
    Label = tk.Label(canvas, text =str(city), font = ('Comic Sans MS',30),
     fg = 'blue')
    Label.grid(row=4,column=630,sticky='n')
    rain_Frame=(root)
    rain_Frame.grid()

here is the code to set the background image

    background_image=Image.open('rain.jpg')
    canvas = tk.Canvas(rain_Frame, width=1025, height=522)
    canvas.grid()
    image1=ImageTk.PhotoImage(background_image)
    canvas.create_image(0,0, image=image1, anchor='center')
    label1=tk.Label(canvas,text='search by country/city')
    label1.grid(row=0,column=0)
    entry_1=tk.Entry(canvas)
    label_1.grid(row=0,column=0)
    entry_1.grid(row=0,column=20)
    search_button=tk.Button(canvas,text="search",
                      command=get_entry,
                width = 10, activebackground = "#33B5E5")

   search_button.grid(row=0,column=35,padx=10,pady=1,sticky='e')

   root.resizable(width=False, height=False)

   root.mainloop()

here is what the outcome looks like Tkinter weather app

k.john
  • 1
  • 2
  • Please can you provide a **minimal**, **complete** and **verifiable** example according to [this guide](https://stackoverflow.com/help/mcve). – Ethan Field Nov 13 '17 at 15:29

1 Answers1

0

Hmmm... why not use a bigger image? :D

Or stop the window resizing?

root = Tk()
root.resizable(0,0)

For the labels, you could use a grid... or you could just place them somewhere and leave them?

label.place(x=20, y=380)
David W. Beck
  • 182
  • 1
  • 9