from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtWidgets import * from views.Ui_caller_main import Ui_caller_main from controllers.assembly.tray_icon import TrayIcon import caller_src_rc class CallerMain(QWidget): def __init__(self): super().__init__() self.ui = Ui_caller_main() self.ui.setupUi(self) # 去除边框 self.setWindowFlag(Qt.FramelessWindowHint) self.setAttribute(Qt.WA_TranslucentBackground) # 将界面属性设置为半透明 # 设定一个阴影:半径为10 颜色为#444 定位为0,0 self.shadow = QGraphicsDropShadowEffect() self.shadow.setBlurRadius(10) self.shadow.setColor(QColor('#444')) self.shadow.setOffset(0, 0) self.ui.f_caller_main.setGraphicsEffect(self.shadow) # 动态添加右键菜单 1打开右键菜单策略 2绑定事件 self.ui.f_caller_main.setContextMenuPolicy(Qt.CustomContextMenu) self.ui.f_caller_main.customContextMenuRequested.connect( self.show_context_menu) # 显示右下角托盘 self.tray = TrayIcon(self) self.tray.show() # 重写鼠标按下事件 左键按下时获取鼠标坐标,按下右键取消 def mousePressEvent(self, event): if event.button() == Qt.LeftButton: self.m_flag = True self.m_Position = event.globalPos() - self.pos() event.accept() elif event.button() == Qt.RightButton: self.m_flag = False # 重写鼠标移动事件 在按下左键移动是根据坐标移动界面 def mouseMoveEvent(self, QMouseEvent): if Qt.LeftButton and self.m_flag: self.move(QMouseEvent.globalPos() - self.m_Position) QMouseEvent.accept() # 重写鼠标释放事件 当鼠标按键释放时 取消移动 def mouseReleaseEvent(self, QMouseEvent): self.m_flag = False # 创建右键菜单方法 def show_context_menu(self, pos): menu = QMenu(self) action = menu.addAction('方法1') action = menu.addAction('退出') menu.exec_(QCursor.pos())