自己写的一个pytq5查询天气的小程序[Python] 纯文本查看 复制代码 import re
import sys
import requests
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QLineEdit, QTextEdit, QVBoxLayout, QHBoxLayout
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.setGeometry(300, 300, 300, 300)
self.setWindowTitle('天气查询')
self.city_label = QLabel('地区:')
self.line = QLineEdit()
QH = QHBoxLayout()
QH.addWidget(self.city_label)
QH.addWidget(self.line)
self.text_edit = QTextEdit(self)
self.text_edit.move(20, 45)
self.inquire_button = QPushButton('查询', self)
self.inquire_button.clicked.connect(self.inquire_event)
self.empty_button = QPushButton('清空', self)
self.empty_button.clicked.connect(self.clearResult)
QH1 = QHBoxLayout()
QH1.addWidget(self.inquire_button)
QH1.addWidget(self.empty_button)
QV = QVBoxLayout()
QV.addLayout(QH)
QV.addWidget(self.text_edit)
QV.addLayout(QH1)
self.setLayout(QV)
self.show()
def inquire_event(self):
cityName = self.line.text()
res = requests.get('http://wthrcdn.etouch.cn/weather_mini?city=%s' % cityName).json()
forecast = res.get('data').get('forecast')
result = ''
for item in forecast:
fengli = re.findall("CDATA\[(.*?)\]", item.get('fengli'))[0]
str_item = f'''
-------------------------
日期星期 {item.get('date')}
{item.get('high')}
{item.get('low')}
风向 {item.get('fengxiang')}
风力 {fengli}
天气 {item.get('type')}'''
result += str_item
self.text_edit.setText(result)
def clearResult(self):
self.text_edit.clear()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
演示
演示
|