前言:通过在网上找到的“电池电压监控界面”,学习PyQt5中添加工具栏、增加SwitchButton控件,在滑块控件右侧增加文本显示、设置界面背景颜色、修改文本控件字体颜色等。
1. 上位机界面效果展示
网络上原图如下:
自己使用PyQt5做的界面效果如下:
做出来的界面效果不如原来的界面好看,也尝试设置窗口的背景图片,但是选不到好看的背景图片,做出来的效果还不如单纯将界面背景设置成黑色的大气。
2. 设置开关控件,左划关闭右划打开
参考文章:PyQt 定义控件SwitchButton 指南
搞定PyQt5 开关控件 SwitchButton 滑动开关一文就够了使用这篇文章的SwitchButton 控件,实际界面在划动SwitchButton时,速度太慢了,主要是点击SwitchButton 控件事件不是鼠标往右边拉动事件。
结合上述两篇文章的代码,主要以PyQt 定义控件SwitchButton 指南这篇文章代码为主。
QSwitchButton空间效果如下,也是鼠标点击事件,不是鼠标向左右划动事件。
修改的QSwitchButton空间代码如下:
#!/usr/bin/env pythonimport sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout
from PyQt5.QtCore import pyqtSignal, Qt, QRect
from PyQt5.QtGui import QPainter, QFont, QBrush, QColor, QPenclass QSwitchButton(QWidget):clickedOn = pyqtSignal()clickedOff = pyqtSignal()def __init__(self, parent=None):super(QSwitchButton, self).__init__(parent)self.setWindowFlags(self.windowFlags() | Qt.FramelessWindowHint)self.setAttribute(Qt.WA_TranslucentBackground)# self.resize(100, 30)# QSwitchButtonstate:True is ON,False is OFFself.state = Falseself.setFixedSize(80, 40)def mousePressEvent(self, event):"""set click event for state change"""super(QSwitchButton, self).mousePressEvent(event)if self.state:# 发射信号self.clickedOff.emit()else:# 发射信号self.clickedOn.emit()self.state = False if self.state else Trueself.update()def paintEvent(self, event):"""Set the button"""super(QSwitchButton, self).paintEvent(event)# Create a renderer and set anti-aliasing and smooth transitionspainter = QPainter(self)painter.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)# Defining font stylesfont = QFont("Arial")font.setPixelSize(self.height() // 3)painter.setFont(font)# QSwitchButton state:ONif self.state:# Drawing backgroundpainter.setPen(Qt.NoPen)# brush = QBrush(QColor('#bd93f9'))brush = QBrush(QColor("green"))painter.setBrush(brush)# Top left corner of the rectangle coordinaterect_x = 0rect_y = 0rect_width = self.width()rect_height = self.height()rect_radius = self.height() // 2painter.drawRoundedRect(rect_x, rect_y, rect_width, rect_height, rect_radius, rect_radius)# Drawing slides circlepainter.setPen(Qt.NoPen)brush.setColor(QColor("#ffffff"))painter.setBrush(brush)# Phase difference pixel point# Top left corner of the rectangle coordinatediff_pix = 3rect_x = self.width() - diff_pix - (self.height() - 2 * diff_pix)rect_y = diff_pixrect_width = self.height() - 2 * diff_pixrect_height = self.height() - 2 * diff_pixrect_radius = (self.height() - 2 * diff_pix) // 2painter.drawRoundedRect(rect_x, rect_y, rect_width, rect_height, rect_radius, rect_radius)# ON txt setpainter.setPen(QPen(QColor("#ffffff")))painter.setBrush(Qt.NoBrush)painter.drawText(QRect(int(self.height() / 3), int(self.height() / 3.5), 50, 20),Qt.AlignLeft,"ON",)# QSwitchButton state:OFFelse:# Drawing backgroundpainter.setPen(Qt.NoPen)# brush = QBrush(QColor('#525555'))brush = QBrush(QColor("white"))painter.setBrush(brush)# Top left corner of the rectangle coordinaterect_x = 0rect_y = 0rect_width = self.width()rect_height = self.height()rect_radius = self.height() // 2painter.drawRoundedRect(rect_x, rect_y, rect_width, rect_height, rect_radius, rect_radius)# Drawing slides circlepen = QPen(QColor("#999999"))pen.setWidth(1)painter.setPen(pen)# Phase difference pixel pointdiff_pix = 3# Top left corner of the rectangle coordinaterect_x = diff_pixrect_y = diff_pixrect_width = self.height() - 2 * diff_pixrect_height = self.height() - 2 * diff_pixrect_radius = (self.height() - 2 * diff_pix) // 2painter.drawRoundedRect(rect_x, rect_y, rect_width, rect_height, rect_radius, rect_radius)# OFF txt setpainter.setBrush(Qt.NoBrush)painter.drawText(QRect(int(self.width() * 1 / 2), int(self.height() / 3.5), 50, 20),Qt.AlignLeft,"OFF",)def main():app = QApplication(sys.argv)window = QMainWindow()window.setGeometry(100, 100, 100, 290)window.setWindowTitle("Switch Button Example")switch1 = QSwitchButton()switch2 = QSwitchButton()layout = QVBoxLayout()layout.addWidget(switch1)layout.addWidget(switch2)window.setCentralWidget(QWidget())window.centralWidget().setLayout(layout)window.show()sys.exit(app.exec_())if __name__ == "__main__":main()
3. 设置工具栏为界面左侧显示
以下是添加工具栏到界面底部的代码:
from PyQt5.QtCore import Qt# 创建一个工具栏
toolbar = QToolBar("Bottom Toolbar")
# 添加一个动作到工具栏
toolbar.addAction(QAction(QIcon('./icon/下载.png'), "下载", self))
# 将工具栏添加到主窗口
self.addToolBar(Qt.BottomToolBarArea, toolbar)
只要修改Qt.BottomToolBarArea为Qt.LeftToolBarArea就可以实现添加界面左侧工具栏功能。
# 将工具栏添加到主窗口
self.addToolBar(Qt.LeftToolBarArea, toolbar)
4. toolbar工具栏设置图标间隔大小
使用下面一行代码,可以设置工具栏上每个图标的间隔大小是一个固定值。
toolbar.setStyleSheet("QToolBar{spacing:8px;}")
要达到界面美观的效果,必须考虑图标之间的间隔要随着窗口的缩放而变化,使用以下在图标之间增加一个QWidget控件的方法可以实现工具栏图标间距为自适应窗口大小。
# 创建一个垂直的工具栏self.leftToolBar = QToolBar()# self.leftToolBar.setStyleSheet("QToolBar{spacing:60px;}")# 将工具栏添加到主窗口self.addToolBar(Qt.LeftToolBarArea, self.leftToolBar)# 创建一个QAction,并设置其图标和提示action = QAction(QIcon('./icon/主界面.png'), 'menu', self)action.setShortcut('Ctrl+Q')action.triggered.connect(self.close)self.leftToolBar.addAction(action)# 增加工具栏图标间距为自适应窗口大小self.set_action_spacer(self.leftToolBar)action = QAction(QIcon('./icon/网关信息.png'), 'internet', self)action.setShortcut('Ctrl+Q')action.triggered.connect(self.close)self.leftToolBar.addAction(action)def set_action_spacer(self, ToolBar):# 插入一个透明的间隔动作(这里使用QWidget作为间隔)spacer = QWidget()size_policy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)spacer.setSizePolicy(size_policy)ToolBar.addWidget(spacer)
5. 滑块QSlider右侧设置文本框显示滑块数值
想达到的效果是:文本框紧挨着滑块,开始的做法是设置文本框与滑块在gridLayout布局的同一个格子里面,这样就实现了文本框紧挨着滑块并显示在滑块右侧的视觉效果。
# 创建一个标签来显示滑块的值self.horizontalSlider.setFixedSize(200, 20)self.horizontalSlider_2.setFixedSize(200, 20)self.horizontalSlider.setEnabled(True) # 确保滑块是启用的self.horizontalSlider_2.setEnabled(True) # 确保滑块是启用的self.valueLabel = QLabel('10%', self)self.valueLabel.setAlignment(Qt.AlignRight | Qt.AlignVCenter) # 右对齐self.valueLabel.setFixedSize(240, 20)# 将滑块和标签添加到布局中self.gridLayout_4.addWidget(self.valueLabel, 1, 0, 1, 1)
但这样做的后果是拉不动滑块,找到原因可能是滑块的布局受限导致影响滑块的移动,取消文本框与滑块在同一个格子之后,滑块能够正常拉动数值。
使用Qt Designer在滑块右侧的格子中添加文本框,通过QgridLayout的layoutHorizontalSpacing属性设置滑块与文本框之间的间距,尽量在界面展示效果上达到文本框紧挨着滑块显示数值的功能。
6. 设置窗口背景图片
参考文章:Pyqt5设置窗口背景的3种方法
# 设置界面背景图片
palette = QPalette()
palette.setBrush(QPalette.Background, QBrush(QPixmap("./img.png")))
self.setPalette(palette)
界面设置背景图片后的展示效果如下:
7. 点击工具栏图标实现截图功能
参考文章:python PyQt5截图
关键代码:
# 创建一个QAction,用于触发截图screenshot_action = QAction(QIcon('./icon/照相机.png'), 'Screenshot', self)screenshot_action.triggered.connect(self.take_screenshot)screenshot_action.setShortcut("Ctrl+S")self.leftToolBar.addAction(screenshot_action)def take_screenshot(self):# 获取整个屏幕的图像screen = QGuiApplication.primaryScreen()if screen is not None:screenshot = screen.grabWindow(0)screenshot.save('screenshot.png', 'png')# 弹出消息框显示截图QMessageBox.information(self, 'Screenshot', 'Screenshot taken.')
8. 如何去掉工具栏最上方的省略号
使用如下代码添加工具栏到界面最左侧:
# 创建一个垂直的工具栏self.leftToolBar = QToolBar()# 将工具栏添加到主窗口self.addToolBar(Qt.LeftToolBarArea, self.leftToolBar)
运行界面会出现一种情况:工具栏最上方会有几个省略号,这样会影响美观。
考虑为什么会出现这种情况,当只是添加一个工具栏不添加QAction,工具栏最上方只有两个点,随着QAction的添加,工具栏上方的点数也增加,可能原因猜测是由于工具栏的空间不足导致的。考虑使用Qt Designer在界面最左侧添加一个verticalLayout,将工具栏放置在verticalLayout上面,增加工具栏的空间。
使用如下代码实现将工具栏放置在verticalLayout上面,增加工具栏的空间。
# 创建一个垂直的工具栏self.leftToolBar = QToolBar()self.leftToolBar.setOrientation(Qt.Vertical)# 将工具栏添加到主窗口# self.addToolBar(Qt.LeftToolBarArea, self.leftToolBar)self.verticalLayout.addWidget(self.leftToolBar)
结论:将工具栏放置在verticalLayout上面,增加工具栏的空间,通过这种方式解决了工具栏最上方的省略号问题,但是另一个问题是随着界面全屏显示,工具栏位置不在界面最左侧固定了,也会随着窗口变大而变化,原因应该是verticalLayout宽度增加的问题。
现在要解决的问题:如何使工具栏位置固定在界面最左侧,不会随着窗口变大而位置变化
解决办法:使用Qt Designer设计界面布局时,增加如下所示的两个horizontalSpacer,并且horizontalSpacer的sizeType设置为Expanding。这样随着窗口的放大,最左侧的工具栏位置也不会变动,会一直靠左显示。
9. 设置工具栏上QAction控件的背景颜色
QAction控件是没有样式表可以设置其背景颜色的,只有通过设置QToolBar工具栏的样式表背景颜色来达到设置工具栏上QAction控件的背景颜色目的。
设置工具栏上QToolBar的背景颜色为淡蓝色,关键代码:
self.leftToolBar.setStyleSheet(f"QToolBar {{background-color: {QColor('lightBlue').name()}}};")
完整代码:
# 创建一个垂直的工具栏self.leftToolBar = QToolBar()self.leftToolBar.setOrientation(Qt.Vertical)# self.leftToolBar.setStyleSheet("QToolBar{spacing:60px;}")# 将工具栏添加到主窗口# self.addToolBar(Qt.LeftToolBarArea, self.leftToolBar)self.verticalLayout.addWidget(self.leftToolBar)action = QAction(QIcon("./icon/电池.png"), "BatteryInfo", self)# action.setShortcut("Ctrl+Q")# action.triggered.connect(self.close)self.leftToolBar.addAction(action)self.leftToolBar.setStyleSheet(f"QToolBar {{background-color: {QColor('lightBlue').name()}}};")# 增加工具栏图标间距为自适应窗口大小self.set_action_spacer(self.verticalLayout)
10. 完整界面程序代码
main.py代码如下:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : Logintern09import sys
from PyQt5.QtWidgets import (QApplication,QMainWindow,QWidget,QLabel,QAction,QToolBar,QSizePolicy,QMessageBox,
)
from PyQt5.QtGui import QIcon, QPalette, QColor, QFont, QGuiApplication
from PyQt5.QtCore import Qt
from battery_v import Ui_MainWindow
from QSwitchButton import QSwitchButtonclass mainWindow(QMainWindow, Ui_MainWindow):def __init__(self):super(mainWindow, self).__init__()self.setupUi(self) # 初始化窗口# 设置界面的背景颜色self.set_background_color()# 设置界面背景图片# palette = QPalette()# palette.setBrush(QPalette.Background, QBrush(QPixmap("./img.png")))# self.setPalette(palette)# 设置窗口图标self.setWindowIcon(QIcon("./icon/电量未知.png"))progressBar_list = [self.progressBar_3,self.progressBar_4,self.progressBar_5,self.progressBar_6,]for progressBar in progressBar_list:progressBar.setFixedSize(60, 150)# 创建QSwitchButton控件并放入容器self.switchButton = QSwitchButton()self.gridLayout_8.addWidget(self.switchButton, 0, 1, 1, 1)self.switchButton.clickedOn.connect(self.on_voltage_view)self.switchButton.clickedOff.connect(self.off_voltage_view)# 创建一个标签来显示滑块的值self.horizontalSlider.setFixedSize(200, 20)self.horizontalSlider_2.setFixedSize(200, 20)self.horizontalSlider.setEnabled(True) # 确保滑块是启用的self.horizontalSlider_2.setEnabled(True) # 确保滑块是启用的# 连接滑块的值改变信号到槽函数self.horizontalSlider.valueChanged.connect((lambda: self.on_slider_value_changed(self.label_26, self.horizontalSlider.value())))# # 连接滑块的值改变信号到槽函数self.horizontalSlider_2.valueChanged.connect((lambda: self.on_slider_value_changed(self.label_27, self.horizontalSlider_2.value())))# 找到所有的QLabel控件label_obj = self.findChildren(QLabel)for label in label_obj:label.setStyleSheet("color: white;") # 设置字体颜色为白色# 设置QLabel的字体大小font = QFont()font.setPointSize(14) # 设置字体大小为14点label.setFont(font)self.label_7.setStyleSheet("color: green;") # 设置字体颜色为绿色self.label_25.setFixedSize(100, 40)# 设置按钮的样式self.pushButton.setFixedSize(200, 40)font = QFont()font.setPointSize(14) # 设置字体大小为14点font.setBold(True)self.pushButton.setFont(font)# 创建一个垂直的工具栏self.leftToolBar = QToolBar()self.leftToolBar.setOrientation(Qt.Vertical)# self.leftToolBar.setStyleSheet("QToolBar{spacing:60px;}")# 将工具栏添加到主窗口# self.addToolBar(Qt.LeftToolBarArea, self.leftToolBar)self.verticalLayout.addWidget(self.leftToolBar)# 创建一个QAction,并设置其图标和提示action = QAction(QIcon("./icon/主界面.png"), "Menu", self)# action.setShortcut("Ctrl+Q")# action.triggered.connect(self.close)self.leftToolBar.addAction(action)# 增加工具栏图标间距为自适应窗口大小self.set_action_spacer(self.verticalLayout)# 创建一个垂直的工具栏self.leftToolBar = QToolBar()self.leftToolBar.setOrientation(Qt.Vertical)# self.leftToolBar.setStyleSheet("QToolBar{spacing:60px;}")# 将工具栏添加到主窗口# self.addToolBar(Qt.LeftToolBarArea, self.leftToolBar)self.verticalLayout.addWidget(self.leftToolBar)action = QAction(QIcon("./icon/网关信息.png"), "Internet", self)# action.setShortcut("Ctrl+Q")# action.triggered.connect(self.close)self.leftToolBar.addAction(action)# 增加工具栏图标间距为自适应窗口大小self.set_action_spacer(self.verticalLayout)# 创建一个垂直的工具栏self.leftToolBar = QToolBar()self.leftToolBar.setOrientation(Qt.Vertical)# self.leftToolBar.setStyleSheet("QToolBar{spacing:60px;}")# 将工具栏添加到主窗口# self.addToolBar(Qt.LeftToolBarArea, self.leftToolBar)self.verticalLayout.addWidget(self.leftToolBar)action = QAction(QIcon("./icon/HD连接.png"), "HDConnect", self)# action.setShortcut("Ctrl+Q")# action.triggered.connect(self.close)self.leftToolBar.addAction(action)# 增加工具栏图标间距为自适应窗口大小self.set_action_spacer(self.verticalLayout)# 创建一个垂直的工具栏self.leftToolBar = QToolBar()self.leftToolBar.setOrientation(Qt.Vertical)# self.leftToolBar.setStyleSheet("QToolBar{spacing:60px;}")# 将工具栏添加到主窗口# self.addToolBar(Qt.LeftToolBarArea, self.leftToolBar)self.verticalLayout.addWidget(self.leftToolBar)action = QAction(QIcon("./icon/电池.png"), "BatteryInfo", self)# action.setShortcut("Ctrl+Q")# action.triggered.connect(self.close)self.leftToolBar.addAction(action)self.leftToolBar.setStyleSheet(f"QToolBar {{background-color: {QColor('lightBlue').name()}}};")# 增加工具栏图标间距为自适应窗口大小self.set_action_spacer(self.verticalLayout)# 创建一个垂直的工具栏self.leftToolBar = QToolBar()self.leftToolBar.setOrientation(Qt.Vertical)# self.leftToolBar.setStyleSheet("QToolBar{spacing:60px;}")# 将工具栏添加到主窗口# self.addToolBar(Qt.LeftToolBarArea, self.leftToolBar)self.verticalLayout.addWidget(self.leftToolBar)# 创建一个QAction,用于触发截图screenshot_action = QAction(QIcon("./icon/照相机.png"), "Screenshot", self)screenshot_action.triggered.connect(self.take_screenshot)screenshot_action.setShortcut("Ctrl+S")self.leftToolBar.addAction(screenshot_action)# 增加工具栏图标间距为自适应窗口大小self.set_action_spacer(self.verticalLayout)# 创建一个垂直的工具栏self.leftToolBar = QToolBar()self.leftToolBar.setOrientation(Qt.Vertical)# self.leftToolBar.setStyleSheet("QToolBar{spacing:60px;}")# 将工具栏添加到主窗口# self.addToolBar(Qt.LeftToolBarArea, self.leftToolBar)self.verticalLayout.addWidget(self.leftToolBar)action = QAction(QIcon("./icon/四点连接.png"), "Close", self)action.setShortcut("Ctrl+Q")action.triggered.connect(self.close)self.leftToolBar.addAction(action)def set_background_color(self):# 创建调色板palette = QPalette()# 设置背景颜色为亮黑色palette.setColor(QPalette.Background, QColor(30, 30, 30)) # 亮黑色# 应用调色板self.setPalette(palette)def on_slider_value_changed(self, valueLabel, value):# 当滑块的值改变时,更新标签显示的数值valueLabel.setText(str(value) + "%")def on_voltage_view(self):progressBar_list = [self.progressBar_3,self.progressBar_4,self.progressBar_5,self.progressBar_6,]text_label = [self.label_14, self.label_15, self.label_16, self.label_17]label_data = ["3.79V", "3.80V", "3.80V", "3.80V"]for progressBar in progressBar_list:progressBar.setValue(60)for i, label in enumerate(text_label):label.setText(label_data[i])def off_voltage_view(self):progressBar_list = [self.progressBar_3,self.progressBar_4,self.progressBar_5,self.progressBar_6,]text_label = [self.label_14, self.label_15, self.label_16, self.label_17]for progressBar in progressBar_list:progressBar.setValue(0)for i, label in enumerate(text_label):label.setText("0V")def set_action_spacer(self, layout):# 插入一个透明的间隔动作(这里使用QWidget作为间隔)spacer = QWidget()size_policy = QSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)spacer.setSizePolicy(size_policy)layout.addWidget(spacer)def take_screenshot(self):# 获取整个屏幕的图像screen = QGuiApplication.primaryScreen()if screen is not None:screenshot = screen.grabWindow(0)screenshot.save("screenshot.png", "png")# 弹出消息框显示截图QMessageBox.information(self, "Screenshot", "Screenshot taken.")if __name__ == "__main__":app = QApplication(sys.argv)ui = mainWindow()ui.show()sys.exit(app.exec_())
Qt Designer工具设计基础界面布局,生成的.ui转成python的.py代码如下:
# -*- coding: utf-8 -*-# Form implementation generated from reading ui file 'battery_v.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(982, 740)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)self.gridLayout.setObjectName("gridLayout")self.horizontalLayout = QtWidgets.QHBoxLayout()self.horizontalLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)self.horizontalLayout.setObjectName("horizontalLayout")self.label = QtWidgets.QLabel(self.centralwidget)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())self.label.setSizePolicy(sizePolicy)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label.setFont(font)self.label.setAlignment(QtCore.Qt.AlignCenter)self.label.setObjectName("label")self.horizontalLayout.addWidget(self.label)self.gridLayout.addLayout(self.horizontalLayout, 0, 2, 1, 1)self.frame_4 = QtWidgets.QFrame(self.centralwidget)self.frame_4.setFrameShape(QtWidgets.QFrame.Box)self.frame_4.setFrameShadow(QtWidgets.QFrame.Sunken)self.frame_4.setLineWidth(2)self.frame_4.setObjectName("frame_4")self.gridLayout_10 = QtWidgets.QGridLayout(self.frame_4)self.gridLayout_10.setObjectName("gridLayout_10")self.gridLayout_4 = QtWidgets.QGridLayout()self.gridLayout_4.setContentsMargins(-1, 50, -1, 0)self.gridLayout_4.setSpacing(20)self.gridLayout_4.setObjectName("gridLayout_4")self.label_26 = QtWidgets.QLabel(self.frame_4)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label_26.setFont(font)self.label_26.setObjectName("label_26")self.gridLayout_4.addWidget(self.label_26, 1, 1, 1, 1)self.label_5 = QtWidgets.QLabel(self.frame_4)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_5.sizePolicy().hasHeightForWidth())self.label_5.setSizePolicy(sizePolicy)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label_5.setFont(font)self.label_5.setAlignment(QtCore.Qt.AlignCenter)self.label_5.setObjectName("label_5")self.gridLayout_4.addWidget(self.label_5, 1, 4, 1, 1)self.horizontalSlider = QtWidgets.QSlider(self.frame_4)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.horizontalSlider.sizePolicy().hasHeightForWidth())self.horizontalSlider.setSizePolicy(sizePolicy)self.horizontalSlider.setMaximum(100)self.horizontalSlider.setProperty("value", 10)self.horizontalSlider.setOrientation(QtCore.Qt.Horizontal)self.horizontalSlider.setObjectName("horizontalSlider")self.gridLayout_4.addWidget(self.horizontalSlider, 1, 0, 1, 1)self.horizontalSlider_2 = QtWidgets.QSlider(self.frame_4)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.horizontalSlider_2.sizePolicy().hasHeightForWidth())self.horizontalSlider_2.setSizePolicy(sizePolicy)self.horizontalSlider_2.setAutoFillBackground(False)self.horizontalSlider_2.setMaximum(100)self.horizontalSlider_2.setProperty("value", 30)self.horizontalSlider_2.setOrientation(QtCore.Qt.Horizontal)self.horizontalSlider_2.setObjectName("horizontalSlider_2")self.gridLayout_4.addWidget(self.horizontalSlider_2, 1, 2, 1, 1)self.label_2 = QtWidgets.QLabel(self.frame_4)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth())self.label_2.setSizePolicy(sizePolicy)self.label_2.setObjectName("label_2")self.gridLayout_4.addWidget(self.label_2, 0, 2, 1, 1)self.label_3 = QtWidgets.QLabel(self.frame_4)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())self.label_3.setSizePolicy(sizePolicy)self.label_3.setObjectName("label_3")self.gridLayout_4.addWidget(self.label_3, 0, 0, 1, 1)self.label_4 = QtWidgets.QLabel(self.frame_4)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_4.sizePolicy().hasHeightForWidth())self.label_4.setSizePolicy(sizePolicy)self.label_4.setLayoutDirection(QtCore.Qt.LeftToRight)self.label_4.setAlignment(QtCore.Qt.AlignLeading | QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)self.label_4.setObjectName("label_4")self.gridLayout_4.addWidget(self.label_4, 0, 4, 1, 1)self.label_27 = QtWidgets.QLabel(self.frame_4)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label_27.setFont(font)self.label_27.setObjectName("label_27")self.gridLayout_4.addWidget(self.label_27, 1, 3, 1, 1)self.gridLayout_10.addLayout(self.gridLayout_4, 0, 0, 1, 1)self.gridLayout.addWidget(self.frame_4, 1, 2, 1, 1)self.frame_3 = QtWidgets.QFrame(self.centralwidget)self.frame_3.setFrameShape(QtWidgets.QFrame.Box)self.frame_3.setFrameShadow(QtWidgets.QFrame.Sunken)self.frame_3.setLineWidth(2)self.frame_3.setObjectName("frame_3")self.gridLayout_2 = QtWidgets.QGridLayout(self.frame_3)self.gridLayout_2.setObjectName("gridLayout_2")self.gridLayout_9 = QtWidgets.QGridLayout()self.gridLayout_9.setContentsMargins(-1, -1, -1, 0)self.gridLayout_9.setHorizontalSpacing(20)self.gridLayout_9.setVerticalSpacing(0)self.gridLayout_9.setObjectName("gridLayout_9")self.label_24 = QtWidgets.QLabel(self.frame_3)self.label_24.setFrameShadow(QtWidgets.QFrame.Sunken)self.label_24.setText("")self.label_24.setAlignment(QtCore.Qt.AlignCenter)self.label_24.setObjectName("label_24")self.gridLayout_9.addWidget(self.label_24, 0, 2, 1, 1)self.pushButton = QtWidgets.QPushButton(self.frame_3)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.pushButton.sizePolicy().hasHeightForWidth())self.pushButton.setSizePolicy(sizePolicy)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.pushButton.setFont(font)self.pushButton.setObjectName("pushButton")self.gridLayout_9.addWidget(self.pushButton, 0, 3, 1, 1)self.label_23 = QtWidgets.QLabel(self.frame_3)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_23.sizePolicy().hasHeightForWidth())self.label_23.setSizePolicy(sizePolicy)self.label_23.setAlignment(QtCore.Qt.AlignCenter)self.label_23.setObjectName("label_23")self.gridLayout_9.addWidget(self.label_23, 0, 0, 1, 1)self.label_25 = QtWidgets.QLabel(self.frame_3)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_25.sizePolicy().hasHeightForWidth())self.label_25.setSizePolicy(sizePolicy)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label_25.setFont(font)self.label_25.setFrameShape(QtWidgets.QFrame.StyledPanel)self.label_25.setFrameShadow(QtWidgets.QFrame.Sunken)self.label_25.setLineWidth(100)self.label_25.setMidLineWidth(20)self.label_25.setAlignment(QtCore.Qt.AlignCenter)self.label_25.setObjectName("label_25")self.gridLayout_9.addWidget(self.label_25, 0, 1, 1, 1)self.gridLayout_2.addLayout(self.gridLayout_9, 0, 0, 1, 1)self.gridLayout.addWidget(self.frame_3, 4, 2, 1, 1)spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)self.gridLayout.addItem(spacerItem, 0, 1, 1, 1)self.verticalLayout = QtWidgets.QVBoxLayout()self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)self.verticalLayout.setSpacing(1)self.verticalLayout.setObjectName("verticalLayout")self.label_28 = QtWidgets.QLabel(self.centralwidget)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_28.sizePolicy().hasHeightForWidth())self.label_28.setSizePolicy(sizePolicy)self.label_28.setText("")self.label_28.setObjectName("label_28")self.verticalLayout.addWidget(self.label_28)self.gridLayout.addLayout(self.verticalLayout, 0, 0, 5, 1)self.frame_2 = QtWidgets.QFrame(self.centralwidget)self.frame_2.setFrameShape(QtWidgets.QFrame.Box)self.frame_2.setFrameShadow(QtWidgets.QFrame.Sunken)self.frame_2.setLineWidth(2)self.frame_2.setObjectName("frame_2")self.gridLayout_12 = QtWidgets.QGridLayout(self.frame_2)self.gridLayout_12.setObjectName("gridLayout_12")self.gridLayout_6 = QtWidgets.QGridLayout()self.gridLayout_6.setHorizontalSpacing(80)self.gridLayout_6.setVerticalSpacing(7)self.gridLayout_6.setObjectName("gridLayout_6")self.progressBar_5 = QtWidgets.QProgressBar(self.frame_2)self.progressBar_5.setProperty("value", 0)self.progressBar_5.setOrientation(QtCore.Qt.Vertical)self.progressBar_5.setObjectName("progressBar_5")self.gridLayout_6.addWidget(self.progressBar_5, 0, 2, 1, 1)self.progressBar_4 = QtWidgets.QProgressBar(self.frame_2)self.progressBar_4.setProperty("value", 0)self.progressBar_4.setOrientation(QtCore.Qt.Vertical)self.progressBar_4.setObjectName("progressBar_4")self.gridLayout_6.addWidget(self.progressBar_4, 0, 1, 1, 1)self.label_16 = QtWidgets.QLabel(self.frame_2)self.label_16.setAlignment(QtCore.Qt.AlignCenter)self.label_16.setObjectName("label_16")self.gridLayout_6.addWidget(self.label_16, 1, 2, 1, 1)self.label_14 = QtWidgets.QLabel(self.frame_2)self.label_14.setAlignment(QtCore.Qt.AlignCenter)self.label_14.setObjectName("label_14")self.gridLayout_6.addWidget(self.label_14, 1, 0, 1, 1)self.progressBar_3 = QtWidgets.QProgressBar(self.frame_2)self.progressBar_3.setProperty("value", 0)self.progressBar_3.setOrientation(QtCore.Qt.Vertical)self.progressBar_3.setObjectName("progressBar_3")self.gridLayout_6.addWidget(self.progressBar_3, 0, 0, 1, 1)self.label_15 = QtWidgets.QLabel(self.frame_2)self.label_15.setAlignment(QtCore.Qt.AlignCenter)self.label_15.setObjectName("label_15")self.gridLayout_6.addWidget(self.label_15, 1, 1, 1, 1)self.progressBar_6 = QtWidgets.QProgressBar(self.frame_2)self.progressBar_6.setProperty("value", 0)self.progressBar_6.setOrientation(QtCore.Qt.Vertical)self.progressBar_6.setObjectName("progressBar_6")self.gridLayout_6.addWidget(self.progressBar_6, 0, 3, 1, 1)self.label_17 = QtWidgets.QLabel(self.frame_2)self.label_17.setAlignment(QtCore.Qt.AlignCenter)self.label_17.setObjectName("label_17")self.gridLayout_6.addWidget(self.label_17, 1, 3, 1, 1)self.gridLayout_12.addLayout(self.gridLayout_6, 0, 0, 2, 1)self.gridLayout_8 = QtWidgets.QGridLayout()self.gridLayout_8.setObjectName("gridLayout_8")self.label_22 = QtWidgets.QLabel(self.frame_2)self.label_22.setObjectName("label_22")self.gridLayout_8.addWidget(self.label_22, 0, 0, 1, 1)self.gridLayout_12.addLayout(self.gridLayout_8, 1, 2, 1, 1)self.gridLayout_7 = QtWidgets.QGridLayout()self.gridLayout_7.setHorizontalSpacing(100)self.gridLayout_7.setVerticalSpacing(0)self.gridLayout_7.setObjectName("gridLayout_7")self.label_18 = QtWidgets.QLabel(self.frame_2)self.label_18.setObjectName("label_18")self.gridLayout_7.addWidget(self.label_18, 0, 0, 1, 1)self.label_19 = QtWidgets.QLabel(self.frame_2)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label_19.setFont(font)self.label_19.setObjectName("label_19")self.gridLayout_7.addWidget(self.label_19, 1, 0, 1, 1)self.label_20 = QtWidgets.QLabel(self.frame_2)self.label_20.setObjectName("label_20")self.gridLayout_7.addWidget(self.label_20, 0, 1, 1, 1)self.label_21 = QtWidgets.QLabel(self.frame_2)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label_21.setFont(font)self.label_21.setObjectName("label_21")self.gridLayout_7.addWidget(self.label_21, 1, 1, 1, 1)self.gridLayout_12.addLayout(self.gridLayout_7, 0, 2, 1, 1)spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Minimum)self.gridLayout_12.addItem(spacerItem1, 0, 1, 1, 1)self.gridLayout.addWidget(self.frame_2, 3, 2, 1, 1)self.frame = QtWidgets.QFrame(self.centralwidget)self.frame.setFrameShape(QtWidgets.QFrame.Box)self.frame.setFrameShadow(QtWidgets.QFrame.Sunken)self.frame.setLineWidth(2)self.frame.setObjectName("frame")self.gridLayout_3 = QtWidgets.QGridLayout(self.frame)self.gridLayout_3.setObjectName("gridLayout_3")self.gridLayout_5 = QtWidgets.QGridLayout()self.gridLayout_5.setHorizontalSpacing(180)self.gridLayout_5.setVerticalSpacing(20)self.gridLayout_5.setObjectName("gridLayout_5")self.label_11 = QtWidgets.QLabel(self.frame)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_11.sizePolicy().hasHeightForWidth())self.label_11.setSizePolicy(sizePolicy)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label_11.setFont(font)self.label_11.setObjectName("label_11")self.gridLayout_5.addWidget(self.label_11, 1, 2, 1, 1)self.label_7 = QtWidgets.QLabel(self.frame)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_7.sizePolicy().hasHeightForWidth())self.label_7.setSizePolicy(sizePolicy)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label_7.setFont(font)self.label_7.setObjectName("label_7")self.gridLayout_5.addWidget(self.label_7, 1, 0, 1, 1)self.label_9 = QtWidgets.QLabel(self.frame)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_9.sizePolicy().hasHeightForWidth())self.label_9.setSizePolicy(sizePolicy)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label_9.setFont(font)self.label_9.setObjectName("label_9")self.gridLayout_5.addWidget(self.label_9, 1, 1, 1, 1)self.label_8 = QtWidgets.QLabel(self.frame)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_8.sizePolicy().hasHeightForWidth())self.label_8.setSizePolicy(sizePolicy)self.label_8.setObjectName("label_8")self.gridLayout_5.addWidget(self.label_8, 0, 1, 1, 1)self.label_10 = QtWidgets.QLabel(self.frame)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_10.sizePolicy().hasHeightForWidth())self.label_10.setSizePolicy(sizePolicy)self.label_10.setObjectName("label_10")self.gridLayout_5.addWidget(self.label_10, 0, 2, 1, 1)self.label_6 = QtWidgets.QLabel(self.frame)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_6.sizePolicy().hasHeightForWidth())self.label_6.setSizePolicy(sizePolicy)self.label_6.setObjectName("label_6")self.gridLayout_5.addWidget(self.label_6, 0, 0, 1, 1)self.label_12 = QtWidgets.QLabel(self.frame)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_12.sizePolicy().hasHeightForWidth())self.label_12.setSizePolicy(sizePolicy)self.label_12.setObjectName("label_12")self.gridLayout_5.addWidget(self.label_12, 0, 3, 1, 1)self.label_13 = QtWidgets.QLabel(self.frame)sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)sizePolicy.setHorizontalStretch(0)sizePolicy.setVerticalStretch(0)sizePolicy.setHeightForWidth(self.label_13.sizePolicy().hasHeightForWidth())self.label_13.setSizePolicy(sizePolicy)font = QtGui.QFont()font.setBold(True)font.setWeight(75)self.label_13.setFont(font)self.label_13.setObjectName("label_13")self.gridLayout_5.addWidget(self.label_13, 1, 3, 1, 1)self.gridLayout_3.addLayout(self.gridLayout_5, 0, 0, 1, 1)self.gridLayout.addWidget(self.frame, 2, 2, 1, 1)spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)self.gridLayout.addItem(spacerItem2, 1, 3, 1, 1)MainWindow.setCentralWidget(self.centralwidget)self.statusbar = QtWidgets.QStatusBar(MainWindow)self.statusbar.setObjectName("statusbar")MainWindow.setStatusBar(self.statusbar)self.retranslateUi(MainWindow)QtCore.QMetaObject.connectSlotsByName(MainWindow)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "智能电池信息"))self.label.setText(_translate("MainWindow", "智能电池信息"))self.label_26.setText(_translate("MainWindow", "10%"))self.label_5.setText(_translate("MainWindow", "00:52"))self.label_2.setText(_translate("MainWindow", "低电量报警"))self.label_3.setText(_translate("MainWindow", "严重低电量报警"))self.label_4.setText(_translate("MainWindow", "飞行时间"))self.label_27.setText(_translate("MainWindow", "30%"))self.pushButton.setText(_translate("MainWindow", "电池历史记录信息"))self.label_23.setText(_translate("MainWindow", "开始自放电时间"))self.label_25.setText(_translate("MainWindow", "10天"))self.label_16.setText(_translate("MainWindow", "0V"))self.label_14.setText(_translate("MainWindow", "0V"))self.label_15.setText(_translate("MainWindow", "0V"))self.label_17.setText(_translate("MainWindow", "0V"))self.label_22.setText(_translate("MainWindow", "主屏显示电压"))self.label_18.setText(_translate("MainWindow", "电池寿命"))self.label_19.setText(_translate("MainWindow", "100%"))self.label_20.setText(_translate("MainWindow", "循环次数"))self.label_21.setText(_translate("MainWindow", "3"))self.label_11.setText(_translate("MainWindow", "4471 mAH"))self.label_7.setText(_translate("MainWindow", "15.18 V"))self.label_9.setText(_translate("MainWindow", "1922 mAH"))self.label_8.setText(_translate("MainWindow", "当前电量"))self.label_10.setText(_translate("MainWindow", "电池容量"))self.label_6.setText(_translate("MainWindow", "电压"))self.label_12.setText(_translate("MainWindow", "温度"))self.label_13.setText(_translate("MainWindow", "40 ℃"))