Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View a document TKinter

Tags:

python

tkinter

I am new to TKinter and cant seem to find any examples on how to view a document in a window. What I am trying to accomplish is that when selecting a PDF or TIF it will open the file and show the first page in a window using TKinter. Is this possible?

like image 995
thedemon Avatar asked Feb 15 '26 19:02

thedemon


1 Answers

A long time has passed since this question was posted but, for those still looking for a solution, here's one I've found :

https://github.com/rk700/PyMuPDF/wiki/Demo:-GUI-script-to-display-a-PDF-using-wxPython-or-Tkinter

Basically, it uses PyMuPDF, a python binding for MuPDF. MuPDF is a lightweight document viewer capable of displaying a few file formats, such as pdf and epub.

I quote the code used for TKinter:

This demo can easily be adopted to Tkinter. You need the imports

from Tkinter import Tk, Canvas, Frame, BOTH, NW
from PIL import Image, ImageTk  

and do the following to display each PDF page image:

#-----------------------------------------------------------------
# MuPDF code
#-----------------------------------------------------------------
pix = doc.getPagePixmap(pno - 1)      # create pixmap for a page

#-----------------------------------------------------------------
# Tkinter code
#-----------------------------------------------------------------
self.img = Image.frombytes("RGBA",
                       [pix.width, pix.height],
                       str(pix.samples))
self.photo = ImageTk.PhotoImage(self.img)
canvas = Canvas(self, width=self.img.size[0]+20, 
            height=self.img.size[1]+20)
canvas.create_image(10, 10, anchor=NW, image=self.photo)
canvas.pack(fill=BOTH, expand=1)
like image 56
DrD Avatar answered Feb 18 '26 07:02

DrD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!