python之Tkinter
Tkinter使用学习GUI界面设计,通过API接口获取各个城市当前的天气情况,最后时候pyinstaller包装成exe文件。
![在这里插入图片描述](https://img-blog.csdnimg.cn/0aa12ec992fa4df498fa804af3e9cf69.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA6Zi_56OK5b6I5Yqq5Yqb,size_18,color_FFFFFF,t_70,g_se,x_16#pic_center)
Python代码如下:
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 30 10:27:30 2021
@author: 阿磊很努力
summary:Tkinter study(GUI)
"""
#======================= 学习网站 =======================#
# kinter manual
# https://www.tutorialspoint.com/python/python_gui_programming.htm
# HTML color Picker
# https://www.w3schools.com/colors/colors_picker.asp
# pack可以设置位置:fill & expand & side
# 网站:https://www.tutorialspoint.com/python/tk_pack.htm
# grid可以像网格一样设置位置: row & column
# 网站:https://www.tutorialspoint.com/python/tk_grid.htm
# place最大程度控制你的GUI
# 网站:https://www.tutorialspoint.com/python/tk_place.htm
# pyinstaller可以将python文件包装为exe文件
# 网站:https://pyinstaller.readthedocs.io/en/stable/
import tkinter as tk
from tkinter import font
import requests
H = 500
W = 600
font1 = {'family': 'Times New Roman',
'weight': 'bold',
'size': 120,
}
# def main( ):
# # Create root window
# root = tk.Tk()
# # Create instance with root as the master window
# app = Application(master=root)
# # An infinite loop used to run the application
# app.mainloop()
#======================= 界面基本属性设置 =======================#
#root = tk.Tk()
## 窗口大小设置
#canvas = tk.Canvas(root,height = H,width = W)
#canvas.pack()
## 背景框设置
#frame = tk.Frame(root,bg = '#b3b3ff')
#frame.place(relx = 0.1, rely = 0.1, relwidth = 0.8, relheight = 0.8) #relx/y是指frame的相对位置,relwidth是frame相对宽度
##按钮设置
#button = tk.Button(frame,text='click',bg = 'gray') #fg = 'red' 字体颜色为红色
#button.pack()
##frame中添加小部件
# label = tk.Label(frame,text = 'Label',bg = '#ff8533' )
# label.pack()
# entry = tk.Entry(frame,bg = '#5dd55d',font=font1)
# entry.pack()
# root.mainloop()
#======================= 功能设置 =======================#
def Test_function(entry):
print("This is the entry:",entry)
# api.openweathermap.org/data/2.5/forecast?q={city name},{country code}
# a4aa5e3d83ffefaba8c00284de6ef7c3
def format_response(weather):
try:
country = weather['sys']['country']
name = weather['name']
des = weather['weather'][0]['description']
temp = weather['main']['temp']
final_str = ' Country:%s \n City:%s \n Conditions:%s \n Temperature (K):%s' % (country,name,des,temp)
#final_str = str(country)+''+ str(name)+''+ str(des) +''+ str(temp)
except:
final_str = 'There was a problem retrieving that information'
return final_str
def get_weather(city):
weather_key = 'a4aa5e3d83ffefaba8c00284de6ef7c3'
url = 'http://api.openweathermap.org/data/2.5/weather'
params = {'APPID':weather_key,'q': city,'units':'standard'}
response = requests.get(url,params = params)
print(response.json())
weather = response.json()
# print(weather['sys']['country'])
# print(weather['name'])
# print(weather['weather'][0]['description'])
# print(weather['main']['temp'])
label['text']= format_response(weather)
#======================= 实例展示 =======================#
root = tk.Tk()
#wlcm_scrn = tk.Toplevel()
#窗口
canvas = tk.Canvas(root,height = H,width = W)
canvas.pack()
#背景图片设置
#PhoteImage只能读取gif或者PNG文件,强行转成PNG的文件也不行
background_image = tk.PhotoImage(file ='landscape.png')
background_label = tk.Label(root,image = background_image)
background_label.place(relheight = 1, relwidth = 1)
#背景框
frame = tk.Frame(root,bg = '#80c1ff',bd = 5)
frame.place(relx = 0.5,rely = 0.1, relwidth = 0.75, relheight = 0.1, anchor = 'n')
#输入框
entry = tk.Entry(frame,font = font1)
entry.place(relwidth = 0.65,relheight = 1)
#按钮
button = tk.Button(frame,text = 'Get results',font = font1,command = lambda: get_weather(entry.get()))
button.place(relx = 0.7,relheight = 1, relwidth = 0.3)
#下方的背景框
low_frame = tk.Frame(root,bg = '#80c1ff',bd = 10)
low_frame.place(relx = 0.5,rely = 0.25,relwidth = 0.75,relheight = 0.6, anchor = 'n')
#标签设置
label = tk.Label(low_frame,font = ('Courier',15),anchor = 'nw',justify = 'left',bd = 10)
label.place(relwidth = 1, relheight = 1)
#print(tk.font.families())
root.mainloop()