python 天氣查詢帶GUI介面

2021-10-24 04:25:07 字數 3538 閱讀 6123

乙個簡單python 通過api查詢全國城市的天氣gui帶介面的查詢

檢視已經安裝的模組(包)命令,pip list:

若未安裝tkinter

pip install tkinter
還用到

tkinter介面設計**如下

root = tk()

root.title('hello')

canvas = canvas(root, height=height, width=width)

canvas.pack()

frame = frame(root, bg='#80c1ff', bd=5)

frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')

button = button(frame, text='get weather', font=40, command=get_weather)

button.place(relx=0.7, relwidth=0.3, relheight=1)

entry = entry(frame, font=40)

entry.place(relwidth=0.65, relheight=1)

lower_frame = frame(root, bg='#80c1ff', bd=10)

lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')

label = label(lower_frame, text="this is a label", font=('宋體', 20), bg='white')

label.place(relwidth=1, relheight=1)

root.mainloop()

python實現查詢**

def format_response(data):

try:

city = data['result']['city']

date = data['result']['date']

week = data['result']['week']

weather = data['result']['weather']

temp = data['result']['templow']

temphigh = data['result']['temphigh']

final_str = 'city: %s \n date: %s \n week: %s \n weather: %s \n temp: %s temphigh: %s' % (

city, date, week, weather, temp, temphigh)

except:

final_str = 'there was a problem retrieving that information !!!'

return final_str

def get_weather():

cityname = entry.get()

data = requests.get(url).text

data = json.loads(data)

label['text'] = format_response(data)

全部**如下:

#!/usr/bin/env python

# -*- coding: utf-8 -*-

from tkinter import *

import requests

import json

import random

height = 700

width = 800

def format_response(data):

try:

city = data['result']['city']

date = data['result']['date']

week = data['result']['week']

weather = data['result']['weather']

temp = data['result']['templow']

temphigh = data['result']['temphigh']

final_str = 'city: %s \n date: %s \n week: %s \n weather: %s \n temp: %s temphigh: %s' % (

city, date, week, weather, temp, temphigh)

except:

final_str = 'there was a problem retrieving that information !!!'

return final_str

def get_weather():

cityname = entry.get()

data = requests.get(url).text

data = json.loads(data)

label['text'] = format_response(data)

root = tk()

root.title('hello')

canvas = canvas(root, height=height, width=width)

canvas.pack()

frame = frame(root, bg='#80c1ff', bd=5)

frame.place(relx=0.5, rely=0.1, relwidth=0.75, relheight=0.1, anchor='n')

button = button(frame, text='get weather', font=40, command=get_weather)

button.place(relx=0.7, relwidth=0.3, relheight=1)

entry = entry(frame, font=40)

entry.place(relwidth=0.65, relheight=1)

lower_frame = frame(root, bg='#80c1ff', bd=10)

lower_frame.place(relx=0.5, rely=0.25, relwidth=0.75, relheight=0.6, anchor='n')

label = label(lower_frame, text="this is a label", font=('宋體', 20), bg='white')

label.place(relwidth=1, relheight=1)

root.mainloop()

天氣查詢介面如下

python天氣查詢 python天氣查詢

主要通過字典查詢全國各地主要城市天氣情況 from urllib.request import urlopen from bs4 import beautifulsoup import re citylist main 全國主要城市 北上廣深,華北,東北,華南,西北,西南,華東,華中,def cit...

python中文及符號檢測工具帶GUI介面

import tkinter import webbrowser import re 本程式是乙個中文字元和中文檢測工具 中文字元自己新增,我只新增了一點 輸入字串,點選檢查文字即可判斷有沒有中文字元 qianxiao996精心製作 win tkinter.tk win.title 中文字元檢測工具...

天氣查詢python小程式

匯入工具庫 import urllib.request import gzip city name input 請輸入要查詢的城市名稱 將城市的中文名字編碼成utf 8字元 urllib.parse.quote city name 生成完整url鏈結 url urllib.parse.quote c...