background:
During the exam week, a big Python assignment was suddenly assigned. I originally planned to find a ready-made one online and splice it together, but otherwise I would have to pay 50 yuan for the same needs, or it would be too professional, and it would not be something that students could complete in a few days. So I decided to make one myself.
Task requirements:
Design of weather forecast system based on python language
(1) The system must have an interface operation method and a friendly interface;
(2) The system can select the city for weather forecast;
(3) The system can display weather data for at least seven days;
(4) The system can display Temperature range, wind, rain, snow and other weather conditions;
(5) The system can perform statistical analysis on the temperature, wind and other conditions of each city;
(6) The system can perform statistical analysis on the temperature, wind and other conditions of each city that change over time. A kind of graphic display;
(7) The system can save and process weather information.
I added two small functions myself:
(8) Use the existing tcl script to beautify the interface and simulate the win11 style
(9) Add one-click switching to dark night mode
Effect demonstration:
Boot interface:
7-day pattern and line chart:
15-day pattern and statistical chart:
Dark night mode:
Crawled weather for the next 15 days:
Code:
1. Crawling the weather for the next 15 days
Since the question requires weather data to include wind strength, on mainstream weather websites, the 15-day forecast often does not include wind strength, or the 15 days are split into 1-7 and 8-15 for detailed broadcast.
Therefore, here we crawled 1-7 days of weather network weather data and 8-15 days of weather network weather data respectively, and added them to a list to achieve date, week, weather conditions, minimum temperature, maximum temperature, wind level Data crawling can output 7-day CSV and 15-day CSV files.
This part of the code is a change to the existing weather crawling code, as shown below:
import requests
from bs4 import BeautifulSoup
import csv
import os
def getHTMLtext(url):
try:
r = requests.get(url, timeout=30)
r.raise_for_status()
r.encoding = r.apparent_encoding
print("成功访问")
return r.text
except:
print("访问错误")
return " "
def get_content(html):
final = [] # 初始化一个列表保存数据
bs = BeautifulSoup(html, "html.parser") # 创建BeautifulSoup对象
body = bs.body
data = body.find('div', {'id': '7d'}) # 找到div标签且id = 7d
# 下面爬取7天的数据
ul = data.find('ul') # 找到所有的ul标签
li = ul.find_all('li') # 找到左右的li标签
i = 0 # 控制爬取的天数
for day in li: # 遍历找到的每一个li
if 7 > i >= 0:
temp = [] # 临时存放每天的数据
date = day.find('h1').string # 得到日期
date1 = date[0:date.index('(')]
temp.append(date1)
week = date[date.index('(')+1:date.index(')')] # 得到星期
temp.append(week)
inf = day.find_all('p') # 找出li下面的p标签,提取第一个p标签的值,即天气
wea = inf[0].string
if '转' in wea:
wea = wea[0:wea.index('转')]
temp.append(wea)
tem_low = inf[1].find('i').string # 找到最低气温
temp.append(tem_low[:-1])
if inf[1].find('span') is None: # 天气预报可能没有最高气温
tem_high = None
temp.append(tem_high)
else:
tem_high = inf[1].find('span').string # 找到最高气温
if tem_high[-1] == '℃':
temp.append(tem_high[:-1])
else:
temp.append(tem_high)
wind_scale = inf[2].find('i').string # 找到风级
wind = wind_scale[0:wind_scale.index('级')]
temp.append(wind)
final.append(temp)
i = i + 1
return final
def get_content2(html):
final = [] # 初始化一个列表保存数据
bs = BeautifulSoup(html, "html.parser") # 创建BeautifulSoup对象
body = bs.body
data = body.find('div', {'id': '15d'}) # 找到div标签且id = 15d
ul = data.find('ul') # 找到所有的ul标签
li = ul.find_all('li') # 找到左右的li标签
i = 0 # 控制爬取的天数
for day in li: # 遍历找到的每一个li
if i < 8:
temp = [] # 临时存放每天的数据
date = day.find('span', {'class': 'time'}).string # 得到日期
date1 = date[date.index('(') + 1:-1] # 取出日期号
temp.append(date1)
week = date[0:date.index('(')]
temp.append(week)
weather = day.find('span', {'class': 'wea'}).string # 找到天气
if '转' in weather:
weather = weather[0:weather.index('转')]
temp.append(weather)
tem = day.find('span', {'class': 'tem'}).text # 找到温度
temp.append(tem[tem.index('/') + 1:-1]) # 找到最低气温
temp.append(tem[:tem.index('/') - 1]) # 找到最高气温
wind_scale = day.find('span', {'class': 'wind1'}).string # 找到风级
wind = wind_scale[0:wind_scale.index('级')]
temp.append(wind)
final.append(temp)
return final
def write_to_csv(file_name, data):
with open(file_name, 'a', errors='ignore', newline='') as f:
header = ['日期', '星期', '天气', '最低气温', '最高气温', '风级']
f_csv = csv.writer(f)
f_csv.writerow(header)
f_csv.writerows(data)
def main(city):
if city == '西安':
erea = '101110101'
if city == '武汉':
erea = '101200101'
if city == '深圳':
erea = '101280601'
if city == '上海':
erea = '101020100'
if city == '哈尔滨':
erea = '101050101'
url1 = 'http://www.weather.com.cn/weather/' + erea + '.shtml' # 7天天气中国天气网
url2 = 'http://www.weather.com.cn/weather15d/' + erea + '.shtml' # 8-15天天气中国天气网
html1 = getHTMLtext(url1)
data1_7 = get_content(html1) # 获得1-7天和当天的数据
html2 = getHTMLtext(url2)
data8_15 = get_content2(html2) # 获得8-15天数据
data15 = data1_7 + data8_15
if os.path.isfile("weather15.csv"): # 检查文件是否存在
os.remove("weather15.csv") # 删除文件
if os.path.isfile("weather7.csv"): # 检查文件是否存在
os.remove("weather7.csv") # 删除文件
write_to_csv('weather7.csv', data1_7) # 保存为csv文件
write_to_csv('weather15.csv', data15) # 保存为csv文件
Note that this part of the code only defines five functions for calling the main py file and cannot be run independently. And the input variable here is the city name. The one I wrote only supports Xi'an, Wuhan, Shenzhen, Shanghai, and Harbin. You can add the city ID by yourself.
2. Create an options dialog box
The options dialog box is mainly used to set the city or date mode. It can be defined and placed using the tkinter library. The code is as follows:
# 选项栏显示
def xuanxianglan():
global var_4, var_6
option_city_list = ["城市", "西安", "武汉", "深圳", "上海", "哈尔滨"]
option_mode_list = ["模式", "7天", "15天"]
var_4 = tk.StringVar(value=option_city_list[1]) # 存储城市
var_6 = tk.StringVar(value=option_mode_list[1])
# 创建一个框架!!!!!!!
check_frame = ttk.LabelFrame(frame1, text="选项", padding=(20, 10))
check_frame.grid(row=0, column=0, padx=(20, 10), pady=(20, 10), sticky="nw")
# 创建选择列表选择城市
optionmenucity = ttk.OptionMenu(check_frame, var_4, *option_city_list)
optionmenucity.grid(row=0, column=0, padx=5, pady=10, sticky="nsew")
# 创建选择列表选择模式
optionmenumode = ttk.OptionMenu(check_frame, var_6, *option_mode_list)
optionmenumode.grid(row=1, column=0, padx=5, pady=10, sticky="nsew")
# 创建按钮切换统计图
button = ttk.Button(check_frame, text="统计图")
button.grid(row=2, column=0, padx=5, pady=10, sticky="nsew")
button.config(command=get_statistic)
# 创建按钮切换折线图
button1 = ttk.Button(check_frame, text="折线图")
button1.grid(row=3, column=0, padx=5, pady=10, sticky="nsew")
button1.config(command=get_line_chart)
var_1 = tk.StringVar()
def callCheckbutton():
if var_1.get() == '1':
root.tk.call("set_theme", "dark")
else:
root.tk.call("set_theme", "light")
# Switch来切换暗夜模式
switchdark = ttk.Checkbutton(check_frame, variable=var_1, text="Dark", style="Switch.TCheckbutton",
command=callCheckbutton)
switchdark.grid(row=4, column=0, padx=5, pady=10, sticky="nsew")
3. Create a weather condition display bar
It is divided into 7-day mode and 15-day mode to create different display columns. There are 7 LabelFrame components in 7 days, 7 of which need to be arranged in a row, and the 7-day mode supports image display of weather; the 15-day mode arranges 15 LabelFrame components in 5×3.
The code for seven days is as follows:
# 7天的天气栏显示
def tianqilan7():
global day16_frame, day17_frame, day18_frame, day19_frame, day20_frame, day21_frame, day22_frame
global tianqi7flag
jiazaitubiao()
tianqi7flag = 1
# 创建第一天天气框架!!!!!
day16_frame = ttk.LabelFrame(frame1, text='今天'.format(df['星期'][0]), padding=(20, 10))
day16_frame.grid(row=0, column=1, padx=0, pady=(20, 10), sticky="nsw")
# 先插一张图片
tianqi_img16 = tk.Label(day16_frame, image=ifphoto(df['天气'][0]))
tianqi_img16.grid(row=0, column=0)
# 再插一个大文本
word1 = tk.Label(day16_frame,
text='\n{}\n'.format(df['天气'][0]),
font=('仿宋', 22),
padx=0,
pady=0)
word1.grid(row=1, column=0, padx=5, pady=0, sticky="s")
# 再插几行小文本
word2 = tk.Label(day16_frame,
text='{}~{}℃\n{}级风\n{}'.format(df['最低气温'][0], df['最高气温'][0], df['风级'][0],
df['日期'][0]),
font=('微软雅黑', 10),
padx=0,
pady=0)
word2.grid(row=2, column=0, padx=5, pady=0, sticky="s")
# 创建第二天天气框架!!!!!
day17_frame = ttk.LabelFrame(frame1, text='明天'.format(df['星期'][1]), padding=(20, 10))
day17_frame.grid(row=0, column=2, padx=3, pady=(20, 10), sticky="ns")
# 先插一张图片
tianqi_img17 = tk.Label(day17_frame, image=ifphoto(df['天气'][1]))
tianqi_img17.grid(row=0, column=0)
# 再插一个大文本
word3 = tk.Label(day17_frame,
text='\n{}\n'.format(df['天气'][1]),
font=('仿宋', 22),
padx=0,
pady=0)
word3.grid(row=1, column=0, padx=5, pady=0, sticky="s")
# 再插几行小文本
word4 = tk.Label(day17_frame,
text='{}~{}℃\n{}级风\n{}'.format(df['最低气温'][1], df['最高气温'][1], df['风级'][1],
df['日期'][1]),
font=('微软雅黑', 10),
padx=0,
pady=0)
word4.grid(row=2, column=0, padx=5, pady=0, sticky="s")
# 创建第三天天气框架!!!!!
day18_frame = ttk.LabelFrame(frame1, text='{}'.format(df['星期'][2]), padding=(20, 10))
day18_frame.grid(row=0, column=3, padx=0, pady=(20, 10), sticky="nsw")
# 先插一张图片
tianqi_img18 = tk.Label(day18_frame, image=ifphoto(df['天气'][2]))
tianqi_img18.grid(row=0, column=0)
# 再插一个大文本
word5 = tk.Label(day18_frame,
text='\n{}\n'.format(df['天气'][2]),
font=('仿宋', 22),
padx=0,
pady=0)
word5.grid(row=1, column=0, padx=5, pady=0, sticky="s")
# 再插几行小文本
word6 = tk.Label(day18_frame,
text='{}~{}℃\n{}级风\n{}'.format(df['最低气温'][2], df['最高气温'][2], df['风级'][2],
df['日期'][2]),
font=('微软雅黑', 10),
padx=0,
pady=0)
word6.grid(row=2, column=0, padx=5, pady=0, sticky="s")
# 创建第四天天气框架!!!!!
day19_frame = ttk.LabelFrame(frame1, text='{}'.format(df['星期'][3]), padding=(20, 10))
day19_frame.grid(row=0, column=4, padx=3, pady=(20, 10), sticky="nsw")
# 先插一张图片
tianqi_img19 = tk.Label(day19_frame, image=ifphoto(df['天气'][3]))
tianqi_img19.grid(row=0, column=0)
# 再插一个大文本
word7 = tk.Label(day19_frame,
text='\n{}\n'.format(df['天气'][3]),
font=('仿宋', 22),
padx=0,
pady=0)
word7.grid(row=1, column=0, padx=5, pady=0, sticky="s")
# 再插几行小文本
word8 = tk.Label(day19_frame,
text='{}~{}℃\n{}级风\n{}'.format(df['最低气温'][3], df['最高气温'][3], df['风级'][3],
df['日期'][3]),
font=('微软雅黑', 10),
padx=0,
pady=0)
word8.grid(row=2, column=0, padx=5, pady=0, sticky="s")
# 创建第五天天气框架!!!!!
day20_frame = ttk.LabelFrame(frame1, text='{}'.format(df['星期'][4]), padding=(20, 10))
day20_frame.grid(row=0, column=5, padx=2, pady=(20, 10), sticky="nsw")
# 先插一张图片
tianqi_img20 = tk.Label(day20_frame, image=ifphoto(df['天气'][4]))
tianqi_img20.grid(row=0, column=0)
# 再插一个大文本
word9 = tk.Label(day20_frame,
text='\n{}\n'.format(df['天气'][4]),
font=('仿宋', 22),
padx=0,
pady=0)
word9.grid(row=1, column=0, padx=5, pady=0, sticky="s")
# 再插几行小文本
word10 = tk.Label(day20_frame,
text='{}~{}℃\n{}级风\n{}'.format(df['最低气温'][4], df['最高气温'][4], df['风级'][4],
df['日期'][4]),
font=('微软雅黑', 10),
padx=0,
pady=0)
word10.grid(row=2, column=0, padx=5, pady=0, sticky="s")
# 创建第六天天气框架!!!!!
day21_frame = ttk.LabelFrame(frame1, text='{}'.format(df['星期'][5]), padding=(20, 10))
day21_frame.grid(row=0, column=6, padx=2, pady=(20, 10), sticky="nsw")
# 先插一张图片
tianqi_img21 = tk.Label(day21_frame, image=ifphoto(df['天气'][5]))
tianqi_img21.grid(row=0, column=0)
# 再插一个大文本
word11 = tk.Label(day21_frame,
text='\n{}\n'.format(df['天气'][5]),
font=('仿宋', 22),
padx=0,
pady=0)
word11.grid(row=1, column=0, padx=5, pady=0, sticky="s")
# 再插几行小文本
word12 = tk.Label(day21_frame,
text='{}~{}℃\n{}级风\n{}'.format(df['最低气温'][5], df['最高气温'][5], df['风级'][5],
df['日期'][5]),
font=('微软雅黑', 10),
padx=0,
pady=0)
word12.grid(row=2, column=0, padx=5, pady=0, sticky="s")
# 创建第七天天气框架!!!!!
day22_frame = ttk.LabelFrame(frame1, text='{}'.format(df['星期'][6]), padding=(20, 10))
day22_frame.grid(row=0, column=7, padx=1, pady=(20, 10), sticky="nsw")
# 先插一张图片
tianqi_img22 = tk.Label(day22_frame, image=ifphoto(df['天气'][6]))
tianqi_img22.grid(row=0, column=0)
# 再插一个大文本
word13 = tk.Label(day22_frame,
text='\n{}\n'.format(df['天气'][6]),
font=('仿宋', 22),
padx=0,
pady=0)
word13.grid(row=1, column=0, padx=5, pady=0, sticky="s")
# 再插几行小文本
word14 = tk.Label(day22_frame,
text='{}~{}℃\n{}级风\n{}'.format(df['最低气温'][6], df['最高气温'][6], df['风级'][6],
df['日期'][6]),
font=('微软雅黑', 10),
padx=0,
pady=0)
word14.grid(row=2, column=0, padx=5, pady=0, sticky="s")
print('7天天气情况插入成功')
In order to facilitate debugging, I did not use a for loop to define the 7 identical components. This needs to be improved.
I will only list the first three of the 15 days of code:
def tianqilan15():
global day1_frame, day2_frame, day3_frame, day4_frame, day5_frame, day6_frame, day7_frame, day8_frame
global day9_frame, day10_frame, day11_frame, day12_frame, day13_frame, day14_frame, day15_frame
global tianqi15flag
jiazaitubiao()
tianqi15flag = 1
# 创建第1天天气框架!!!!!
day1_frame = ttk.LabelFrame(frame1, text='今天'.format(df['星期'][0]), padding=(20, 10))
day1_frame.grid(row=0, column=1, padx=26, pady=20, sticky="n")
# 插一个大文本
word1 = tk.Label(day1_frame,
text='{}'.format(df['天气'][0]),
font=('仿宋', 16),
padx=0,
pady=0)
word1.grid(row=0, column=0, padx=0, pady=0, sticky="nsew")
# 再插几行小文本
word2 = tk.Label(day1_frame,
text='{}~{}℃\n{}级风\n{}'.format(df['最低气温'][0], df['最高气温'][0], df['风级'][0],
df['日期'][0]),
font=('微软雅黑', 10),
padx=0,
pady=0)
word2.grid(row=1, column=0, padx=5, pady=0, sticky="nsew")
# 创建第2天天气框架!!!!!
day2_frame = ttk.LabelFrame(frame1, text='明天'.format(df['星期'][1]), padding=(20, 10))
day2_frame.grid(row=0, column=2, padx=26, pady=20, sticky="n")
# 插一个大文本
word3 = tk.Label(day2_frame,
text='{}'.format(df['天气'][1]),
font=('仿宋', 16),
padx=0,
pady=0)
word3.grid(row=0, column=0, padx=0, pady=0, sticky="n")
# 再插几行小文本
word4 = tk.Label(day2_frame,
text='{}~{}℃\n{}级风\n{}'.format(df['最低气温'][1], df['最高气温'][1], df['风级'][1],
df['日期'][1]),
font=('微软雅黑', 10),
padx=0,
pady=0)
word4.grid(row=1, column=0, padx=5, pady=0, sticky="n")
# 创建第3天天气框架!!!!!
day3_frame = ttk.LabelFrame(frame1, text='{}'.format(df['星期'][2]), padding=(20, 10))
day3_frame.grid(row=0, column=3, padx=25, pady=20, sticky="n")
# 插一个大文本
word5 = tk.Label(day3_frame,
text='{}'.format(df['天气'][2]),
font=('仿宋', 16),
padx=0,
pady=0)
word5.grid(row=0, column=0, padx=0, pady=0, sticky="n")
# 再插几行小文本
word6 = tk.Label(day3_frame,
text='{}~{}℃\n{}级风\n{}'.format(df['最低气温'][2], df['最高气温'][2], df['风级'][2],
df['日期'][2]),
font=('微软雅黑', 10),
padx=0,
pady=0)
word6.grid(row=1, column=0, padx=5, pady=0, sticky="n")
print('15天天气情况插入成功')
4. Use csv files to create line charts and statistical charts (pie charts)
Using the matplotlib library, four functions were written to create a 7-day line chart, a 15-day line chart, a 7-day statistical chart, and a 15-day statistical chart. code show as below:
import matplotlib.pyplot as plt
import pandas as pd
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['font.family'] = 'sans-serif'
plt.rcParams['axes.unicode_minus'] = False
def line7_get():
data = pd.read_csv('weather7.csv', encoding='gb18030')
xdata = data.iloc[:, 0]
y1data = data.iloc[:, 3]
y2data = data.iloc[:, 4]
fig = plt.figure(dpi=96, figsize=(20, 9))
plt.plot(xdata, y1data, color='b', marker='o', mec='b', mfc='w', label=u'最低气温')
plt.plot(xdata, y2data, color='r', marker='o', mec='r', mfc='w',
label=u'最高气温') # color可自定义折线颜色,marker可自定义点形状,label为折线标注
# plt.title(u"未来15天气温折线图", size=20)
plt.legend()
plt.xlabel(u' ', size=100)
plt.legend(prop={'size': 22})
plt.yticks(size=20, weight='bold')
plt.xticks(size=20, weight='bold')
plt.ylabel(u'摄氏度', size=30)
plt.savefig("line7.png")
def line15_get():
data = pd.read_csv('weather15.csv', encoding='gb18030')
xdata = data.iloc[:, 0]
y1data = data.iloc[:, 3]
y2data = data.iloc[:, 4]
fig = plt.figure(dpi=96, figsize=(20, 9))
plt.plot(xdata, y1data, color='b', marker='o', mec='b', mfc='w', label=u'最低气温')
plt.plot(xdata, y2data, color='r', marker='o', mec='r', mfc='w',
label=u'最高气温') # color可自定义折线颜色,marker可自定义点形状,label为折线标注
# plt.title(u"未来15天气温折线图", size=20)
plt.legend()
plt.xlabel(u' ', size=100)
plt.legend(prop={'size': 22})
plt.yticks(size=20, weight='bold')
plt.xticks(size=20, weight='bold')
plt.ylabel(u'摄氏度', size=30)
plt.savefig("line15.png")
def tong15_get():
data = pd.read_csv('weather15.csv', encoding='gb18030')
xdata = data.iloc[:, 2]
weather = list(xdata)
dic_wea = {}
for i in range(0, 15):
if weather[i] in dic_wea.keys():
dic_wea[weather[i]] += 1
else:
dic_wea[weather[i]] = 1
explode = [0.01] * len(dic_wea.keys())
fig = plt.figure(dpi=128, figsize=(10, 6))
color = ['lightskyblue', 'silver', 'yellow', 'salmon', 'grey', 'lime', 'gold', 'red', 'green', 'pink']
patches, l_text, p_text = plt.pie(dic_wea.values(), explode=explode, labels=dic_wea.keys(), autopct='%1.1f%%',
colors=color)
for t in l_text:
t.set_size(25)
for t in p_text:
t.set_size(25)
plt.savefig("tong15.png")
def tong7_get():
data = pd.read_csv('weather7.csv', encoding='gb18030')
xdata = data.iloc[:, 2]
weather = list(xdata)
dic_wea = {}
for i in range(0, 7):
if weather[i] in dic_wea.keys():
dic_wea[weather[i]] += 1
else:
dic_wea[weather[i]] = 1
explode = [0.01] * len(dic_wea.keys())
fig = plt.figure(dpi=128, figsize=(10, 6))
color = ['lightskyblue', 'silver', 'yellow', 'salmon', 'grey', 'lime', 'gold', 'red', 'green', 'pink']
patches, l_text, p_text = plt.pie(dic_wea.values(), explode=explode, labels=dic_wea.keys(), autopct='%1.1f%%',
colors=color)
for t in l_text:
t.set_size(25)
for t in p_text:
t.set_size(25)
plt.savefig("tong7.png")
Similarly, this part of the code only defines four functions for calling the main py file and cannot run independently.
5. Press the "Line Chart" button to start the function (similar to statistical charts)
There is a judgment here: the button can only be pressed when the city and mode are entered at the same time, otherwise the terminal outputs "Please choose correctly!"
# 设置折线按钮:得到折线图与天气栏
def get_line_chart():
global df
if var_4.get() != '城市' and var_6.get() != '模式':
# 爬取到城市var_4未来15天的数据
weather_spider(var_4.get())
# 处理一下
df = pd.read_csv('weather15.csv', encoding='gb18030')
# 这里放天气图和折线图
if var_6.get() == '7天':
forgettianqi()
tianqilan7()
# bujuforget()
buju('折线图', 540, 580)
chartforget()
line7fun() # 7天统计图
elif var_6.get() == '15天':
forgettianqi()
tianqilan15()
bujuforget()
buju('折线图', 540, 650)
chartforget()
line15fun() # 15天统计图
else:
print('请正确选择!')
6. Implementation of other small functions
(1) Startup screen: add a picture and then not display it after two seconds
def begin():
global begin
def close():
begin_img.place_forget()
begin_pic = Image.open("main.png")
begin_pic = begin_pic.resize((1038, 678))
begin = ImageTk.PhotoImage(begin_pic)
begin_img = tk.Label(frame1, image=begin)
begin_img.place(x=0, y=0)
frame1.after(2000, close)
(2) Define and determine which image to insert
# 将天气图标进行命名
def jiazaitubiao():
global xiaoyu, yin, yu, duoyun, qing
photo1 = Image.open("weather_xiaoyu.png")
photo2 = Image.open("weather_yu.png")
photo3 = Image.open("weather_yin.png")
photo4 = Image.open("weather_duoyun.png")
photo5 = Image.open("weather_qing.png")
photo1 = photo1.resize((60, 60))
photo2 = photo2.resize((60, 60))
photo3 = photo3.resize((60, 60))
photo4 = photo4.resize((60, 60))
photo5 = photo5.resize((60, 60))
xiaoyu = ImageTk.PhotoImage(photo1)
yu = ImageTk.PhotoImage(photo2)
yin = ImageTk.PhotoImage(photo3)
duoyun = ImageTk.PhotoImage(photo4)
qing = ImageTk.PhotoImage(photo5)
# 判断使用哪一副画
def ifphoto(weather):
if '小雨' in weather:
ans = xiaoyu
elif '雨' in weather:
ans = yu
elif '阴' in weather:
ans = yin
elif '多云' in weather:
ans = duoyun
elif '晴' in weather:
ans = qing
return ans
(3) Use of flags
When switching one picture to another, or one set of labelframes to another, if the old pictures are not removed, they will overlap and display. Since the definition and placement of our image and labelframe are written together, when a certain image is not released, it means that it is not defined. If you request to remove it, an error will be reported as not defined. Here we define the flag bit to determine whether a certain picture has been released.
Code for removing the weather display bar using the flag bit: (similar to the removal picture)
# 关闭天气栏
def forgettianqi():
global tianqi15flag
global tianqi7flag
if tianqi15flag == 1: # 判断是否有15天的放下
tianqi15flag = 0
day1_frame.grid_forget()
day2_frame.grid_forget()
day3_frame.grid_forget()
day4_frame.grid_forget()
day5_frame.grid_forget()
day6_frame.grid_forget()
day7_frame.grid_forget()
day8_frame.grid_forget()
day9_frame.grid_forget()
day10_frame.grid_forget()
day11_frame.grid_forget()
day12_frame.grid_forget()
day13_frame.grid_forget()
day14_frame.grid_forget()
day15_frame.grid_forget()
if tianqi7flag == 1:
tianqi7flag = 0
day16_frame.grid_forget()
day17_frame.grid_forget()
day18_frame.grid_forget()
day19_frame.grid_forget()
day20_frame.grid_forget()
day21_frame.grid_forget()
day22_frame.grid_forget()
Improve:
Due to the rush of time, the program has many areas worthy of improvement:
1. A total of 22 labelframes, 7+15, are defined. In fact, they can be defined using a for loop, which greatly reduces the workload. 2. It is
lazy to crawl and generate two files. In fact, only 15 days are generated, and the first 7 days are enough. Since there are two files
3. The white border of the chart display box in dark night mode is too ugly. Think of a way to change it to output a transparent png image.