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)
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_())
|