基于Python-PyQt5实现的桌宠

得意时要看淡,失意时要看开。不论得意失意,切莫大意;不论成功失败,切莫止步。志得意满时,需要的是淡然,给自己留一条退路;失意落魄时,需要的是泰然,给自己觅一条出路基于Python-PyQt5实现的桌宠,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com,来源:原文

# !/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_())

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/156862.html

(0)
飞熊的头像飞熊bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!