Python Pyqt5 程序放置托盘区 点击托盘区图标执行

# 将某个程序放置再托盘区,并且可以点击托盘区图标执行某个方法
class ThirdWindow(QDialog):

    @classmethod
    def instance(cls, *args, **kwargs):
        if not hasattr(ThirdWindow, "_instance"):
            ThirdWindow._instance = ThirdWindow(*args, **kwargs)
        return ThirdWindow._instance

    def __init__(self):
        super(ThirdWindow, self).__init__()
        # 成功弹窗
        self.fourth = FourthWindow.instance()
        # self.resize(55, 180)
        # self.setVisible(False)
        # progrem_size = self.geometry()
        # self.left = PC_WIDTH - progrem_size.width()
        # self.top = PC_HEIGHT - progrem_size.height()
        # self.move(self.left, self.top - 50)
        #
        # # 截图按钮btn1
        # self.btn = QPushButton(self)
        # self.btn.resize(55, 154)
        # get_pic("shot_menu.png")
        # self.btn.setStyleSheet("background-image: url(shot_menu.png)")
        # self.btn.installEventFilter(self)
        # self.btn.setCursor(QtCore.Qt.PointingHandCursor)
        #
        # # 最小化按钮btn2
        # self.btn2 = QPushButton(self)
        # self.btn2.resize(55, 26)
        # self.btn2.move(0, 154)
        # get_pic("shot_min.png")
        # self.btn2.setStyleSheet("background-image: url(shot_min.png)")
        # self.btn2.clicked.connect(self.set_windows_min)
        self.initUI()

    def initUI(self):
        # 托盘内图标,以及托盘内图标绑点点击事件
        get_pic("shot_sys_menu.png")
        self.sysIcon = QIcon('shot_sys_menu.png')
        self.trayIcon = QSystemTrayIcon(self)
        self.trayIcon.setIcon(self.sysIcon)
        self.trayIcon.setToolTip("点击截图全屏,\n截图后将自动保存至“证据附件”")
        self.trayIcon.activated.connect(self.set_windows_show)
        self.trayIcon.show()
        # 置顶,无选项栏
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.SplashScreen)  # 置顶

    def set_windows_min(self):
        # 窗体不可见,最小化的图标被隐藏起来了(非托盘区的)
        self.setVisible(False)

    def set_windows_show(self):
        # 窗体恢复原样
        # self.showNormal()
        # 直接点击托盘里的图标进行截屏
        self.work()

    def handle_click(self):
        if not self.isVisible():
            self.show()

    def eventFilter(self, object, event):
        """
        按钮设置鼠标时间,移入移出修改样式,点击整个窗体隐藏并且截图
        """
        if event.type() == QtCore.QEvent.HoverMove:
            get_pic("shot_menu.png")
            self.btn.setStyleSheet("background-image: url(shot_menu.png)")
            return True
        elif event.type() == QtCore.QEvent.HoverLeave:  # 鼠标移出
            get_pic("shot_menu.png")
            self.btn.setStyleSheet("background-image: url(shot_menu.png)")
            self.setVisible(True)
            return True

        elif event.type() == QtCore.QEvent.MouseButtonPress:  # 鼠标点击事件   1、窗口隐藏 2、截图
            self.setVisible(False)
            self.work()
            return True

        return False

    def work(self):
        """
        截图,保存到指定位置
        并且将图片复制到剪切板上
        """
        im = ImageGrab.grab()
        pic_path = str(int(time.time())) + '.png'
        im.save(r"C:\Users\default.LAPTOP-6L6GGMK1\Desktop\Work\environment-clear\ui\\" + pic_path, 'png')
        # im.save(r"C:\Users\baoquanuser\Documents\userfiles\\" + pic_path, 'png')
        # self.fourth.move(self.left - 5, self.top - 100)
        # 点击托盘的图标截屏的时候,窗口四的位置需要调整
        progrem_size = self.fourth.geometry()
        self.left = PC_WIDTH - progrem_size.width()
        self.top = PC_HEIGHT - progrem_size.height()
        self.fourth.move(self.left - 140, self.top - 40)  # 为了将截图成功弹窗放置在截图功能之上
        self.fourth.work()
        # self.copypic2clipboard(r"C:\Users\baoquanuser\Documents\userfiles\\" + pic_path)
        self.copypic2clipboard(r"C:\Users\default.LAPTOP-6L6GGMK1\Desktop\Work\environment-clear\ui\\" + pic_path)

    def copypic2clipboard(self, filepath):
        image = Image.open(filepath)
        output = BytesIO()
        image.convert("RGB").save(output, "BMP")
        data = output.getvalue()[14:]
        output.close()
        self.send_to_clipboard(win32clipboard.CF_DIB, data)

    def send_to_clipboard(self, clip_type, data):
        win32clipboard.OpenClipboard()
        win32clipboard.EmptyClipboard()
        win32clipboard.SetClipboardData(clip_type, data)
        win32clipboard.CloseClipboard()


class FourthWindow(QWidget):
    """
    截图成功弹窗
    截图成功后显示此窗口,在0.8s后自动退出
    """
    @classmethod
    def instance(cls, *args, **kwargs):
        if not hasattr(FourthWindow, "_instance"):
            FourthWindow._instance = FourthWindow(*args, **kwargs)
        return FourthWindow._instance

    def __init__(self):
        super(FourthWindow, self).__init__()
        self.resize(96, 40)
        self.setWindowFlags(Qt.FramelessWindowHint | QtCore.Qt.WindowStaysOnTopHint)
        self.lable0 = QLabel(self)
        self.lable0.resize(96, 40)
        get_pic("shot_success.png")
        self.lable0.setStyleSheet("background: url('shot_success.png');")

        self.time = QTimer(self)
        self.time.setInterval(800)
        self.time.timeout.connect(self.Refresh)

    def work(self):
        self.setVisible(True)
        self.time.start()

    def Refresh(self):
        self.setVisible(False)


if __name__ == "__main__":
    App = QtWidgets.QApplication(sys.argv)
    if not QSystemTrayIcon.isSystemTrayAvailable():
        QMessageBox.critical(None, '系统托盘', '本系统不支持托盘功能')
        sys.exit(1)

    QApplication.setQuitOnLastWindowClosed(False)
    first = ThirdWindow()
    # first = FourthWindow()
    # first.show()
    sys.exit(App.exec_())