0%

不务正业系列:天气预报(偶尔更新)

天气预报

这是一个小项目,就是某本书上的一个例题

原理为:

  • requests 获取中国天气官网地址的 web API
  • 使用 Python 进行打印

样例代码为:

1
2
3
4
5
6
7
8
9
10
11
import requests

rep=requests.get('http://www.weather.com.cn/data/sk/101270101.html') # 成都,101270101

rep.encoding='utf-8'
print('返回结果: %s' % rep.json() )
print('城市: %s' %rep.json()['weatherinfo']['city'])
print('风向: %s' % rep.json()['weatherinfo']['WD'])
print('温度: %s' % rep.json()['weatherinfo']['temp']+ " 度")
print('风力: %s' % rep.json()['weatherinfo']['WS'])
print('湿度: %s' % rep.json()['weatherinfo']['SD'])

CallWeatherWin_V1.0

这个小程序是为了桌宠写的,我会把它添加到桌宠的右键菜单中

目前只添加了两个城市,如果有需要可以在网上搜索城市代码,自行添加

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import requests

class Mainweather(QWidget):
def __init__(self,parent=None,**kwargs):
super(Mainweather, self).__init__(parent)
for key, value in kwargs.items():
if hasattr(self.cfg, key): setattr(self.cfg, key, value)
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.SubWindow)
self.ui = UI_Form()
self.ui.setupUI(self)

def queryWeather(self):
print('* queryWeather ')
cityName = self.ui.weatherComboBox.currentText()
cityCode = self.transCityName(cityName)

rep = requests.get('http://www.weather.com.cn/data/sk/'+cityCode+'.html')
rep.encoding = 'utf-8'
print(rep.json())

msg1 = ('城市: %s' % rep.json()['weatherinfo']['city'])+'\n'
msg2 = ('风向: %s' % rep.json()['weatherinfo']['WD'])+'\n'
msg3 = ('温度: %s' % rep.json()['weatherinfo']['temp'] + " 度")+'\n'
msg4 = ('风力: %s' % rep.json()['weatherinfo']['WS'])+'\n'
msg5 = ('湿度: %s' % rep.json()['weatherinfo']['SD'])+'\n'

result = msg1+msg2+msg3+msg4+msg5
self.ui.resultText.setText(result)

def transCityName(self,cityName):
cityCode = ''
if cityName == '成都':
cityCode = '101270101'
elif cityName == '南充':
cityCode = '101270501'
return cityCode

def clearResult(self):
print('* clearResult ')
self.ui.resultText.clear()

def quitWindows(self):
print('* quitWindows ')
result = "由于本项目不完善,请右键桌宠进行关闭"
self.ui.resultText.setText(result)
self.close()

class UI_Form(object):
def setupUI(self,Form):
Form.setObjectName("Form")
Form.resize(450,350)

self.groupBox = QtWidgets.QGroupBox(Form)
self.groupBox.setGeometry(QtCore.QRect(10,10,431,251))
self.groupBox.setObjectName("groupBox")
self.weatherComboBox = QtWidgets.QComboBox(self.groupBox)
self.weatherComboBox.setGeometry(QtCore.QRect(80,30,221,21))
self.weatherComboBox.addItem("")
self.weatherComboBox.addItem("")
self.resultText = QtWidgets.QTextEdit(self.groupBox)
self.resultText.setGeometry(QtCore.QRect(10,60,411,181))
self.resultText.setObjectName("resultText")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(20,20,72,21))
self.label.setObjectName("laber")
self.okButton = QtWidgets.QPushButton(Form)
self.okButton.setGeometry(QtCore.QRect(50,300,93,28))
self.okButton.setObjectName("okButton")
self.clearButton = QtWidgets.QPushButton(Form)
self.clearButton.setGeometry(QtCore.QRect(160,300,93,28))
self.clearButton.setObjectName("clearButton")
self.quitButton = QtWidgets.QPushButton(Form)
self.quitButton.setGeometry(QtCore.QRect(270,300,93,28))
self.quitButton.setObjectName("quitButton")

self.retranslateUI(Form)
self.okButton.clicked.connect(Form.queryWeather)
self.clearButton.clicked.connect(Form.clearResult)
self.quitButton.clicked.connect(Form.quitWindows)

#QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUI(self,Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form","天气预报"))

self.groupBox.setTitle(_translate("Form","查询城市天气"))
self.weatherComboBox.setItemText(0,_translate("Form","成都"))
self.weatherComboBox.setItemText(1,_translate("Form","南充"))
self.label.setText(_translate("Form","城市"))
self.okButton.setText(_translate("Form","查询"))
self.clearButton.setText(_translate("Form","清空"))
self.quitButton.setText(_translate("Form","退出"))

if __name__=='__main__':
app = QApplication(sys.argv)
win = Mainweather()
win.show()
sys.exit(app.exec_())

更新日志:

  • version:v1.0

  • date:2022.5.12

  • type:

    • Features:NULL
    • Changed:NULL
    • Removed:NULL
  • desc:

    • 第一代版本,后续考虑添加更换壁纸的功能

CallWeatherWin_V1.1

第二代版本主要是进行了美化操作,功能没有太大改变

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
import requests

class Mainweather(QWidget):
def __init__(self,parent=None):
super(Mainweather, self).__init__(parent)
self.setWindowFlags(Qt.SubWindow)
self.ui = UI_Form()
self.ui.setupUI(self)

def queryWeather(self):
print('* queryWeather ')
cityName = self.ui.weatherComboBox.currentText()
cityCode = self.transCityName(cityName)

rep = requests.get('http://www.weather.com.cn/data/sk/'+cityCode+'.html')
rep.encoding = 'utf-8'
print(rep.json())

msg1 = ('城市: %s' % rep.json()['weatherinfo']['city'])+'\n'
msg2 = ('风向: %s' % rep.json()['weatherinfo']['WD'])+'\n'
msg3 = ('温度: %s' % rep.json()['weatherinfo']['temp'] + " 度")+'\n'
msg4 = ('风力: %s' % rep.json()['weatherinfo']['WS'])+'\n'
msg5 = ('湿度: %s' % rep.json()['weatherinfo']['SD'])+'\n'

result = msg1+msg2+msg3+msg4+msg5
self.ui.resultText.setText(result)

def transCityName(self,cityName):
cityCode = ''
if cityName == '成都':
cityCode = '101270101'
elif cityName == '南充':
cityCode = '101270501'
return cityCode

def clearResult(self):
print('* clearResult ')
self.ui.resultText.clear()

def quitWindows(self):
print('* quitWindows ')
self.close()

class UI_Form(object):
def setupUI(self,Form):
Form.setObjectName("Form")
Form.resize(450,350)

self.groupBox = QtWidgets.QGroupBox(Form)
self.groupBox.setGeometry(QtCore.QRect(10,10,250,250))
self.groupBox.setObjectName("groupBox")
self.groupBox.setStyleSheet("color:white")
self.weatherComboBox = QtWidgets.QComboBox(self.groupBox)
self.weatherComboBox.setGeometry(QtCore.QRect(80,30,140,21))
self.weatherComboBox.addItem("")
self.weatherComboBox.addItem("")
self.weatherComboBox.setStyleSheet("color:black")
self.resultText = QtWidgets.QTextEdit(self.groupBox)
self.resultText.setGeometry(QtCore.QRect(10,60,220,181))
self.resultText.setObjectName("resultText")
self.resultText.setFont(QFont("微软雅黑",12,QFont.Bold))
self.resultText.setStyleSheet("color:black")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(20,20,72,21))
self.label.setObjectName("laber")
self.okButton = QtWidgets.QPushButton(Form)
self.okButton.setGeometry(QtCore.QRect(50,300,93,28))
self.okButton.setObjectName("okButton")
self.clearButton = QtWidgets.QPushButton(Form)
self.clearButton.setGeometry(QtCore.QRect(160,300,93,28))
self.clearButton.setObjectName("clearButton")
self.quitButton = QtWidgets.QPushButton(Form)
self.quitButton.setGeometry(QtCore.QRect(270,300,93,28))
self.quitButton.setObjectName("quitButton")

self.retranslateUI(Form)
self.okButton.clicked.connect(Form.queryWeather)
self.clearButton.clicked.connect(Form.clearResult)
self.quitButton.clicked.connect(Form.quitWindows)

QtCore.QMetaObject.connectSlotsByName(Form)

def retranslateUI(self,Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form","天气预报"))
self.groupBox.setTitle(_translate("Form","查询城市天气"))
self.weatherComboBox.setItemText(0,_translate("Form","成都"))
self.weatherComboBox.setItemText(1,_translate("Form","南充"))
self.label.setText(_translate("Form","城市"))
self.okButton.setText(_translate("Form","查询"))
self.clearButton.setText(_translate("Form","清空"))
self.quitButton.setText(_translate("Form","退出"))

if __name__=='__main__':
app = QApplication(sys.argv)
win = Mainweather()
palette = QPalette()
palette.setBrush(QPalette.Background, QBrush(QPixmap("D:\PythonProject\images\YHellow.png")))
win.setPalette(palette)
win.show()

sys.exit(app.exec_())

更新日志:

  • version:v1.1

  • date:2022.5.13

  • type:

    • Features:
      • 导入了图片背景
      • 修改了字体和颜色
    • Changed:
      • 对部分代码进行了调整,使其更易读
    • Removed:NULL
  • desc:

    • 目前该项目以完善,在很长一段时间里可能都不会再碰该项目