美文网首页
PyQt天气查询Demo

PyQt天气查询Demo

作者: 呱呱呱2 | 来源:发表于2020-04-05 14:57 被阅读0次

0. 效果

image.png

1. UI文件

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

# Form implementation generated from reading ui file '.\queryWeather.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(688, 481)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(10, 20, 661, 381))
        self.groupBox.setObjectName("groupBox")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(40, 40, 51, 21))
        self.label.setObjectName("label")
        self.comboBox = QtWidgets.QComboBox(self.groupBox)
        self.comboBox.setGeometry(QtCore.QRect(110, 40, 121, 22))
        self.comboBox.setObjectName("comboBox")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.comboBox.addItem("")
        self.textEdit = QtWidgets.QTextEdit(self.groupBox)
        self.textEdit.setGeometry(QtCore.QRect(10, 80, 641, 291))
        self.textEdit.setObjectName("textEdit")
        self.queryBtn = QtWidgets.QPushButton(self.centralwidget)
        self.queryBtn.setGeometry(QtCore.QRect(10, 410, 93, 28))
        self.queryBtn.setObjectName("queryBtn")
        self.clearBtn = QtWidgets.QPushButton(self.centralwidget)
        self.clearBtn.setGeometry(QtCore.QRect(130, 410, 93, 28))
        self.clearBtn.setObjectName("clearBtn")
        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)
        self.queryBtn.clicked.connect(MainWindow.queryWeather)
        self.clearBtn.clicked.connect(MainWindow.clearWeather)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.groupBox.setTitle(_translate("MainWindow", "查询城市天气"))
        self.label.setText(_translate("MainWindow", "城市"))
        self.comboBox.setItemText(0, _translate("MainWindow", "北京"))
        self.comboBox.setItemText(1, _translate("MainWindow", "上海"))
        self.comboBox.setItemText(2, _translate("MainWindow", "深圳"))
        self.queryBtn.setText(_translate("MainWindow", "查询"))
        self.clearBtn.setText(_translate("MainWindow", "清空"))


2. 逻辑处理代码

import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
from queryWeather import Ui_MainWindow
import requests


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
    def queryWeather(self):
        print('* queryWeather')
        cityName = self.ui.comboBox.currentText()
        cityCode = self.transCityName(cityName)
        url = 'http://www.weather.com.cn/data/sk/'+cityCode+'.html'
        rep = requests.get(url)
        rep.encoding = 'utf-8'
        #print( rep.json() )
        self.ui.textEdit.setText(str(rep.json()))
    def transCityName(self, cityName):
        cityDic = {'北京':'101010100','天津':'101030100','深圳':'101280601'}
        return cityDic[cityName]
    def clearWeather(self):
        self.ui.textEdit.clear()

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

相关文章

网友评论

      本文标题:PyQt天气查询Demo

      本文链接:https://www.haomeiwen.com/subject/bduuphtx.html