【原创源码】【Python】PyQt天气查询
自己写的一个pytq5查询天气的小程序import reimport 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'))
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_())
pyQT,这个是我见过代码最少的一个成品。厉害啦! 不错啊 刚好哪来学习下 学习了感谢 分享 没用过这个,收藏学习一下 不错,学习一下 pyqt好复杂,看不懂,这代码写的好简洁,学习一下 这不是一本书里的例子吗?
页:
[1]