我正在尝试设置天气应用程序的背景图像我尝试为此使用画布,但图像不会像窗口一样展开,它仅在我单击搜索查看结果时才会展开,标签看起来很乱,就像我使用过的一样网格
有人可以帮我解决这个问题吗
这是代码
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']
通过标签显示结果:
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()
这是设置背景图像的代码
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()