基于Python-PyQt5实现的桌宠

# !/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
@author  : v_jiaohaicheng@baidu.com
@des     :癞皮狗桌宠

"""
import sys
import time

from PyQt5 import QtGui
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QApplication, QDialog, QLabel,QWidget
from PyQt5.QtCore import Qt
import threading


WINDOW_X_SIZE = 160
WINDOW_Y_SIZE = 120

TIP_X = 140
TIP_Y = 30

TIP_TXT_OFFSET_X = 10
TIP_TXT_OFFSET_Y = 10

TIP_FLAG = False


class MyDialog(QDialog):
    def __init__(self, Text, x, y):
        super().__init__()
        # 设置窗口标志,去除边框
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setGeometry(x, y - TIP_Y, TIP_X, TIP_Y)
        # 创建标签控件,用于显示文本
        self.label = QLabel(self)
        self.label.setGeometry(
            TIP_TXT_OFFSET_X,
            TIP_TXT_OFFSET_Y,
            TIP_X,
            TIP_Y)
        # 设置文本
        self.label.setText(Text)
        self.label.setAlignment(Qt.AlignLeft)
        self.label.setStyleSheet("background:transparent")
        self.label.setAttribute(Qt.WA_TranslucentBackground)

        self.setModal(True)

    def mouseDoubleClickEvent(self, event):
        self.close()

    def hideEvent(self, event):
        global TIP_FLAG
        TIP_FLAG = False
        print("隐藏界面")

    def closeEvent(self, event):
        global TIP_FLAG
        TIP_FLAG = False
        print("关闭界面")


class MainWindow(QWidget):
    # single_exit = pyqtSignal(str)
    def __init__(self):
        super().__init__()
        desktop = QApplication.desktop()
        print(desktop.width(),desktop.height())
        self.x, self.y = 10, 10
        # 设置窗口为无边框,透明背景,置顶窗口
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint)
        self.setAttribute(Qt.WA_TranslucentBackground)
        # 设置窗口大小和位置
        self.setGeometry(self.x, self.y, WINDOW_X_SIZE, WINDOW_Y_SIZE)
        # 创建标签控件,用于显示背景图片
        self.bg_label = QLabel(self)
        self.bg_label.setGeometry(self.x, self.y, WINDOW_X_SIZE, WINDOW_Y_SIZE)

        pixmap = QPixmap("pet.png")
        self.bg_label.setPixmap(pixmap)
        self.bg_label.setScaledContents(True)

        self.flag_enter = False
        self.t = ""
        # self.single_exit.connect(self.exit_thread)
        t2 = threading.Thread(target=self.stop)
        t2.start()

    def enterEvent(self, event):
        self.flag_enter = True

    def leaveEvent(self, event):
        self.flag_enter = False

    def mousePressEvent(self, event):
        # 当鼠标按下时,记录当前鼠标位置和窗口位置
        if self.flag_enter:
            if event.button() == Qt.LeftButton:
                self.drag_position = event.globalPos() - self.pos()
            elif event.button() == Qt.RightButton:
                if self.x >= TIP_X and self.y >= TIP_Y:
                    try:
                        global TIP_FLAG
                        TIP_FLAG = True
                        self.t = threading.Thread(
                            target=self.show_tips, args=(
                                "癞皮狗在此(^_^)", self.x, self.y,))
                        self.t.start()
                    except BaseException:
                        pass

                pass

    def stop(self):
        while True:
            global TIP_FLAG
            while self.t != "":
                while TIP_FLAG:
                    time.sleep(0.1)
                self.t.join(0)
                self.t = ""
            time.sleep(0.1)

    def show_tips(self, text, x, y):
        app = QApplication(sys.argv)
        window = MyDialog(text, x, y)
        window.show()
        sys.exit(app.exec_())

    def mouseMoveEvent(self, event):
        # 当鼠标移动时,更新窗口位置
        new_opt = event.globalPos() - self.drag_position
        self.x, self.y = new_opt.x(), new_opt.y()
        self.move(new_opt)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值