此博客中涉及到的知识点
PyQt设置窗口完全透明但内部控件不透明的方法动态更新控件的方法PyQt 给窗口设置一个右键菜单PyQt 实现鼠标拖动窗口文件操作字符串方法
这里以我写的小程序为例 介绍前4个方法如何实现
PyQt设置窗口完全透明但内部控件不透明的方法
在窗口初始化时加上下面两句话即可
self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool)self.setAttribute(Qt.WA_TranslucentBackground)
动态更新控件(控件个数及内容)的方法
Qt提供了很好的方法去做这种操作,前提是你控件中的个数不需要动态地去改变,有兴趣可以去了解一下,我这里并没有采用这种主流的方法,而是利用类属性去记录子控件引用的方法。在源代码Remind类 函数show_()中得到了体现。
PyQt 给窗口设置一个右键菜单/鼠标拖动
这里只需要重写其父类的contextMenuEvent(self,event)方法就好,示例:
class Remind(QDialog):def __init__(self, parent=None):super(Remind, self).__init__(parent)self.move_point = 0self.mouse_press = False# 添加右键菜单def contextMenuEvent(self, event):menu = QMenu(self)add_remind = QAction('添加今日提醒事项')if self.load_file_tag == True:add_remind.setText('更换提醒事项')menu.addAction(add_remind)add_remind.triggered.connect(self.loadfile)clo = menu.addAction('关闭')clo.triggered.connect(self.F_close)menu.exec_(event.globalPos()) # 获取当前光标# 以下三个函数为实现鼠标拖动窗口功能def mousePressEvent(self, event):if event.button() == Qt.LeftButton:self.setMouseTracking(True)self.mouse_press = Trueself.move_point = event.pos()def mouseReleaseEvent(self, event):self.mouse_press = Falsedef mouseMoveEvent(self, event):if self.mouse_press:move_pos = event.globalPos()self.move(move_pos - self.move_point)
下面给出桌面提醒事项的打包好的小程序和源代码
# encoding:utf-8from PyQt5.QtWidgets import *from PyQt5.QtGui import *from PyQt5.QtCore import *import sysclass Remind(QDialog):def __init__(self, parent=None):super(Remind, self).__init__(parent)self.move_point = 0self.mouse_press = Falseself.load_file_tag = Falseself.settings = {'title': '今日提醒', 'color': 'red', 'font': '楷体', 'size': 30, 'remindlist': []}self.label_list = []self.layout = QVBoxLayout()self.title = QLabel("右键添加今日目标")self.title.setFont(QFont(self.settings['font'], self.settings['size']))self.title.setAlignment(Qt.AlignCenter)self.title.setStyleSheet("color:red")self.layout.addWidget(self.title)self.setLayout(self.layout)# 设置窗口透明,控件不透明self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool)self.setAttribute(Qt.WA_TranslucentBackground)# end# 以下三个函数为实现鼠标拖动窗口功能def mousePressEvent(self, event):if event.button() == Qt.LeftButton:self.setMouseTracking(True)self.mouse_press = Trueself.move_point = event.pos()def mouseReleaseEvent(self, event):self.mouse_press = Falsedef mouseMoveEvent(self, event):if self.mouse_press:move_pos = event.globalPos()self.move(move_pos - self.move_point)# 添加右键菜单def contextMenuEvent(self, event):menu = QMenu(self)add_remind = QAction('添加今日提醒事项')if self.load_file_tag == True:add_remind.setText('更换提醒事项')menu.addAction(add_remind)add_remind.triggered.connect(self.loadfile)clo = menu.addAction('关闭')clo.triggered.connect(self.F_close)menu.exec_(event.globalPos()) # 获取当前光标def loadfile(self):f_name = QFileDialog.getOpenFileName(self, '添加今日提醒事项', '//', "提醒事项(*.txt)")if self.load_file_tag == True: # 如果已经读取过一次,则覆盖self.settings['remindlist'].clear()if f_name[0] == '':passelse:with open(f_name[0], 'r') as fp:remindlist = fp.readlines()# 对读取的文件进行规格化:list_start = 0 # 具体事项从第0行开始if remindlist == []:QMessageBox.about(self, '提示', '该文件为空,请重新选择!')else:if remindlist[0][0] == '#':list_start = 1parm = remindlist[0][1:-1].split(' ') # 过滤掉首字母'#'和尾字母'\n'parm = [i.split('=') for i in parm]for j in parm[:-1]:if j[0] in self.settings: # 如果用户添加了可设置属性 则更改self.settings[j[0]] = j[1]# 添加具体的事项for i in remindlist[list_start:-1]:self.settings['remindlist'].append(i[:-1])if remindlist[-1][0] != '#':self.settings['remindlist'].append(remindlist[-1])self.show_() # 参数读取完成,将参数信息显示到界面上def show_(self):# 更新title信息self.title.setText(self.settings['title'])for i in self.label_list: # 先把上一次的label清除i.clear()self.layout.removeWidget(i)self.title.setText(self.settings['title'])self.title.setFont(QFont(self.settings['font'], int(self.settings['size'])))self.title.setStyleSheet("color:" + self.settings['color'])# 显示具体的提醒事项if self.settings['remindlist'] == []:QMessageBox.about(self, '提示', '该文件无具体待办事项!')else:for i in self.settings['remindlist']:buf = QLabel('·' + i)buf.setFont(QFont(self.settings['font'], int(self.settings['size'])))buf.setStyleSheet("color:" + self.settings['color'])self.label_list.append(buf)self.layout.addWidget(buf)self.load_file_tag = True # 真正在窗口显示了才算一次读取操作print(self.layout.count())def F_close(self):sys.exit()if __name__ == '__main__':app = QApplication(sys.argv)demo = Remind()demo.show()sys.exit(app.exec_())
在本地文档中填写提醒事项及结果
下面为提醒事项填写格式,其中第一行可以省略,属性也可以缺省
运行展示
如果觉得《在桌面上显示你的每日计划(提醒事项)》对你有帮助,请点赞、收藏,并留下你的观点哦!