【PySimpleGUI】- 天气预报小程序

声明:文章为自己笔记,目的仅作学习与回顾,如有侵权请文后留言,会尽快删除

参考自链接:https://blog.csdn.net/zc666ying/article/details/105945712 CSDN链接

代码

# 参考链接:https://blog.csdn.net/zc666ying/article/details/105945712
import json
import requests
import PySimpleGUI as sg    # pip install pysimplegui

def get_weather(city,key):
    r = requests.get('http://wthrcdn.etouch.cn/weather_mini?city=' + city)
    text = json.loads(r.text)
    dict = text['data']['forecast'][0]
    result = dict[str(key)]
    return result


if __name__ == '__main__':
    # 让所有文本居中
    sg.SetOptions(text_justification='center')

	# 整体的布局是从上而下,同一列表中为从左往右
	# 创建视图窗口,用二维列表 list 表示,用来存放组件
	# Text , Input 等是PySimpleGUI中的类
    layout = [
         # key 值代表了是这个输入框输入的值,以字典的 键 值来表示,这个用于找到输入框并跟新它的值
        [sg.Text('城市', size=(20, 1)), sg.Input(key='CITY')],
        [sg.Text('日期', size=(20, 1)), sg.Input(key='DATE')],
        [sg.Text('最高气温', size=(20, 1)), sg.Input(key='HIGH')],
        [sg.Text('风力', size=(20, 1)), sg.Input(key='FENGLI')],
        [sg.Text('最低气温', size=(20, 1)), sg.Input(key='LOW')],
        [sg.Text('风向', size=(20, 1)), sg.Input(key='FENGXIANG')],
        [sg.Text('天气', size=(20, 1)), sg.Input(key='TYPE')],
        [sg.Button('搜索')],
        [sg.Button('Exit', size=(10, 1))]
    ]
	# 创建窗口,将视窗放在窗口中 ‘天气小助手’为窗口的名字
    window = sg.Window('天气小助手', layout, location=(450, 200))

    while True:
        # 获取输入框和按钮元素的值
        event, values = window.read()
        ### 5. 退出系统
        if event == 'Exit' or event is None:
            break
        # 得到城市这个输入框的值
        city = values['CITY']
        #以下六行代码均根据 key 值进行定位输入框
        date = get_weather(city,'date')
        high = get_weather(city,'high')
        fengli = get_weather(city,'fengli')
        low = get_weather(city,'low')
        fengxiang = get_weather(city,'fengxiang')
        type = get_weather(city,'type')
        #更新输入框中的值
        weather_date = window['DATE'].update(date)
        weather_high = window['HIGH'].update(high)
        weather_fengli = window['FENGLI'].update(fengli)
        weather_low = window['LOW'].update(low)
        weather_fengxiang = window['FENGXIANG'].update(fengxiang)
        weather_type = window['TYPE'].update(type)

        # # 找到天气输入框
        # weather_wind = window['TYPE']
        # # 将天气更新到输入框
        # weather_wind.update(type)

        # 显示窗口
        window.read()

    window.close()

效果图

PS:在图中城市一栏输入所要查询的城市,联网状态下即可访问互联网并在查询后返回结果。

在这里插入图片描述

第一章:数据结构和算法 13 1.1 解压序列赋值给多个变量 . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.2 解压可迭代对象赋值给多个变量 . . . . . . . . . . . . . . . . . . . . . . . 14 1.3 保留最后 N 个元素 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 1.4 查找最大或最小的 N 个元素 . . . . . . . . . . . . . . . . . . . . . . . . . 19 1.5 实现一个优先级队列 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 1.6 字典中的键映射多个值 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 1.7 字典排序 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 1.8 字典的运算 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 1.9 查找两字典的相同点 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 1.10 删除序列相同元素并保持顺序 . . . . . . . . . . . . . . . . . . . . . . . . 28 1.11 命名切片 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 1.12 序列中出现次数最多的元素 . . . . . . . . . . . . . . . . . . . . . . . . . 31 1.13 通过某个关键字排序一个字典列表 . . . . . . . . . . . . . . . . . . . . . 32 1.14 排序不支持原生比较的对象 . . . . . . . . . . . . . . . . . . . . . . . . . 34 1.15 通过某个字段将记录分组 . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 1.16 过滤序列元素 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37 1.17 从字典中提取子集 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 1.18 映射名称到序列元素 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 1.19 转换并同时计算数据 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 1.20 合并多个字典或映射 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43 第二章:字符串和文本 47 2.1 使用多个界定符分割字符串 . . . . . . . . . . . . . . . . . . . . . . . . . . 47 2.2 字符串开头或结尾匹配 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 2.3 用 Shell 通配符匹配字符串 . . . . . . . . . . . . . . . . . . . . . . . . . . 50 2.4 字符串匹配和搜索 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 2.5 字符串搜索和替换 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 2.6 字符串忽略大小写的搜索替换 . . . . . . . . . . . . . . . . . . . . . . . . . 56 2.7 最短匹配模式 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 2.8 多行匹配模式 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 2.9 将 Unicode 文本标准化 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 2.10 在正则式中使用 Unicode . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 2.11 删除字符串中不需要的字符 . . . . . . . . . . . . . . . . . . . . . . . . . 61 2.12 审查清理文本字符串 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 2.13 字符串对齐 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 2.14 合并拼接字符串 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 2.15 字符串中插入变量 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 2.16 以指定列宽格式化字符串 . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 2.17 在字符串中处理 html 和 xml . . . . . . . . . . . . . . . . . . . . . . . . . 73 2.18 字符串令牌解析 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 2.19 实现一个简单的递归下降分析器 . . . . . . . . . . . . . . . . . . . . . . . 77 2.20 字节字符串上的字符串操作 . . . . . . . . . . . . . . . . . . . . . . . . . 85 第三章:数字日期和时间 88 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值