from tkinter import * from tkinter import filedialog from zipfile import ZipFile as zf from PIL import Image, ImageTk import io class MangaReader: def __init__(self, master): self.master = master self.page = 0 self.total = 0 self.file = 'None' self.img = '' master.title("Manga Reader") # tk window size and allow resize master.geometry("700x900") master.resizable(width=True, height=True) # Menu Bar self.menu = Menu(master) item = Menu(self.menu) item.add_command(label="Open", command=self.open_menu) item.add_separator() item.add_command(label="Exit", command=master.quit) self.menu.add_cascade(label="File", menu=item) root.config(menu=self.menu) # Frames self.buttonFrame = Frame(master) self.mangaFrame = Frame(master, bg="green") self.titleFrame = Frame(master) # forward/back buttons self.nextPg = Button(self.buttonFrame, text='>') self.nextPg.bind('', self.nextPage) self.nextPg.grid(row=0, column=2) self.buttonFrame.grid_rowconfigure(0, weight=1) 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.bind('', self.backPage) self.backPg.grid(row=0, column=0) self.buttonFrame.grid_rowconfigure(0, weight=1) self.buttonFrame.grid_columnconfigure(0,weight=1) # page counter self.pgcount = Label(self.buttonFrame, text= str(self.page) + " of " + str(self.total)) self.pgcount.grid(row=1, columns=1, columnspan=2) self.buttonFrame.grid_rowconfigure(1, weight=1) self.buttonFrame.grid_columnconfigure(1,weight=1) # title frame 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 self.mangaFrame.grid(row=1, column=0, sticky="NESW") self.master.grid_rowconfigure(1, weight=1) self.master.grid_columnconfigure(0,weight=1) 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): # file dialog. reset page counts, call display Manga passing in starting values mangaFile = filedialog.askopenfilename() self.file = mangaFile self.page = 0 self.total = 0 self.mangaTitle.configure(text=self.file) self.displayManga(self.file, 0) def displayManga(self, file, pg): basewidth = 500 basewidth = self.mangaFrame.winfo_width() 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 fl = zip.namelist() fl.sort() self.total = len(fl)-1 # grab the current page out of the array. treat it as byte data since it's still zipped. data = zip.read(fl[pg]) 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 wpercent = (basewidth / float(im.size[0])) hsize = int((float(im.size[1]) * float(wpercent))) im = im.resize((basewidth, hsize), Image.ANTIALIAS) # convert to tkImage and update the on screen image. self.img = ImageTk.PhotoImage(im) self.panel.config(image=self.img) # finally update the current page count self.pgcount.configure(text=str(pg+1) + " of " + str(self.total)) def nextPage(self, event): self.page = self.page + 1 if self.page < self.total: self.displayManga(self.file, self.page) else: # reset self.page and doNothing self.page = self.page - 1 # self.doNothing() def backPage(self, event): self.page = self.page - 1 if self.page >= 0: self.displayManga(self.file, self.page) else: # reset self.page and doNothing self.page = self.page + 1 # self.doNothing() def doNothing(self): print(self.page) print("FUCCK YOU") # initalize tk root=Tk() app = MangaReader(root) root.mainloop()