1 Star 0 Fork 0

godonce/Python_MoFish

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
main.py 5.60 KB
一键复制 编辑 原始数据 按行查看 历史
godonce 提交于 2023-06-02 16:33 . [Title]20230602_版本V1.0入库
import tkinter # 绘制操作界面
import requests
from bs4 import BeautifulSoup
from config import cfgJsonRW
from tray import sysTray
class winExec(object):
def __init__(self, win_main, win_cfg, win_content, all_page):
self.root = win_main
self.cfg = win_cfg
self.con = win_content
self.root.bind("<Button-1>", self.mouseDown) # 按下鼠标左键绑定MouseDown函数
self.root.bind("<B1-Motion>", self.mouseMove) # 鼠标左键按住拖曳事件,3个函数都不要忘记函数写参数
self.root.bind("<Double-Button-1>", self.commExit) # 双击鼠标左键,关闭窗体
self.root.bind("<Escape>", self.commExit) # 退出
self.root.bind(self.cfg.key_boss, self.keyBoss) #按下键盘隐藏窗口
self.root.bind(self.cfg.key_up, self.keyUp) #向上翻页
self.root.bind(self.cfg.key_down, self.keyDown) #向下翻页
self.boss_flag = 0
self.now_page = self.cfg.page_now
self.all_page = all_page
def mouseDown(self, event): #鼠标按下
self.mousX, self.mousY = event.x, event.y # 获取鼠标相对于窗体左上角的X/Y坐标
def mouseMove(self, event): #鼠标移动
self.root.geometry(f'+{event.x_root - self.mousX}+{event.y_root - self.mousY}') # 窗体移动代码(event.x_root/event.y_root为窗体相对于屏幕左上角的X/Y)
def commExit(self, event): #通用退出(双击或者Esc)
self.root.destroy()
def keyBoss(self, event): #老板键
if self.boss_flag == 0:
self.root.attributes("-alpha", 0)
self.boss_flag = 1
else:
self.root.attributes("-alpha", 1)
self.boss_flag = 0
def keyUp(self, event): #向上翻页
self.now_page = self.now_page - 1
if self.cfg.is_prog == 0:
info.set(self.con[self.now_page * self.cfg.char_num : (self.now_page + 1) * self.cfg.char_num])
else:
info.set(self.con[self.now_page * self.cfg.char_num : (self.now_page + 1) * self.cfg.char_num] + " (%d/%d)" % (self.now_page, self.all_page))
self.cfg.cfgJsonWritePage(self.now_page) #当前页写json文件存储
def keyDown(self, event): #向下翻页
self.now_page = self.now_page + 1
if self.cfg.is_prog == 0:
info.set(self.con[self.now_page * self.cfg.char_num : (self.now_page + 1) * self.cfg.char_num])
else:
info.set(self.con[self.now_page * self.cfg.char_num : (self.now_page + 1) * self.cfg.char_num] + " (%d/%d)" % (self.now_page, self.all_page))
self.cfg.cfgJsonWritePage(self.now_page) #当前页写json文件存储
def readFile(file_name, coding_mode, char_num): #解析文件内容(输入参数:文件名/编码方式/每页字数)(示例文件:test.txt)
try:
with open(file_name, encoding=coding_mode, errors='ignore') as file_obj:
contents = file_obj.read()
except FileNotFoundError:
msg = 'Sorry, the file ' + file_name + ' does not exist.'
print(msg)
else:
words = contents.replace("\n", "").replace(" ", "") #删除换行符和空格
num_words = len(words)
all_page = num_words // char_num + 1
print("----读取文件 %s----\n共 %s 字.按照 %d 个字分页,最大 %d 页----" % (file_name, str(num_words), char_num, all_page))
return words, all_page
def readWeb(file_name, char_num, web_num): #解析网页内容(输入参数:文件名/每页字数/一次性加载网页章节数)(示例网页:http://book.zongheng.com/chapter/897468/58575172.html)
words = ""
url = file_name
for web_id in range(web_num):
resp = requests.get(url=url)
html = resp.text
soup = BeautifulSoup(html, "html.parser")
for i in range(0, len(soup.find_all('p'))): #读取网页中的文本内容
words = words + soup.find_all('p')[i].string
next_url = soup.find("a", class_="nextchapter") #获得下一章的链接用于下一次循环加载
if next_url:
url = next_url["href"]
print("----读取第 %d 个网页----\n" % web_id)
else:
break
num_words = len(words)
all_page = num_words // char_num + 1
print("----读取起始网页 %s 开始的 %d 章节----\n共 %s 字.按照 %d 个字分页,最大 %d 页----" % (file_name, web_num, str(num_words), char_num, all_page))
return words, all_page
win = tkinter.Tk()
win_cfg = cfgJsonRW()
if "http" in win_cfg.file_name:
win_content, all_page = readWeb(win_cfg.file_name, win_cfg.char_num, win_cfg.web_num) # 读取网页(输入参数:文件名/每页字数/一次性加载网页章节数)
else:
win_content, all_page = readFile(win_cfg.file_name, win_cfg.coding_mode, win_cfg.char_num) # 读取文件(输入参数:文件名/编码方式/每页字数)
win_exec = winExec(win, win_cfg, win_content, all_page)
win.overrideredirect(True) # 实现隐藏了整个标题栏的窗口
win.attributes("-transparentcolor", 'snow') #将snow颜色设置为透明色,可以替换不同的颜色
win.attributes("-topmost", True) # 将窗口保持最前
info = tkinter.StringVar()
info.set("Welcome to MoFish !\n" + win_cfg.cfg_info)
text = tkinter.Label(win, anchor="nw", justify='left', bg='snow', wraplength=win_cfg.label_width, fg=win_cfg.font_color, font=("", win_cfg.font_size), textvariable=info).pack() #把已经变成透明色的snow色设置为背景
sysTray(win, win_cfg, win_exec).createSysTray() # 创建最小化托盘对象
win.mainloop()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/zhang-xuanchi/Python_MoFish.git
git@gitee.com:zhang-xuanchi/Python_MoFish.git
zhang-xuanchi
Python_MoFish
Python_MoFish
master

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385