本文来源吾爱破解论坛
本帖最后由 huhus123 于 2019-1-15 10:42 编辑
感觉PyQt5的学习资源比较少,只能摸着石头过河,简单写个小工具练练手。
窗体文件如下
[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*- # Date:2018-12-12 # # Created by: DaddyHu_CN # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.setWindowModality(QtCore.Qt.NonModal) Form.setEnabled(True) Form.resize(436, 336) Form.setMinimumSize(QtCore.QSize(436, 336)) Form.setMaximumSize(QtCore.QSize(436, 336)) self.groupBox = QtWidgets.QGroupBox(Form) self.groupBox.setGeometry(QtCore.QRect(10, 10, 421, 250)) self.groupBox.setObjectName("groupBox") self.formLayout = QtWidgets.QFormLayout(self.groupBox) self.formLayout.setObjectName("formLayout") self.label = QtWidgets.QLabel(self.groupBox) font = QtGui.QFont() font.setFamily("Agency FB") font.setPointSize(11) self.label.setFont(font) self.label.setObjectName("label") self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label) self.weatherComboBox = QtWidgets.QComboBox(self.groupBox) self.weatherComboBox.setObjectName("weatherComboBox") self.weatherComboBox.addItem("") self.weatherComboBox.addItem("") self.weatherComboBox.addItem("") self.weatherComboBox.addItem("") self.weatherComboBox.addItem("") self.weatherComboBox.addItem("") self.weatherComboBox.addItem("") self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.weatherComboBox) self.resultText = QtWidgets.QTextEdit(self.groupBox) self.resultText.setEnabled(True) self.resultText.setReadOnly(True) self.resultText.setObjectName("resultText") self.formLayout.setWidget(1, QtWidgets.QFormLayout.SpanningRole, self.resultText) self.queryBtn = QtWidgets.QPushButton(Form) self.queryBtn.setGeometry(QtCore.QRect(70, 280, 101, 31)) self.queryBtn.setObjectName("queryBtn") self.clearBtn = QtWidgets.QPushButton(Form) self.clearBtn.setEnabled(True) self.clearBtn.setGeometry(QtCore.QRect(260, 280, 101, 31)) self.clearBtn.setObjectName("clearBtn") self.queryBtn.raise_() self.groupBox.raise_() self.clearBtn.raise_() self.retranslateUi(Form) self.queryBtn.clicked.connect(Form.queryWeather) self.clearBtn.clicked.connect(Form.clearResult) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "天气快速查询")) self.groupBox.setTitle(_translate("Form", "查询城市天气")) self.label.setText(_translate("Form", " 城市")) self.weatherComboBox.setItemText(0, _translate("Form", "北京")) self.weatherComboBox.setItemText(1, _translate("Form", "上海")) self.weatherComboBox.setItemText(2, _translate("Form", "天津")) self.weatherComboBox.setItemText(3, _translate("Form", "重庆")) self.weatherComboBox.setItemText(4, _translate("Form", "郑州")) self.weatherComboBox.setItemText(5, _translate("Form", "长沙")) self.weatherComboBox.setItemText(6, _translate("Form", "乌鲁木齐")) self.queryBtn.setText(_translate("Form", "查询")) self.clearBtn.setText(_translate("Form", "清空")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec_())
核心代码如下
[Python] 纯文本查看 复制代码
from PyQt5.QtWidgets import QApplication, QMainWindow from WeatherWin import Ui_Form import sys from PyQt5.QtCore import * import requests import time from bs4 import BeautifulSoup class MainWindow(QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.ui = Ui_Form() self.ui.setupUi(self) def queryWeather(self): global cityCode cityName = self.ui.weatherComboBox.currentText() cityCode = self.transCityName(cityName) self.thread = WorkThread() self.thread.trigger.connect(self.writeText) self.thread.start() def writeText(self, text): self.ui.resultText.setText(text) def transCityName(self, cityName): cityCode = '' if cityName == '北京': cityCode = '101010100' elif cityName == '上海': cityCode = '101020100' elif cityName == '天津': cityCode = '101030100' elif cityName == '长沙': cityCode = '101250101' elif cityName == '乌鲁木齐': cityCode = '101130101' elif cityName == '重庆': cityCode = '101040100' elif cityName == '郑州': cityCode = '101180101' return cityCode def clearResult(self): self.ui.resultText.clear() class WorkThread(QThread): trigger = pyqtSignal(str) def __init__(self): super(WorkThread, self).__init__() def run(self): global cityCode times = str(time.time()).split('.') times = ''.join(times[0]+times[1][0:3]) headers = { "Connection":'keep-alive', 'Host':'d1.weather.com.cn', 'Referer':'http://www.weather.com.cn/weather1d/%s.shtml' % cityCode, 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36' } url = 'http://d1.weather.com.cn/sk_2d/%s.html?_=%s' % (cityCode, times) rep = requests.get(url, headers=headers) rep = rep.content page = BeautifulSoup(rep, 'lxml') page = page.text.split('=') page = eval(page[1]) oxgan = ['优', '良', '轻度污染', '中度污染', '重度污染', '极度污染'] live = int(page['aqi']) if 0<=live<=50: oxgan = oxgan[0] elif 50<live<=100: oxgan = oxgan[1] elif 100<live<=150: oxgan = oxgan[2] elif 150<live<=200: oxgan = oxgan[3] elif 200<live<=300: oxgan = oxgan[3] elif 300<live<=500: oxgan = oxgan[3] msg1 = '城市: %s\n' % page['cityname'] msg2 = '温度: %s' % page['temp'] + '℃\n' msg3 = '风向: %s\n' % page['WD'] msg4 = '风力: %s\n' % page['WS'] msg5 = '湿度: %s\n' % page['SD'] msg6 = '空气质量: %s ' % page['aqi'] + oxgan + '\n' msg7 = '时间: %s %s\n' % (page['date'], page['time']) msg8 = '\n\n\n\n\t by:DaddyHu_CN' result = msg1 + msg2 + msg3 + msg4 + msg5 + msg6 + msg7 + msg8 self.trigger.emit(result) if __name__ == '__main__': app = QApplication(sys.argv) win = MainWindow() win.show() sys.exit(app.exec_())
涉及简单的爬虫和抓包的知识,都是很简单很简单的,仅供学习探讨。
企业微信截图_15458028296468.png (30.33 KB, 下载次数: 1)
下载附件 保存到相册
运行效果如图
上下分别保存成.py文件运行下面那个即可,需要pyqt5支持,具体需要什么模块请自行安装吧,希望大家都有点动探索精神,都是要学习的别等着别人喂给你,谢谢!
版权声明:
本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。
- 上一篇: python实现开机时自动发送远程电脑ip到指定邮箱
- 下一篇: 一键获取西刺代,理IP