通过PyQt5编写天气预报界面程序 您所在的位置:网站首页 获取天气信息及时提醒的程序 通过PyQt5编写天气预报界面程序

通过PyQt5编写天气预报界面程序

2023-09-11 07:47| 来源: 网络整理| 查看: 265

1:通过Qt Designer设计WeatherWin.ui界面

a:找到Library/bin下的Qt设计器,点击designer,启动Qt设计器

2

b:启动之后是这样的

1 标题

c:新建窗体中选第一个,然后创建好后如下。

d:上图界面的所有控件详细清单如下:

e:weatherComboBox中的详细信息如下:

f:定义好两个按钮的信号与槽函数如下

g:两个槽函数要自己定义好,如下

h:WeatherWin.ui界面代码如下:

Dialog 0 0 581 358 Dialog 100 310 75 23 查询 420 310 75 23 清空 50 110 491 181 20 10 81 16 查询城市天气 40 40 61 16 城市 true 100 40 361 22 true true 上海 北京 天津 桂林 QueryBtn clicked() Dialog QueryWeather() 121 314 219 335 clearBtn clicked() Dialog clearResult() 437 319 512 338 QueryWeather() clearResult() 2:设计好界面后,通过pyuic5将界面.ui文件转换为.py文件

具体实现细节如下:

a:将设计好的ui文件放到指定位置,通过以下命令转换为.py文件

pyuic5 -o WeatherWin.py WeatherWin.ui

转换好的WeatherWin.py文件如下:

b:WeatherWin.py代码如下:

# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'WeatherWin.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_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(581, 358) self.QueryBtn = QtWidgets.QPushButton(Dialog) self.QueryBtn.setGeometry(QtCore.QRect(100, 310, 75, 23)) self.QueryBtn.setObjectName("QueryBtn") self.clearBtn = QtWidgets.QPushButton(Dialog) self.clearBtn.setGeometry(QtCore.QRect(420, 310, 75, 23)) self.clearBtn.setObjectName("clearBtn") self.resultText = QtWidgets.QTextEdit(Dialog) self.resultText.setGeometry(QtCore.QRect(50, 110, 491, 181)) self.resultText.setObjectName("resultText") self.label = QtWidgets.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(20, 10, 81, 16)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(Dialog) self.label_2.setGeometry(QtCore.QRect(40, 40, 61, 16)) self.label_2.setObjectName("label_2") self.weatherComboBox = QtWidgets.QComboBox(Dialog) self.weatherComboBox.setEnabled(True) self.weatherComboBox.setGeometry(QtCore.QRect(100, 40, 361, 22)) self.weatherComboBox.setEditable(True) self.weatherComboBox.setDuplicatesEnabled(True) self.weatherComboBox.setObjectName("weatherComboBox") self.weatherComboBox.addItem("") self.weatherComboBox.addItem("") self.weatherComboBox.addItem("") self.weatherComboBox.addItem("") self.retranslateUi(Dialog) self.QueryBtn.clicked.connect(Dialog.QueryWeather) self.clearBtn.clicked.connect(Dialog.clearResult) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.QueryBtn.setText(_translate("Dialog", "查询")) self.clearBtn.setText(_translate("Dialog", "清空")) self.label.setText(_translate("Dialog", "查询城市天气")) self.label_2.setText(_translate("Dialog", "城市")) self.weatherComboBox.setItemText(0, _translate("Dialog", "上海")) self.weatherComboBox.setItemText(1, _translate("Dialog", "北京")) self.weatherComboBox.setItemText(2, _translate("Dialog", "天津")) self.weatherComboBox.setItemText(3, _translate("Dialog", "桂林")) 3:调用主窗口类,写好两个自定义槽函数,以便WeatherWin.py的两个按钮触发点击信号与两个槽函数进行绑定

CallWeatherWin.py代码如下:

# -*- coding: utf-8 -*- import sys from PyQt5.QtWidgets import QApplication, QMainWindow from WeatherWin import Ui_Dialog import requests class MainWindow(QMainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.ui = Ui_Dialog() self.ui.setupUi(self) #请求天气信息 def QueryWeather(self): print('* queryWeather ') cityName = self.ui.weatherComboBox.currentText() #获取当前下拉列表框中的文本信息(城市名字) cityCode = self.transCityName(cityName) #获取城市代码(通过函数调用) #请求城市天气信息通过城市代码的链接 rep = requests.get('http://www.weather.com.cn/data/sk/' + cityCode + '.html') rep.encoding = 'utf-8' #网页编码信息 print(rep.json()) #打印json网页信息 msg1 = '城市: %s' % rep.json()['weatherinfo']['city'] + '\n' msg2 = '风向: %s' % rep.json()['weatherinfo']['WD'] + '\n' msg3 = '温度: %s' % rep.json()['weatherinfo']['temp'] + ' 度' + '\n' msg4 = '风力: %s' % rep.json()['weatherinfo']['WS'] + '\n' msg5 = '湿度: %s' % rep.json()['weatherinfo']['SD'] + '\n' result = msg1 + msg2 + msg3 + msg4 + msg5 self.ui.resultText.setText(result)#将请求的信息放入resultText def transCityName(self, cityName): cityCode = '' if cityName == '北京': cityCode = '101010100' elif cityName == '天津': cityCode = '101030100' elif cityName == '上海': cityCode = '101020100' elif cityName == '桂林': cityCode='101300501' return cityCode def clearResult(self): print('* clearResult ') self.ui.resultText.clear() if __name__ == "__main__": app = QApplication(sys.argv) win = MainWindow() win.show() sys.exit(app.exec_()) 4:最后直接运行CallWeatherWin.py

运行后单击查询按钮效果如下:

下拉列表框选择桂林,单击查询可以知道明天下雨的概率为73%,说明明天下雨可能性很大,加油,PyQt初级开发者。如果你在自己做的过程中发现bug可以给我回复,一定好好解答,加油~。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有