天气信息由天气网抓取在子程序 def Refrsh_weatherData(city)中
url = 'http://t.weather.sojson.com/api/weather/city/' + city_code[city]
res = requests.get(url).json()
建立了6个子窗口类显示当前几日的天气情况,窗口内建立槽函数,当切换城市时自动更新各窗口内的数据
class ForeCast(QWidget):
data_signal = pyqtSignal(object, object, object, object)
def __init__(self, num, temp_data):
self.num = num
self.temp_data = temp_data
super(ForeCast, self).__init__()
建立1个子窗口类显示当天的天气情况并显示十五天内的天气曲线,同样包含槽函数,切换城市时自动更新各窗口内的数据
class MainForeCast(QWidget):
data_signal = pyqtSignal(object,object,object,object)
def __init__(self, temp_data, current_data, temp_h, temp_l):
self.temp_data = temp_data
self.current_data = current_data
self.temp_h = temp_h
self.temp_l = temp_l
super(MainForeCast, self).__init__()
主程序中实例化子窗口类并完成布局
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle("天气")
# self.status.showMessage('状态提示栏',5000)
self.setWindowIcon(QIcon('./icon/n1.ico'))
self.setStyleSheet("#MainWindow{border-image:url(background.jpeg)}")
temp_data, current_data, temp_h, temp_l = Refrsh_weatherData('昆明')
self.mainforecast = MainForeCast(temp_data, current_data, temp_h, temp_l)
self.forecast1 = ForeCast(2, temp_data)
self.forecast2 = ForeCast(3, temp_data)
self.forecast2 = ForeCast(4, temp_data)
self.forecast2 = ForeCast(5,temp_data)
self.forecast3 = ForeCast(6, temp_data)
self.forecast4 = ForeCast(7, temp_data)
self.forecast5 = ForeCast(8, temp_data)
self.forecast6 = ForeCast(9, temp_data)
主程序中的combobox控件完成城市的切换,切换中发送信号更各子窗口的数据并update窗口更新画面
def combochange(self):
text = self.mainforecast.comboBox.currentText()
temp_data, current_data, temp_h, temp_l = Refrsh_weatherData(text)
self.mainforecast.data_signal.emit(temp_h,temp_l,current_data,temp_data)
self.forecast1.data_signal.emit(temp_h,temp_l,current_data,temp_data)
self.forecast2.data_signal.emit(temp_h, temp_l, current_data, temp_data)
self.forecast3.data_signal.emit(temp_h, temp_l, current_data, temp_data)
self.forecast4.data_signal.emit(temp_h, temp_l, current_data, temp_data)
self.forecast5.data_signal.emit(temp_h, temp_l, current_data, temp_data)
self.forecast6.data_signal.emit(temp_h, temp_l, current_data, temp_data)
界面类 class Ui_MainWindow(object)采用QtDesigner
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid")
MainWindow.setObjectName("MainWindow")
MainWindow.resize(790, 539)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.frame = QFrame(self.centralwidget)
sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
self.frame.setSizePolicy(sizePolicy)
self.frame.setMinimumSize(QSize(300, 100))
整体完成的程序,插入的天气图片用photoshop去底色
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import *
import sys
import random
import requests
import datetime
from zhdate import ZhDate # 农历转换
import ctypes
from datetime import *
import numpy as np
import pandas as pd
def Refrsh_weatherData(city):
print('city',city)
city_code = {'北京': '101010100', '上海': '101020100', '昆明': '101290101', '岳阳': '101251001', '成都': '101270101',
'杭州': '101210101', '南京': '101190101', '天津': '101030100', '广州': '101280101'}
url = 'http://t.weather.sojson.com/api/weather/city/' + city_code[city]
res = requests.get(url).json()
temp_data = pd.concat([pd.DataFrame(res['data']['forecast']), pd.DataFrame([res['data']['yesterday']])]).set_index(
'ymd').sort_index(ascending=True)
current_data = {'更新时间': res['time'], '星期': res['data']['forecast'][0]['week'], '城市': res['cityInfo']['city'],'日期':res['date'],
'温度': res['data']['wendu'], '湿度':res['data']['shidu'],'PM25': res['data']['pm25'], 'PM10': res['data']['pm10'],
'空气质量': res['data']['quality'], '感冒风险': res['data']['ganmao'], }
temp_h = np.zeros([16])
temp_l = np.zeros([16])
count = 0
for x in temp_data['high'].values:
temp_h[count] = int(x[3:x.find('℃')])
count = count + 1
count = 0
for x in temp_data['low'].values:
temp_l[count] = int(x[3:x.find('℃')])
count = count + 1
return temp_data,current_data,temp_h,temp_l
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("myappid")
MainWindow.setObjectName("MainWindow")
MainWindow.resize(790, 539)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.verticalLayout = QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName("verticalLayout")
self.frame = QFrame(self.centralwidget)
sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.frame.sizePolicy().hasHeightForWidth())
self.frame.setSizePolicy(sizePolicy)
self.frame.setMinimumSize(QSize(300, 100))
self.frame.setFrameShape(QFrame.Box)
self.frame.setFrameShadow(QFrame.Sunken)
self.frame.setObjectName("frame")
self.verticalLayout.addWidget(self.frame)
self.frame_2 = QFrame(self.centralwidget)
sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.frame_2.sizePolicy().hasHeightForWidth())
self.frame_2.setSizePolicy(sizePolicy)
self.frame_2.setMinimumSize(QSize(300, 50))
self.frame_2.setFrameShape(QFrame.Box)
self.frame_2.setFrameShadow(QFrame.Sunken)
self.frame_2.setObjectName("frame_2")
self.horizontalLayout = QHBoxLayout(self.frame_2)
self.horizontalLayout.setObjectName("horizontalLayout")
self.frame_3 = QFrame(self.frame_2)
self.frame_3.setMinimumSize(QSize(50, 50))
self.frame_3.setFrameShape(QFrame.Panel)
self.frame_3.setFrameShadow(QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.horizontalLayout.addWidget(self.frame_3)
self.frame_4 = QFrame(self.frame_2)
self.frame_4.setMinimumSize(QSize(50, 50))
self.frame_4.setFrameShape(QFrame.Panel)
self.frame_4.setFrameShadow(QFrame.Raised)
self.frame_4.setObjectName("frame_4")
# self.label_2 = QLabel(self.frame_4)
# self.label_2.setGeometry(QtCore.QRect(20, 20, 63, 14))
# self.label_2.setObjectName("label_2")
self.horizontalLayout.addWidget(self.frame_4)
self.frame_5 = QFrame(self.frame_2)
sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.frame_5.sizePolicy().hasHeightForWidth())
self.frame_5.setSizePolicy(sizePolicy)
self.frame_5.setMinimumSize(QSize(50, 50))
self.frame_5.setFrameShape(QFrame.Panel)
self.frame_5.setFrameShadow(QFrame.Raised)
self.frame_5.setObjectName("frame_5")
self.horizontalLayout.addWidget(self.frame_5)
self.frame_6 = QFrame(self.frame_2)
self.frame_6.setMinimumSize(QSize(50, 50))
self.frame_6.setFrameShape(QFrame.Panel)
self.frame_6.setFrameShadow(QFrame.Raised)
self.frame_6.setObjectName("frame_6")
self.horizontalLayout.addWidget(self.frame_6)
self.frame_7 = QFrame(self.frame_2)
self.frame_7.setMinimumSize(QSize(50, 50))
self.frame_7.setFrameShape(QFrame.Panel)
self.frame_7.setFrameShadow(QFrame.Raised)
self.frame_7.setObjectName("frame_7")
self.horizontalLayout.addWidget(self.frame_7)
self.frame_8 = QFrame(self.frame_2)
self.frame_8.setMinimumSize(QSize(50, 50))
self.frame_8.setFrameShape(QFrame.Panel)
self.frame_8.setFrameShadow(QFrame.Raised)
self.frame_8.setObjectName("frame_8")
self.horizontalLayout.addWidget(self.frame_8)
self.verticalLayout.addWidget(self.frame_2)
self.frame_10 = QFrame(self.centralwidget)
sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(1)
sizePolicy.setHeightForWidth(self.frame_10.sizePolicy().hasHeightForWidth())
self.frame_10.setSizePolicy(sizePolicy)
self.frame_10.setMinimumSize(QSize(50, 100))
self.frame_10.setFrameShape(QFrame.Box)
self.frame_10.setFrameShadow(QFrame.Sunken)
self.frame_10.setObjectName("frame_10")
self.verticalLayout.addWidget(self.frame_10)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QMenuBar(MainWindow)
self.menubar.setGeometry(QRect(0, 0, 790, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QCoreApplication.translate
class PaintArea(QWidget):
def __init__(self):
super(PaintArea, self).__init__()
self.setPalette(QPalette(QColor(70, 130, 180)))
self.setAutoFillBackground(True)
self.setMinimumSize(400, 400)
self.pen = QPen()
self.brush = QBrush()
label1 = QLabel(self)
label1.move(100, 100)
label1.resize(100, 100)
label1.setScaledContents(True)
pixmap = QPixmap('./icon/weather/sun.png')
label1.setPixmap(pixmap)
def paintEvent(self, event):
p = QPainter(self)
p.setPen(self.pen)
p.setBrush(self.brush)
class ForeCast(QWidget):
data_signal = pyqtSignal(object, object, object, object)
def __init__(self, num, temp_data):
self.num = num
self.temp_data = temp_data
super(ForeCast, self).__init__()
# self.date ='预报1'
self.label1 = QLabel(self)
self.label2 = QLabel(self)
self.label3 = QLabel(self)
self.label4 = QLabel(self)
self.label5 = QLabel(self)
self.label6 = QLabel(self)
self.label7 = QLabel(self)
self.label8 = QLabel(self)
self.label9 = QLabel(self)
self.data_signal.connect(self.data_refresh)
def data_refresh(self, temp_h, temp_l, current_data, temp_data):
self.temp_data = temp_data
self.current_data = current_data
self.temp_h = temp_h
self.temp_l = temp_l
def drawPage(self):
width = self.size().width()
height = self.size().height()
week_list = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
year = int(self.temp_data.index[self.num][:4])
mon = int(self.temp_data.index[self.num][5:7])
day = int(self.temp_data.index[self.num][8:])
if self.temp_data.iloc[self.num]['type'] == '晴':
pic = './icon/weather/sun.png'
elif self.temp_data.iloc[self.num]['type']== '阴':
pic = './icon/weather/cloudy.png'
elif self.temp_data.iloc[self.num]['type'] == '多云':
pic = './icon/weather/cloud.png'
elif self.temp_data.iloc[self.num]['type'] == '小雨':
pic = './icon/weather/rain1.png'
elif self.temp_data.iloc[self.num]['type'] == '中雨':
pic = './icon/weather/rain2.png'
elif self.temp_data.iloc[self.num]['type'] == '大雨':
pic = './icon/weather/rain3.png'
else:
pic = './icon/weather/sun.png'
font_size = int(height / 12)
text = self.temp_data.index[self.num] + ' ' + week_list[date(year, mon, day).weekday()]
self.label1.setText(text)
self.label1.setAutoFillBackground(True)
self.label1.setAlignment(Qt.AlignCenter)
self.label1.adjustSize()
x_pos = 10
y_pos = 0
StyleSheet = ("QLabel {"
+ "color:" + '#ff0000' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label1.setStyleSheet(StyleSheet)
self.label1.move(x_pos, y_pos)
self.label2.setAutoFillBackground(True)
self.label2.setAlignment(Qt.AlignLeft)
self.label2.setAlignment(Qt.AlignTop)
self.label2.resize(int(height / 5), int(height / 5))
x_pos = int((self.label1.width() - self.label2.width()) / 2)
y_pos = int(self.label1.pos().y() + self.label1.height() + 15)
self.label2.move(x_pos, y_pos)
self.label2.setScaledContents(True)
pixmap = QPixmap(pic)
self.label2.setPixmap(pixmap)
font_size = int(height / 15)
text = str(self.temp_data.iloc[self.num]['low'][3:]) + '-' + str(self.temp_data.iloc[self.num]['high'][3:])
self.label3.setText(text)
self.label3.setAutoFillBackground(True)
self.label3.setAlignment(Qt.AlignLeft)
self.label3.setAlignment(Qt.AlignTop)
self.label3.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#ffffff' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label3.setStyleSheet(StyleSheet)
x_pos = int((self.label1.width() - self.label3.width()) / 2)
y_pos = int(self.label2.pos().y() + self.label2.height() + 15)
self.label3.move(x_pos, y_pos)
text = self.temp_data.iloc[self.num]['type']
self.label4.setText(text)
self.label4.setAutoFillBackground(True)
self.label4.setAlignment(Qt.AlignLeft)
self.label4.setAlignment(Qt.AlignTop)
self.label4.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#ffffff' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label4.setStyleSheet(StyleSheet)
x_pos = int((self.label1.width() - self.label4.width()) / 2)
y_pos = int(self.label3.pos().y() + self.label3.height() + 15)
self.label4.move(x_pos, y_pos)
text = '日出时间 ' + self.temp_data.iloc[self.num]['sunrise']
self.label5.setText(text)
self.label5.setAutoFillBackground(True)
self.label5.setAlignment(Qt.AlignLeft)
self.label5.setAlignment(Qt.AlignTop)
self.label5.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#ffffff' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label5.setStyleSheet(StyleSheet)
x_pos = int((self.label1.width() - self.label5.width()) / 2)
y_pos = int(self.label4.pos().y() + self.label4.height() + 15)
self.label5.move(x_pos, y_pos)
text = '日落时间 ' + self.temp_data.iloc[self.num]['sunset']
self.label6.setText(text)
self.label6.setAutoFillBackground(True)
self.label6.setAlignment(Qt.AlignLeft)
self.label6.setAlignment(Qt.AlignTop)
self.label6.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#ffffff' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label6.setStyleSheet(StyleSheet)
x_pos = int((self.label1.width() - self.label6.width()) / 2)
y_pos = int(self.label5.pos().y() + self.label5.height() + 15)
self.label6.move(x_pos, y_pos)
text = self.temp_data.iloc[self.num]['fx'] + ' ' + self.temp_data.iloc[self.num]['fl']
self.label7.setText(text)
self.label7.setAutoFillBackground(True)
self.label7.setAlignment(Qt.AlignLeft)
self.label7.setAlignment(Qt.AlignTop)
self.label7.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#ffffff' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label7.setStyleSheet(StyleSheet)
x_pos = int((self.label1.width() - self.label7.width()) / 2)
y_pos = int(self.label6.pos().y() + self.label6.height() + 15)
self.label7.move(x_pos, y_pos)
value = int(self.temp_data.iloc[self.num]['aqi'])
if value < 50:
cla = ['一级', '优', '#00ff00', 'black']
elif value < 100:
cla = ['二级', '良', '#ffff00', 'black']
elif value < 150:
cla = ['三级', '轻度污染', '#ffa500', 'white']
elif value < 200:
cla = ['四级', '中度污染', '#ff0000', 'white']
elif value < 300:
cla = ['五级', '重度污染', '#836fff', 'white']
else:
cla = ['六级', '严重污染', '#8b0000', 'white']
self.label9.setFrameStyle(QFrame.Panel | QFrame.Raised)
self.label9.setAutoFillBackground(True)
self.label9.setAlignment(Qt.AlignCenter)
self.label9.setAlignment(Qt.AlignVCenter)
self.label9.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + cla[3] + "; "
+ "font-size: " + str(font_size + 4) + "px;"
+ "background-color:" + cla[2] + ";"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "border-radius: " + "5px" + ";"
+ "}")
self.label9.setStyleSheet(StyleSheet)
x_pos = int((self.label1.width() - self.label9.width()) / 2)
y_pos = int(self.label7.pos().y() + self.label7.height() + 10)
self.label9.move(x_pos, y_pos)
text = " " + str(value) + '' + cla[1]
self.label9.resize(self.label9.width() + 15, self.label9.height() + 5)
self.label9.setText(text)
def paintEvent(self, event):
self.drawPage()
class MainForeCast(QWidget):
data_signal = pyqtSignal(object,object,object,object)
def __init__(self, temp_data, current_data, temp_h, temp_l):
self.temp_data = temp_data
self.current_data = current_data
self.temp_h = temp_h
self.temp_l = temp_l
super(MainForeCast, self).__init__()
self.label21 = QLabel(self)
self.label22 = QLabel(self)
self.label23 = QLabel(self)
self.label24_t = QLabel(self)
self.label24 = QLabel(self)
self.label25 = QLabel(self)
self.label26 = QLabel(self)
self.label27 = QLabel(self)
self.label28 = QLabel(self)
self.label29 = QLabel(self)
self.label30 = QLabel(self)
self.comboBox = QComboBox(self)
self.data_signal.connect(self.data_refresh)
def data_refresh(self,temp_h,temp_l,current_data,temp_data):
self.temp_data = temp_data
self.current_data = current_data
self.temp_h = temp_h
self.temp_l = temp_l
def drawPage(self):
width = self.size().width()
height = self.size().height()
ht = int(self.size().height() / 12)
year = int(self.current_data['日期'][:4])
mon = int(self.current_data['日期'][5:6])
day = int(self.current_data['日期'][7:])
font_size = int(height / 12)
text = self.current_data['更新时间'][:10] + ' ' + self.current_data["星期"]
self.label21.setText(text)
self.label21.setAutoFillBackground(True)
self.label21.setAlignment(Qt.AlignLeft)
self.label21.adjustSize()
self.label21.move(10, 0)
StyleSheet = ("QLabel {"
+ "color:" + '#ffff00' + "; "
+ "font-size:" + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label21.setStyleSheet(StyleSheet)
font_size = int(height / 14)
text = str(ZhDate.from_datetime(datetime(year, mon, day))) # 农历
self.label22.setText(text)
self.label22.setAutoFillBackground(True)
self.label22.setAlignment(Qt.AlignLeft)
self.label22.setAlignment(Qt.AlignTop)
self.label22.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#ff0000' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label22.setStyleSheet(StyleSheet)
x_pos = int((self.label21.width() - self.label22.width()) / 2)
y_pos = int(self.label21.height() + self.label21.pos().y() + 15)
self.label22.move(x_pos, y_pos)
if self.temp_data.iloc[0]['type'] == '晴':
pic = './icon/weather/sun.png'
elif self.temp_data.iloc[0]['type'] == '阴':
pic = './icon/weather/cloudy.png'
elif self.temp_data.iloc[0]['type'] == '多云':
pic = './icon/weather/cloud.png'
elif self.temp_data.iloc[0]['type'] == '小雨':
pic = './icon/weather/rain1.png'
elif self.temp_data.iloc[0]['type'] == '中雨':
pic = './icon/weather/rain2.png'
elif self.temp_data.iloc[0]['type'] == '大雨':
pic = './icon/weather/rain3.png'
else:
pic = './icon/weather/sun.png'
self.label23.resize(ht * 3, ht * 3)
x_pos = int((self.label21.width() - self.label23.width()) / 2)
y_pos = int(self.label22.height() + self.label22.pos().y() + 15)
self.label23.move(x_pos, y_pos)
self.label23.setScaledContents(True)
pixmap = QPixmap(pic)
self.label23.setPixmap(pixmap)
font_size = int(height / 2)
text = self.current_data["温度"]
self.label24.setText(text)
self.label24.setAutoFillBackground(True)
self.label24.setAlignment(Qt.AlignLeft)
self.label24.setAlignment(Qt.AlignTop)
self.label24.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#ffff00' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label24.setStyleSheet(StyleSheet)
x_pos = int((self.label21.width() - self.label24.width()) / 2) - 20
y_pos = int(self.label23.pos().y() + self.label23.height()) + 15
self.label24.move(x_pos, y_pos)
self.label24_t.setScaledContents(True)
pixmap_t = QPixmap('./icon/weather/tempuint1.png')
self.label24_t.setPixmap(pixmap_t)
self.label24_t.move(self.label24.pos().x() + self.label24.width(), self.label24.pos().y() + 10)
self.label24_t.resize(int(self.label24.height() * 2 / 3), int(self.label24.height() * 2 / 3))
font_size = int(height / 10)
x_pos = int(self.label21.width() + self.label21.pos().x()) + ht * 3
y_pos = int(self.label22.pos().y())
self.comboBox.adjustSize()
StyleSheet = ("QComboBox {"
+ "color:" + '#ff0000' + "; "
+ "background-color: #4682B4;"
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.comboBox.setStyleSheet(StyleSheet)
self.comboBox.setStyleSheet(StyleSheet)
self.comboBox.move(x_pos, y_pos)
self.comboBox.resize(font_size * 4, font_size + 10)
font_size = int(height / 14)
text = str(self.temp_l[1]) + '-' + str(self.temp_h[1])
self.label25.setText(text)
self.label25.setAutoFillBackground(True)
self.label25.setAlignment(Qt.AlignLeft)
self.label25.setAlignment(Qt.AlignTop)
self.label25.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#000000' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label25.setStyleSheet(StyleSheet)
x_pos = int(
self.comboBox.width() + self.comboBox.pos().x() - self.comboBox.width() / 2 - self.label25.width() / 2)
y_pos = int(self.comboBox.pos().y() + self.comboBox.height() + 20)
self.label25.move(x_pos, y_pos)
font_size = int(height / 14)
text = self.temp_data.iloc[1]['fx'] + ' ' + str(self.temp_data.iloc[1]['fl'])
self.label26.setText(text)
self.label26.setAutoFillBackground(True)
self.label26.setAlignment(Qt.AlignLeft)
self.label26.setAlignment(Qt.AlignTop)
self.label26.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#000000' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label26.setStyleSheet(StyleSheet)
x_pos = int(
self.comboBox.width() + self.comboBox.pos().x() - self.comboBox.width() / 2 - self.label26.width() / 2)
y_pos = int(self.label25.pos().y() + self.label25.height() + 20)
self.label26.move(x_pos, y_pos)
font_size = int(height / 14)
text = 'PM25:' + str(self.current_data['PM25'])
self.label28.setText(text)
self.label28.setAutoFillBackground(True)
self.label28.setAlignment(Qt.AlignLeft)
self.label28.setAlignment(Qt.AlignTop)
self.label28.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#000000' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label28.setStyleSheet(StyleSheet)
x_pos = int(
self.comboBox.width() + self.comboBox.pos().x() - self.comboBox.width() / 2 - self.label28.width() / 2)
y_pos = int(self.label26.pos().y() + self.label26.height() + 20)
self.label28.move(x_pos, y_pos)
font_size = int(height / 14)
text = 'PM10:' + str(self.current_data['PM10'])
self.label30.setText(text)
self.label30.setAutoFillBackground(True)
self.label30.setAlignment(Qt.AlignLeft)
self.label30.setAlignment(Qt.AlignTop)
self.label30.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#000000' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label30.setStyleSheet(StyleSheet)
x_pos = int(
self.comboBox.width() + self.comboBox.pos().x() - self.comboBox.width() / 2 - self.label30.width() / 2)
y_pos = int(self.label28.pos().y() + self.label28.height() + 20)
self.label30.move(x_pos, y_pos)
font_size = int(height / 14)
value = int(self.temp_data.iloc[1]['aqi'])
if value < 50:
cla = ['一级', '优', '#00ff00', 'black']
elif value < 100:
cla = ['二级', '良', '#ffff00', 'black']
elif value < 150:
cla = ['三级', '轻度污染', '#ffa500', 'white']
elif value < 200:
cla = ['四级', '中度污染', '#ff0000', 'white']
elif value < 300:
cla = ['五级', '重度污染', '#836fff', 'white']
else:
cla = ['六级', '严重污染', '#8b0000', 'white']
self.label27.setFrameStyle(QFrame.Panel | QFrame.Raised)
self.label27.setAutoFillBackground(True)
self.label27.adjustSize()
setStyleSheet = ("QLabel {"
+ "color:" + cla[3] + "; "
+ "font-size: " + str(font_size) + "px;"
+ "background-color:" + cla[2] + ";"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "border-radius: " + "5px" + ";"
+ "}")
self.label27.setStyleSheet(setStyleSheet)
self.label27.setAlignment(Qt.AlignCenter)
self.label27.setAlignment(Qt.AlignVCenter)
text = ' ' + str(value) + ' ' + cla[1]
self.label27.setText(text)
x_pos = int(
self.comboBox.width() + self.comboBox.pos().x() - self.comboBox.width() / 2 - self.label27.width() / 2) - 10
y_pos = int(self.label30.pos().y() + self.label30.height() + 20)
self.label27.move(x_pos, y_pos)
self.label27.resize(self.label27.width() + 20, self.label27.height() + 20)
font_size = int(self.height() / 18)
text = '更新时间:' + str(self.current_data['更新时间'][15:])
self.label29.setText(text)
self.label29.setAutoFillBackground(True)
self.label29.adjustSize()
StyleSheet = ("QLabel {"
+ "color:" + '#000000' + "; "
+ "font-size: " + str(font_size) + "px;"
+ "font-weight:" + " bold" + ";"
+ "font-family: " + "'宋体'" + ";"
+ "}")
self.label29.setStyleSheet(StyleSheet)
x_pos = int(self.label21.width() + self.label21.pos().x() + self.width() / 20)
y_pos = int(self.label21.pos().y() + self.label21.height() - font_size)
self.label29.move(x_pos, y_pos)
def drawPic(self):
label_mark_h = max(self.temp_h)
label_mark_l = min(self.temp_l)
while (label_mark_h%5):
label_mark_h = label_mark_h + 1
while (label_mark_l % 5):
label_mark_l = label_mark_l - 1
temp_axis = np.linspace(label_mark_l, label_mark_h, 6)
x_pos1 = int(self.comboBox.pos().x() + self.comboBox.width() + self.width() / 6) # x_pos1 y_pos1 轴方向点
y_pos1 = int(self.comboBox.pos().y() + self.comboBox.height())
x_pos2 = x_pos1 # x_pos2 y_pos2 0点
y_pos2 = int(y_pos1 + self.height() / 2)
x_pos3 = int(self.width() * 9.5 / 10) # x_pos3 y_pos3 x轴方向点
y_pos3 = y_pos2
y_space = int((y_pos3 - y_pos1) / 6) # y轴向间隔
x_space = int((x_pos3 - x_pos1) / 15)-5 # x轴向间隔
qp = QPainter(self)
pen = QPen(Qt.black, 3, Qt.SolidLine)
qp.setPen(pen)
qp.begin(self)
qp.drawLine(x_pos1, y_pos1, x_pos2, y_pos2) # y轴线
qp.drawLine(x_pos2, y_pos2, x_pos3, y_pos3) # x轴线
pen = QPen(Qt.gray, 1, Qt.DashLine)
qp.setFont(QFont('宋体', 10))
qp.setPen(pen)
y_pos = np.zeros(6)
count = 0
for i in range(6):
pen = QPen(Qt.gray, 1, Qt.DashLine)
qp.setPen(pen)
qp.drawLine(x_pos1, int(y_pos2 - y_space * i), x_pos3 - 10, int(y_pos2 - y_space * i)) # 温度轴线6条虚线
y_pos[count] = int(y_pos2 - y_space * i)
count = count + 1
pen = QPen(Qt.black, 1, Qt.DashLine)
qp.setFont(QFont('宋体', 10))
qp.setPen(pen)
qp.drawText(x_pos1 - 30, y_pos2 - y_space * i, str(format(temp_axis[i], '2.0f'))) # 温度值刻度
pen = QPen(Qt.gray, 1, Qt.DashLine)
qp.setFont(QFont('宋体', 25))
qp.setPen(pen)
TempH_pos = np.zeros(30).reshape(15,2) # 获取日温度线高坐标np数组
TempL_pos = np.zeros(30).reshape(15, 2) # 获取日温度线低坐标np数组
count = 0
for i in range(15):
qp.drawLine(x_pos2 + (i + 1) * x_space, y_pos2, x_pos2 + (i + 1) * x_space, y_pos1) # 日期轴线
sh = temp_axis[temp_axis > self.temp_h[i]].size # 判断当前温度在温度表中第几格
sl = temp_axis[temp_axis > self.temp_l[i]].size # 判断当前温度在温度表中第几格
yh = y_pos[5-sh]
yl = y_pos[5-sl]
print(sh,sl,self.temp_h[i],self.temp_l[i])
if sh > 0:
x1 =(self.temp_h[i] - temp_axis[5-sh]) /((temp_axis[5-sh+1] - temp_axis[5-sh]))
y1 = yh - (y_pos[5 - sh] - y_pos[5 - sh + 1]) * x1
else:
x1 = temp_axis[5]
y1 = y_pos[5]
x2 = (self.temp_l[i] - temp_axis[5 - sl]) / ((temp_axis[5 - sl + 1] - temp_axis[5 - sl]))
y2 = yl - (y_pos[5 - sl] - y_pos[5 - sl + 1]) * x2
TempH_pos[count][1] = y1
TempL_pos[count][1] = y2
TempH_pos[count][0] = x_pos2 + (i + 1) * x_space
TempL_pos[count][0] = x_pos2 + (i + 1) * x_space
count = count +1
pen = QPen(Qt.red, 2, Qt.SolidLine) # 画日温度线——高
qp.setFont(QFont('宋体', 10))
qp.setPen(pen)
count = 0
for i in range(14):
qp.drawLine(int(TempH_pos[count][0]),int(TempH_pos[count][1]),int(TempH_pos[count+1][0]),int(TempH_pos[count+1][1]))
qp.drawText(int(TempH_pos[count][0]-10),int(TempH_pos[count][1]-10),str(format(self.temp_h[i],'2.0f')))
count = count + 1
pen = QPen(Qt.yellow, 2, Qt.SolidLine) # 画日温度线——低
qp.setFont(QFont('宋体', 10))
qp.setPen(pen)
count = 0
for i in range(14):
qp.drawLine(int(TempL_pos[count][0]), int(TempL_pos[count][1]), int(TempL_pos[count + 1][0]), int(TempL_pos[count + 1][1]))
qp.drawText(int(TempL_pos[count][0])-10, int(TempL_pos[count][1])-10,str(format(self.temp_l[i],'2.0f')))
count = count + 1
pen = QPen(Qt.black, 1, Qt.SolidLine)
qp.setFont(QFont('宋体', 10))
qp.setPen(pen)
qp.translate(0, 0)
qp.rotate(90)
for i in range(15):
qp.drawText(int(y_pos2 + 15), int(-1 * (x_pos2 + (i + 1) * x_space)), str(self.temp_data.index[i][5:])) # 日期刻度
qp.transform()
qp.end()
def paintEvent(self, event):
self.drawPage()
self.drawPic()
pass
class MainWindow(QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle("天气")
# self.status.showMessage('状态提示栏',5000)
self.setWindowIcon(QIcon('./icon/n1.ico'))
self.setStyleSheet("#MainWindow{border-image:url(background.jpeg)}")
city_code = {'北京': '101010100', '上海': '101020100', '昆明': '101290101', '岳阳': '101251001', '成都': '101270101',
'杭州': '101210101', '南京': '101190101', '天津': '101030100', '广州': '101280101'}
temp_data, current_data, temp_h, temp_l = Refrsh_weatherData('昆明')
self.mainforecast = MainForeCast(temp_data, current_data, temp_h, temp_l)
self.area = PaintArea()
self.forecast1 = ForeCast(2, temp_data)
self.forecast2 = ForeCast(3, temp_data)
self.forecast2 = ForeCast(4, temp_data)
self.forecast2 = ForeCast(5,temp_data)
self.forecast3 = ForeCast(6, temp_data)
self.forecast4 = ForeCast(7, temp_data)
self.forecast5 = ForeCast(8, temp_data)
self.forecast6 = ForeCast(9, temp_data)
self.setupUi(self)
self.hLayout_3 = QHBoxLayout(self.frame_3)
self.hLayout_3.setObjectName("hLayout_3")
self.hLayout_3.addWidget(self.forecast1)
self.hLayout_4 = QHBoxLayout(self.frame_4)
self.hLayout_4.setObjectName("hLayout_4")
self.hLayout_4.addWidget(self.forecast2)
self.hLayout_5 = QHBoxLayout(self.frame_5)
self.hLayout_5.setObjectName("hLayout_5")
self.hLayout_5.addWidget(self.forecast3)
self.hLayout_6 = QHBoxLayout(self.frame_6)
self.hLayout_6.setObjectName("hLayout_6")
self.hLayout_6.addWidget(self.forecast4)
self.hLayout_7 = QHBoxLayout(self.frame_7)
self.hLayout_7.setObjectName("hLayout_7")
self.hLayout_7.addWidget(self.forecast5)
self.hLayout_8 = QHBoxLayout(self.frame_8)
self.hLayout_8.setObjectName("hLayout_8")
self.hLayout_8.addWidget(self.forecast6)
self.frame_3.setStyleSheet("QWidget{ background-color: steelblue}")
self.frame_4.setStyleSheet("QWidget{ background-color: steelblue}")
self.frame_5.setStyleSheet("QWidget{ background-color: steelblue}")
self.frame_6.setStyleSheet("QWidget{ background-color: steelblue}")
self.frame_7.setStyleSheet("QWidget{ background-color: steelblue}")
self.frame_8.setStyleSheet("QWidget{ background-color: steelblue}")
self.frame.setStyleSheet("QWidget{ background-color: steelblue}")
self.horizontalLayout_10 = QHBoxLayout(self.frame)
self.horizontalLayout_10.setObjectName("horizontalLayout_10")
self.horizontalLayout_10.addWidget(self.mainforecast)
self.horizontalLayout_2 = QHBoxLayout(self.frame_10)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.horizontalLayout_2.addWidget(self.area)
self.mainforecast.comboBox.addItems(city_code.keys())
self.mainforecast.comboBox.currentIndexChanged.connect(self.combochange)
#self.mainforecast.drawPage()
self.update()
def combochange(self):
text = self.mainforecast.comboBox.currentText()
temp_data, current_data, temp_h, temp_l = Refrsh_weatherData(text)
self.mainforecast.data_signal.emit(temp_h,temp_l,current_data,temp_data)
self.forecast1.data_signal.emit(temp_h,temp_l,current_data,temp_data)
self.forecast2.data_signal.emit(temp_h, temp_l, current_data, temp_data)
self.forecast3.data_signal.emit(temp_h, temp_l, current_data, temp_data)
self.forecast4.data_signal.emit(temp_h, temp_l, current_data, temp_data)
self.forecast5.data_signal.emit(temp_h, temp_l, current_data, temp_data)
self.forecast6.data_signal.emit(temp_h, temp_l, current_data, temp_data)
self.update()
if __name__ == '__main__':
app = QApplication(sys.argv)
form = MainWindow()
# form.showMaximized()
form.show()
app.exec_()
