import sys
from PyQt5.QtWidgets import QPushButton, QApplication,QDesktopWidget
from PyQt5.QtWidgets import QWidget
from PyQt5 import QtCore, QtGui, QtWidgets, Qt
import time
currentTime = time.strftime("%H:%M %p")
class Toast(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('toast')
self.setWindowFlags(Qt.Qt.FramelessWindowHint)
self.setStyleSheet("background-color:#3A4659;\n")
self.gridLayout_3 = QtWidgets.QGridLayout(self)
self.gridLayout_3.setContentsMargins(0, 0, 0, 0)
self.gridLayout_3.setObjectName("gridLayout_3")
self.frame = QtWidgets.QFrame(self)
self.frame.setObjectName("frame")
self.gridLayout_2 = QtWidgets.QGridLayout(self.frame)
self.gridLayout_2.setObjectName("gridLayout_2")
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.passLabel = QtWidgets.QLabel(self.frame)
self.passLabel.setMinimumSize(QtCore.QSize(41, 41))
self.passLabel.setMaximumSize(QtCore.QSize(41, 41))
self.passLabel.setObjectName("label_4")
self.horizontalLayout.addWidget(self.passLabel)
self.gridLayout = QtWidgets.QGridLayout()
self.gridLayout.setObjectName("gridLayout")
self.alertsLabel = QtWidgets.QLabel(self.frame)
self.alertsLabel.setMinimumSize(QtCore.QSize(200, 0))
self.alertsLabel.setStyleSheet("font: 20px\"微软雅黑\";color:white")
self.alertsLabel.setText("消息通知")
self.alertsLabel.setObjectName("label")
self.gridLayout.addWidget(self.alertsLabel, 0, 0, 1, 1)
self.timeLabel = QtWidgets.QLabel(self.frame)
self.timeLabel.setStyleSheet("font: 15px\"微软雅黑\";color:white")
self.timeLabel.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.timeLabel.setText(currentTime)
self.timeLabel.setObjectName("label_3")
self.gridLayout.addWidget(self.timeLabel, 0, 1, 1, 1)
self.toastLabel = QtWidgets.QLabel(self.frame)
self.toastLabel.setStyleSheet("font: 15px\"微软雅黑\";color:white")
self.toastLabel.setObjectName("label")
self.gridLayout.addWidget(self.toastLabel, 1, 0, 1, 1)
self.horizontalLayout.addLayout(self.gridLayout)
self.gridLayout_2.addLayout(self.horizontalLayout, 0, 0, 1, 1)
self.gridLayout_3.addWidget(self.frame, 0, 0, 1, 1)
class WinForm(QWidget):
def __init__(self, parent=None):
super(WinForm, self).__init__(parent)
self.setGeometry(300, 300, 350, 350)
self.resize(350,350)
self.setWindowTitle('点击按钮开启弹窗')
quit = QPushButton('开启弹窗', self)
quit.setGeometry(10, 10, 100, 35)
quit.setStyleSheet("background-color: red")
quit.clicked.connect(self.toast)
def toast(self):
self.ui = Toast()
self.ui.show()
QtCore.QTimer().singleShot(2000, self.ui.close)
self.ui.toastLabel.setText('保存成功')
self.ui.passLabel.setPixmap(QtGui.QPixmap("images/pass.png"))
screen = QDesktopWidget().screenGeometry()
size = self.ui.geometry()
self.ui.move((screen.width() - size.width()) / 2,(screen.height() - size.height()) / 2)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = WinForm()
win.show()
sys.exit(app.exec_())