天气查询软件(基于python的pyqt5)

import sys
import os
import requests
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
QInputDialog, QApplication)
from PyQt5.QtWidgets import QWidget, QMessageBox, QApplication
from PyQt5 import QtCore, QtGui, QtWidgets
class Example(QWidget):

def __init__(self):
super().__init__()

self.initUI()

def initUI(self):
#qbtn = QPushButton('退出', self)
#qbtn.clicked.connect(QCoreApplication.instance().quit)
#qbtn.resize(qbtn.sizeHint())
#qbtn.move(20, 200)
##退出按钮
self.btn = QPushButton('输入城市名', self)
self.btn.move(20, 20)
self.btn.clicked.connect(self.showDialog)
##输入城市按钮
self.le = QLineEdit(self)
#QLineEdit
self.le.move(120, 100)#(X,Y)原点为左上角
self.le.resize(220, 80)#(宽,长)
#self.le.setWordWrap(True)
##展示窗口
todaytn = QPushButton('今日天气查询', self)
todaytn.clicked.connect(self.qiwen)
todaytn.move(20,100)
todaytn = QPushButton('明日天气查询', self)
todaytn.clicked.connect(self.toqiwen)
todaytn.move(20,150)
##天气查询
self.setGeometry(150, 100, 460, 350)
self.setWindowTitle('天气查询')
self.show()
##主界面
def closeEvent(self, event):

reply = QMessageBox.question(self, '信息',
"确定退出?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)

if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def showDialog(self):
global text
text, ok = QInputDialog.getText(self, '输入城市名',
'两个字符')

if ok:
#self.le.setText(str(text))
pass
def qiwen(self):
try:
r = requests.get("http://wthrcdn.etouch.cn/weather_mini?city="+text)
except Exception as e:
r = requests.get("http://wthrcdn.etouch.cn/weather_mini?city=南京")
else:
pass
#print(r.text)
resultjson = r.json()
todayqiwen= resultjson['data']['forecast'][0]
tomorowqiwen = resultjson['data']['forecast'][1]
today= todayqiwen['date']
todayhighwendu = todayqiwen['high']
todaylowwendu = todayqiwen['low']
todayfengxiang = todayqiwen['fengxiang']
todayfengli = todayqiwen['fengli']
todaytype = todayqiwen['type']
self.le.setText(str(today)+str(todaylowwendu)+str(todayhighwendu)+str(todaytype))
def toqiwen(self):
try:
r2 = requests.get("http://wthrcdn.etouch.cn/weather_mini?city="+text)
except Exception as e:
r2 = requests.get("http://wthrcdn.etouch.cn/weather_mini?city=南京")
else:
pass
#print(r.text)
resultjson = r2.json()
tomorowqiwen = resultjson['data']['forecast'][1]
today= tomorowqiwen['date']
todayhighwendu = tomorowqiwen['high']
todaylowwendu = tomorowqiwen['low']
todayfengxiang = tomorowqiwen['fengxiang']
todayfengli = tomorowqiwen['fengli']
todaytype = tomorowqiwen['type']
self.le.setText(str(today)+str(todaylowwendu)+str(todayhighwendu)+str(todaytype))
#print(todayqiwen)
#print(tomorowqiwen)
#print(today)
if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

猜你喜欢

转载自www.cnblogs.com/zhujunsheng/p/10873767.html