国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - Python腳本制作天氣查詢實例代碼

Python腳本制作天氣查詢實例代碼

2021-12-17 00:40小妮淺淺 Python

在本篇文章里小編給大家整理的是一篇關于Python腳本制作天氣查詢實例代碼實例,有興趣的朋友們可以參考學習下。

獲取天氣的主要代碼如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# cityCode 替換為具體某一個城市的對應編號
# 1、發送請求,獲取數據
url = f'http://wthrcdn.etouch.cn/weather_mini?citykey={cityCode}'
res = requests.get(url)
res.encoding = 'utf-8'
res_json = res.json()
 
# 2、數據格式化
data = res_json['data']
city = f"城市:{data['city']}\n"
# 字符串格式化的一種方式 f"{}" 通過字典傳遞值
 
today = data['forecast'][0]
date = f"日期:{today['date']}\n"  # \n 換行
now = f"實時溫度:{data['wendu']}度\n"
temperature = f"溫度:{today['high']} {today['low']}\n"
fengxiang = f"風向:{today['fengxiang']}\n"
type = f"天氣:{today['type']}\n"
tips = f"貼士:{data['ganmao']}\n"
 
result = city + date + now + temperature + fengxiang + type + tips
 
print(result)

1、使用Qt Designer繪制窗口,保存為ui文件

2、把ui文件轉為py文件

(1)在生成的ui文件目錄下,打開cmd

(2)輸入以下命令(注意替換名稱)

pyuic5 -o destination.py source.ui

3、信號與槽函數的連接

?
1
2
3
4
5
# 1、清空按鈕與對應函數連接
clearBtn.clicked.connect(widget.clearResult)
 
# 2、查詢按鈕與對應函數連接
queryBtn.clicked.connect(widget.queryWeather)

4、調用主窗口類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import sys    
from PyQt5.QtWidgets import QApplication , QMainWindow
from WeatherWin import Ui_widget
import requests
import json
 
class MainWindow(QMainWindow ):
    def __init__(self, parent=None):   
        super(MainWindow, self).__init__(parent)
        self.ui = Ui_widget()
        self.ui.setupUi(self)
 
        # 通過文本框傳入想要搜索的城市名稱:天津
        cityName = self.ui.weatherComboBox.currentText()
 
        # 獲取天氣部分省略
 
        # 在文本框顯示查詢結果
        self.ui.resultText.setText(result)
 
    def clearResult(self):
        print('* clearResult  ')
        self.ui.resultText.clear() 
 
if __name__=="__main__"
    app = QApplication(sys.argv) 
    win = MainWindow() 
    win.show() 
    sys.exit(app.exec_())

代碼擴展:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from tkinter import *
import urllib.request
import gzip
import json
from tkinter import messagebox
root = Tk()
def main():
  # 輸入窗口
  root.title('Python學習交流群:973783996') # 窗口標題
  Label(root, text='請輸入城市').grid(row=0, column=0) # 設置標簽并調整位置
  enter = Entry(root) # 輸入框
  enter.grid(row=0, column=1, padx=20, pady=20) # 調整位置
  enter.delete(0, END) # 清空輸入框
  enter.insert(0, 'Python學習交流群:973783996') # 設置默認文本
  # enter_text = enter.get()#獲取輸入框的內容
   running = 1
   def get_weather_data(): # 獲取網站數據
    city_name = enter.get() # 獲取輸入框的內容
    url1 = 'http://wthrcdn.etouch.cn/weather_mini?city=' + urllib.parse.quote(city_name)
    url2 = 'http://wthrcdn.etouch.cn/weather_mini?citykey=101010100'
    # 網址1只需要輸入城市名,網址2需要輸入城市代碼
    # print(url1)
    weather_data = urllib.request.urlopen(url1).read()
    # 讀取網頁數據
    weather_data = gzip.decompress(weather_data).decode('utf-8')
    # 解壓網頁數據
    weather_dict = json.loads(weather_data)
    # 將json數據轉換為dict數據
    if weather_dict.get('desc') == 'invilad-citykey':
      print(messagebox.askokcancel("xing", "你輸入的城市名有誤,或者天氣中心未收錄你所在城市"))
    else:
      # print(messagebox.askokcancel('xing','bingguo'))
      show_data(weather_dict, city_name)  def show_data(weather_dict, city_name): # 顯示數據
    forecast = weather_dict.get('data').get('forecast') # 獲取數據塊
    root1 = Tk() # 副窗口
    root1.geometry('650x280') # 修改窗口大小
    root1.title(city_name + '天氣狀況') # 副窗口標題
     # 設置日期列表
    for i in range(5): # 將每一天的數據放入列表中
      LANGS = [(forecast[i].get('date'), '日期'),
           (forecast[i].get('fengxiang'), '風向'),
           (str(forecast[i].get('fengji')), '風級'),
           (forecast[i].get('high'), '最高溫'),
           (forecast[i].get('low'), '最低溫'),
           (forecast[i].get('type'), '天氣')]
      group = LabelFrame(root1, text='天氣狀況', padx=0, pady=0) # 框架
      group.pack(padx=11, pady=0, side=LEFT) # 放置框架
      for lang, value in LANGS: # 將數據放入框架中
        c = Label(group, text=value + ': ' + lang)
        c.pack(anchor=W)
    Label(root1, text='今日' + weather_dict.get('data').get('ganmao'),
       fg='green').place(x=40, y=20, height=40) # 溫馨提示
    Label(root1, text="StarMan: 49star.com", fg="green", bg="yellow").place(x=10, y=255, width=125,                              height=20) # 作者網站
    Button(root1, text='確認并退出', width=10, command=root1.quit).place(x=500, y=230, width=80, height=40) # 退出按鈕
    root1.mainloop()
  # 布置按鍵
  Button(root, text="確認", width=10, command=get_weather_data) \
    .grid(row=3, column=0, sticky=W, padx=10, pady=5)
  Button(root, text='退出', width=10, command=root.quit) \
    .grid(row=3, column=1, sticky=E, padx=10, pady=5)
  if running == 1:
    root.mainloop()
 if __name__ == '__main__':
  main()

到此這篇關于Python腳本制作天氣查詢實例代碼的文章就介紹到這了,更多相關Python腳本如何制作天氣查詢內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://www.py.cn/jishu/gaoji/32261.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 精品国偷自产在线 | 在线观看中文字幕av | 久久男人的天堂 | 国产精品免费一区二区三区四区 | 欧美一级高清免费 | av电影免费观看 | 日本一区二区免费视频 | 免费一区 | 亚洲高清视频在线 | 午夜影院在线播放 | 亚洲精品高潮呻吟久久av | 狠久久 | 日本激情视频 | 黄色网页在线 | 婷婷色av | 青娱乐网 | 亚洲精品欧美在线 | 亚洲一区二区在线视频 | 久久国产精品久久精品国产演员表 | 亚洲激情精品 | av一区二区在线观看 | 国产视频三区 | 四虎免费紧急入口观看 | 精品一区二区三区免费毛片 | 性视屏 | 一级毛片视频 | 国产成人网 | 久久久91精品国产一区二区三区 | 欧美视频一区二区 | 超碰97国产精品人人cao | 午夜视频在线 | 久久在线视频 | 人人干天天干 | 女人高潮特级毛片 | 韩国精品一区二区 | 一级黄色毛片 | av资源中文在线 | 日本中文字幕在线免费观看 | 久久久久久久一区 | 亚洲综合自拍 | 日韩精品网站 |