这篇文章主要介绍“怎么用PyQT5制作一个桌面摸鱼工具”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“怎么用PyQT5制作一个桌面摸鱼工具”文章能帮助大家解决问题。
q 退出
B 书签功能
F 增加字体大小
Shift F 减小字体
O 打开文件,现在仅仅支持 utf8格式的txt文件
FlameLess Window 无边框窗口
一键快速退出
ini 文件读写
右键上下文菜单
pyqt 实现功能还是比较顺畅的,总体功能实现代码量不到200行
from PyQt5 import QtCore from PyQt5.QtWidgets import * from PyQt5.QtGui import * from PyQt5.QtCore import Qt import sys,os import configparser # Q to quit app # B Bookmark # F increase Font size # Shift F decrease Font size # O Open *.txt file class FisherReader(QMainWindow): def __init__(self): super().__init__() # drag self.pos =[0,0] self.mouse_down = False self.down = [0,0] self.prev = [0,0] # text self.txtName = '' self.text = [] self.index = 0 # style self.show_info = False self.font_size = 8 self.bgColor = QColor(255,255,255) self.defPalette() # self.read_Txt() def mousePressEvent(self, event): current = [event.pos().x(),event.pos().y()] self.down = current self.mouse_down = True def mouseMoveEvent(self,event): current = [event.pos().x(),event.pos().y()] if self.mouse_down: delta = [current[0]-self.down[0],current[1]-self.down[1]] new = [self.pos[0]+delta[0],self.pos[1]+delta[1]] self.move(new[0],new[1]) self.pos = new # print(self.pos) self.prev = current def mouseReleaseEvent(self, event): self.mouse_down = False def keyPressEvent(self,event): if event.key() == Qt.Key_Q: app.quit() if event.key() == Qt.Key_Down: if self.index < len(self.text)-1: self.index = self.index+1 self.update() if event.key() == Qt.Key_Up: if self.index > 0: self.index = self.index-1 self.update() if event.key() == Qt.Key_F: if event.modifiers() & QtCore.Qt.ShiftModifier and self.font_size >2: self.font_size -= 2 else: self.font_size += 2 self.update() if event.key() == Qt.Key_I: self.show_info = not self.show_info self.update() if event.key() == Qt.Key_O: self.open() self.update() if event.key() == Qt.Key_B: self.addBookmark() if event.key() == Qt.Key_R: self.getBookmark() def defPalette(self): p = self.palette() p.setColor(QPalette.Background,self.bgColor) self.window().setPalette(p) def paintEvent(self,event): painter = QPainter(self) painter.setRenderHints(QPainter.Antialiasing) if len(self.text)>0: painter.setFont(QFont('SimSun',self.font_size)) painter.drawText(QtCore.QRectF(10,10,600,50),Qt.AlignLeft,self.text[self.index]) if self.show_info: painter.drawText(QtCore.QRectF(610,10,50,50),Qt.AlignLeft,"{}/{}".format(self.index+1,len(self.text))) def open(self): path, _ = QFileDialog.getOpenFileName(self, "打开文件",os.getcwd(), "Text files (*.txt)") if path: self.txtName = path self.read_Txt_smart(path) self.update() def read_Txt(self,file): with open(file,'r',encoding="UTF-8") as f: self.text = f.readlines() def cut(self,text,length): return [text[i:i+length] for i in range(0,len(text),length)] def wheelEvent(self, e): if e.angleDelta().y() < 0: if self.index < len(self.text)-1: self.index = self.index+1 elif e.angleDelta().y() > 0: if self.index > 0: self.index = self.index-1 self.update() def addBookmark(self): config = configparser.ConfigParser() path = "bookmark.ini" config.add_section('bookmark') config.set('bookmark','path',self.txtName) config.set('bookmark','bookmark',str(self.index)) config.write(open(path,'w')) def getBookmark(self): config = configparser.ConfigParser() path = "bookmark.ini" config.read(path) if config.has_option('bookmark','path'): self.txtName = config.get('bookmark','path') self.index = int(config.get('bookmark','bookmark')) self.read_Txt_smart(self.txtName); self.update() def read_Txt_smart(self,file): with open(file,'r',encoding="UTF-8") as f: text_buffer = [] lines = f.readlines() for line in lines: cline = self.cut(line,30) for cl in cline: if len(cl)>1: text_buffer.append(cl) self.text = text_buffer if __name__ == '__main__': app = QApplication(sys.argv) fisher = FisherReader() fisher.resize(660,45) fisher.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint) fisher.show() fisher.setWindowTitle("小鱼") sys.exit(app.exec_())
关于“怎么用PyQT5制作一个桌面摸鱼工具”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。