Compare commits

8 Commits
master ... dev

132
main.py
View File

@@ -2,7 +2,7 @@ from tkinter import *
from tkinter import filedialog from tkinter import filedialog
from zipfile import ZipFile as zf from zipfile import ZipFile as zf
from PIL import Image, ImageTk from PIL import Image, ImageTk
import io import io, os, pickle
class MangaReader: class MangaReader:
def __init__(self, master): def __init__(self, master):
@@ -11,81 +11,94 @@ class MangaReader:
self.total = 0 self.total = 0
self.file = 'None' self.file = 'None'
self.img = '' self.img = ''
self.dir = 'None'
self.nextKey = '<Right>'
self.backKey = '<Left>'
self.quitKey = 'q'
self.openKey = 'o'
master.title("Manga Reader") master.title("Manga Reader")
#Load settings
try:
settingLoad = pickle.load(open('manga.p', 'rb'))
self.dir = settingLoad['directory']
self.nextKey = settingLoad['nextKey']
self.backKey = settingLoad['backKey']
self.quitKey = settingLoad['quitKey']
self.openKey = settingLoad['openKey']
except:
pass
# tk window size and allow resize # tk window size and allow resize
master.geometry("700x900") master.geometry("700x900")
master.resizable(width=True, height=True) master.resizable(width=True, height=True)
# Menu Bar # Menu Bar
self.menu = Menu(master) self.menu = Menu(master)
item = Menu(self.menu) item = Menu(self.menu)
item.add_command(label="Open", command=self.open_menu) item.add_command(label="Open", command=self.open_menu)
item.add_separator() item.add_separator()
item.add_command(label="Exit", command=master.quit) item.add_command(label="Exit", command=self.shutdown)
self.menu.add_cascade(label="File", menu=item) self.menu.add_cascade(label="File", menu=item)
root.config(menu=self.menu) root.config(menu=self.menu)
# Frames
self.buttonFrame = Frame(master) self.buttonFrame = Frame(master)
self.mangaFrame = Frame(master, bg="green")
self.titleFrame = Frame(master)
# forward/back buttons # forward/back buttons
self.nextPg = Button(self.buttonFrame, text='>') self.nextPg = Button(self.buttonFrame, text='>')
self.nextPg.bind('<Button-1>', self.nextPage) self.nextPg.bind('<Button-1>', self.nextPage)
self.nextPg.grid(row=0, column=2) self.nextPg.grid(row=1, column=2)
self.buttonFrame.grid_rowconfigure(0, weight=1) self.buttonFrame.pack(side=BOTTOM)
self.buttonFrame.grid_columnconfigure(2,weight=1)
self.buttonFrame.grid(row=2, column=0, columnspan=2)
self.master.grid_rowconfigure(2, weight=1)
self.master.grid_columnconfigure(0,weight=1)
# self.buttonFrame.pack(side=BOTTOM)
self.backPg = Button(self.buttonFrame, text='<') self.backPg = Button(self.buttonFrame, text='<')
self.backPg.bind('<Button-1>', self.backPage) self.backPg.bind('<Button-1>', self.backPage)
self.backPg.grid(row=0, column=0) self.backPg.grid(row=1, column=0)
self.buttonFrame.grid_rowconfigure(0, weight=1)
self.buttonFrame.grid_columnconfigure(0,weight=1)
# page counter # page counter
self.pgcount = Label(self.buttonFrame, text= str(self.page) + " of " + str(self.total)) self.pgcount = Label(self.buttonFrame, text= str(self.page) + " of " + str(self.total))
self.pgcount.grid(row=1, columns=1, columnspan=2) self.pgcount.grid(row=1, column=1, columnspan=2, pady=(20,20))
self.buttonFrame.grid_rowconfigure(1, weight=1)
self.buttonFrame.grid_columnconfigure(1,weight=1)
# title frame self.mangaTitle = Label(self.buttonFrame, text=os.path.basename(self.file))
self.mangaTitle.grid(row=0, column=1)
self.titleFrame.grid(row=0, column=0, sticky="EW")
self.master.grid_rowconfigure(0, weight=1)
self.master.grid_columnconfigure(0,weight=1)
self.mangaTitle = Label(self.titleFrame, text=self.file)
self.mangaTitle.grid(row=0, sticky="EW")
self.titleFrame.grid_rowconfigure(0,weight=1)
self.titleFrame.grid_columnconfigure(0,weight=1)
# display image # display image
self.mangaFrame.grid(row=1, column=0, sticky="NESW") self.panel = Label(self.master, image=self.img)
self.master.grid_rowconfigure(1, weight=1) self.panel.pack()
self.master.grid_columnconfigure(0,weight=1) self.panel.bind('<Configure>', self.redraw)
self.panel = Label(self.mangaFrame, image=self.img)
self.panel.grid(row=0, column=0, sticky="NESW")
self.panel.grid_rowconfigure(0, weight=1)
self.panel.grid_columnconfigure(0, weight=1)
def open_menu(self): # key bindings
# file dialog. reset page counts, call display Manga passing in starting values self.master.bind(self.nextKey, self.nextPage)
mangaFile = filedialog.askopenfilename() self.master.bind(self.backKey, self.backPage)
self.master.bind(self.quitKey, self.shutdown)
self.master.bind(self.openKey, self.open_menu)
#watch the 'x' button for window close
master.protocol('WM_DELETE_WINDOW', self.shutdown)
def open_menu(self, *event):
# file dialog. reset page counts, call display Maanga passing in starting values
if self.dir == 'None':
mangaFile = filedialog.askopenfilename(initialdir = "/home/dan/Desktop/")
else:
mangaFile = filedialog.askopenfilename(initialdir = self.dir)
if mangaFile != '':
self.file = mangaFile self.file = mangaFile
else :
self.panel.focus_force()
if self.file != '':
self.page = 0 self.page = 0
self.total = 0 self.total = 0
self.mangaTitle.configure(text=self.file) self.mangaTitle.configure(text=os.path.basename(self.file))
self.dir = os.path.dirname(self.file)
self.displayManga(self.file, 0) self.displayManga(self.file, 0)
def displayManga(self, file, pg): def displayManga(self, file, pg):
basewidth = 500 #basewidth = 500
hsize = self.master.winfo_height() - self.buttonFrame.winfo_height() - self.menu.winfo_height()
wsize = 500
basewidth = self.mangaFrame.winfo_width()
with zf(file, 'r') as zip: with zf(file, 'r') as zip:
# grab all the manga pages from the cbz file and sort them to put them in the correct order # grab all the manga pages from the cbz file and sort them to put them in the correct order
fl = zip.namelist() fl = zip.namelist()
@@ -96,24 +109,38 @@ class MangaReader:
im = Image.open(io.BytesIO(data)) im = Image.open(io.BytesIO(data))
# I found the below code to maintain image ratios at https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio # I found the below code to maintain image ratios at https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio
wpercent = (basewidth / float(im.size[0]))
hsize = int((float(im.size[1]) * float(wpercent)))
im = im.resize((basewidth, hsize), Image.ANTIALIAS) # wpercent = (basewidth / float(im.size[0]))
# hsize = int((float(im.size[1]) * float(wpercent)))
#Modified above code to maintain height and scale width
hpercent = (hsize / float(im.size[1]))
widthsize = int((float(im.size[0] * float(hpercent))))
if widthsize > self.master.winfo_width():
widthsize = self.master.winfo_width()
#widthsize = im.size[0]
wpercent = (widthsize / float(im.size[0]))
hsize = int((float(im.size[1]) * float(wpercent)))
# self.master.config(width=widthsize)
im = im.resize((widthsize, hsize), Image.ANTIALIAS)
#convert to tkImage and update the on screen image. #convert to tkImage and update the on screen image.
self.img = ImageTk.PhotoImage(im) self.img = ImageTk.PhotoImage(im)
self.panel.config(image=self.img) self.panel.config(image=self.img)
#finally update the current page count #finally update the current page count
self.pgcount.configure(text=str(pg+1) + " of " + str(self.total)) self.pgcount.configure(text=str(pg+1) + " of " + str(self.total))
self.panel.focus_force()
def nextPage(self, event): def nextPage(self, event):
self.page = self.page + 1 self.page = self.page + 1
if self.page < self.total: if self.page < self.total:
self.displayManga(self.file, self.page) self.displayManga(self.file, self.page)
else: else:
# reset self.page and doNothing #reset self.page and open file selection
self.page = self.page - 1 self.page = self.page - 1
# self.doNothing() self.open_menu()
def backPage(self, event): def backPage(self, event):
self.page = self.page - 1 self.page = self.page - 1
@@ -122,11 +149,18 @@ class MangaReader:
else: else:
#reset self.page and doNothing #reset self.page and doNothing
self.page = self.page + 1 self.page = self.page + 1
# self.doNothing()
def doNothing(self): def redraw(self, event):
print(self.page) if self.file != 'None':
print("FUCCK YOU") self.displayManga(self.file, self.page)
def shutdown(self,*event):
# Collect settings and pickle them. Then quit.
settingSave = {'directory': os.path.dirname(self.file), 'nextKey': self.nextKey, 'backKey': self.backKey,
'quitKey': self.quitKey, 'openKey':self.openKey}
pickle.dump(settingSave, open('manga.p', 'wb'))
quit()
#initalize tk #initalize tk
root=Tk() root=Tk()