python之Tkinter

python之Tkinter


Tkinter使用学习GUI界面设计,通过API接口获取各个城市当前的天气情况,最后时候pyinstaller包装成exe文件。
在这里插入图片描述

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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿磊的MD和CFD记录簿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值