欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于PyQT5制作一个桌面摸鱼工具

程序员文章站 2022-01-03 19:05:12
目录前言按键功能控制主要功能核心代码前言现在我能一整天都严肃地盯着屏幕,看起来就像在很认真地工作,利用摸鱼,打开小说,可实行完美摸鱼,实时保存进度用pyqt5 mock一个摸鱼软件 类似于thief按...

前言

现在我能一整天都严肃地盯着屏幕,看起来就像在很认真地工作,

利用摸鱼,打开小说,可实行完美摸鱼,实时保存进度

用pyqt5 mock一个摸鱼软件 类似于thief

按键功能控制

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制作一个桌面摸鱼工具的文章就介绍到这了,更多相关pyqt5桌面摸鱼工具内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!