How to Add PDF in Tkinter GUI Python?
Python is one of the most popular programming languages used by developers to create efficient and effective applications. Tkinter is a standard GUI framework used in Python. Initially developed for the Tcl language, this framework is now available for Python and offers a wide range of features and tools to build desktop applications with ease.
One of the essential requirements of a GUI application is to handle various file formats. PDF is one such format that is widely used for documents, manuals, and reports. In this article, we will discuss how to add PDF files in Tkinter GUI Python.
Prerequisites
Before we begin, make sure that you have the following Python packages installed on your system:
- Tkinter
- PyPDF2
To install Tkinter, run the following command:
pip install tkinter
To install PyPDF2, run the following command:
pip install PyPDF2
Displaying PDF in Tkinter GUI
Adding a PDF in the Tkinter GUI requires a particular library called PyPDF2. We can use the getPage() method of this library to extract the page from the PDF. Here is a sample code that reads a PDF and displays it in the Tkinter window:
# Importing Required Libraries
from tkinter import *
from PyPDF2 import PdfFileReader
# Creating Tkinter Window
root = Tk()
# Reading PDF File
pdf_file = open("sample.pdf", "rb")
pdf_reader = PdfFileReader(pdf_file)
pdf_page = pdf_reader.getPage(0)
# Converting PDF to Image
pdf_page_image = pdf_page.convert("RGB")
pdf_page_image.thumbnail((600, 700))
# Creating Tkinter Canvas
canvas = Canvas(root, width=600, height=700)
canvas.pack()
# Creating Tkinter Image Object
img = PhotoImage(image=pdf_page_image)
# Adding Image to the Canvas
canvas.create_image(300, 350, image=img)
# Displaying the Window
root.mainloop()
In this code, we import the required packages, including Tkinter and PyPDF2. We read the sample PDF file, extract the first page, and convert it to an image using the convert() method. The thumbnail() method resizes the image to fit the Tkinter window.
Next, we create a Tkinter canvas with the desired dimensions. Then we create an image object and add it to the canvas using the create_image() method. Finally, we display the GUI window using the mainloop() method.
Adding Navigation Buttons
In the previous section, we displayed the entire PDF page on the Tkinter window. However, in most cases, users want to navigate through the PDF pages using next and previous buttons. Here is a sample code that adds navigation buttons to the Tkinter GUI:
# Importing Required Libraries
from tkinter import *
from PyPDF2 import PdfFileReader
# Creating Tkinter Window
root = Tk()
# Reading PDF File
pdf_file = open("sample.pdf", "rb")
pdf_reader = PdfFileReader(pdf_file)
# Creating Tkinter Canvas
canvas = Canvas(root, width=600, height=700)
canvas.pack()
# Creating Tkinter Image Object
img = None
# Button Callback Functions
def prev_page():
global pdf_reader, pdf_page, img
if pdf_reader and pdf_page.getContents():
pdf_page = pdf_reader.getPage(pdf_page.getPageNumber() - 1)
pdf_page_image = pdf_page.convert("RGB")
pdf_page_image.thumbnail((600, 700))
img = PhotoImage(image=pdf_page_image)
canvas.itemconfigure(image_id, image=img)
def next_page():
global pdf_reader, pdf_page, img
if pdf_reader and pdf_page.getPageNumber() < pdf_reader.getNumPages() - 1:
pdf_page = pdf_reader.getPage(pdf_page.getPageNumber() + 1)
pdf_page_image = pdf_page.convert("RGB")
pdf_page_image.thumbnail((600, 700))
img = PhotoImage(image=pdf_page_image)
canvas.itemconfigure(image_id, image=img)
# Creating Navigation Buttons
prev_button = Button(root, text="<<", command=prev_page)
prev_button.pack(side=LEFT)
next_button = Button(root, text=">>", command=next_page)
next_button.pack(side=RIGHT)
# Displaying the Window
root.mainloop()
In this code, we create two button functions prev_page()
and next_page()
to navigate between the PDF pages. It checks if the current page is the first or last page and displays the corresponding page accordingly.
We create two buttons using Tkinter’s Button() method and pack them on the left and right sides of the canvas. We also update the img
variable on each button click, which changes the image displayed on the canvas.
Also,we set up the canvas and img
variable as global variables to access them from within the button functions.
Final Code
Here is the final code to display PDF in Tkinter GUI with navigation buttons:
# Importing Required Libraries
from tkinter import *
from PyPDF2 import PdfFileReader
# Creating Tkinter Window
root = Tk()
# Reading PDF File
pdf_file = open("sample.pdf", "rb")
pdf_reader = PdfFileReader(pdf_file)
pdf_page = pdf_reader.getPage(0)
# Converting PDF to Image
pdf_page_image = pdf_page.convert("RGB")
pdf_page_image.thumbnail((600, 700))
# Creating Tkinter Canvas
canvas = Canvas(root, width=600, height=700)
canvas.pack()
# Creating Tkinter Image Object
img = PhotoImage(image=pdf_page_image)
image_id = canvas.create_image(300, 350, image=img)
# Button Callback Functions
def prev_page():
global pdf_reader, pdf_page, img
if pdf_reader and pdf_page.getPageNumber() > 0:
pdf_page = pdf_reader.getPage(pdf_page.getPageNumber() - 1)
pdf_page_image = pdf_page.convert("RGB")
pdf_page_image.thumbnail((600, 700))
img = PhotoImage(image=pdf_page_image)
canvas.itemconfigure(image_id, image=img)
def next_page():
global pdf_reader, pdf_page, img
if pdf_reader and pdf_page.getPageNumber() < pdf_reader.getNumPages() - 1:
pdf_page = pdf_reader.getPage(pdf_page.getPageNumber() + 1)
pdf_page_image = pdf_page.convert("RGB")
pdf_page_image.thumbnail((600, 700))
img = PhotoImage(image=pdf_page_image)
canvas.itemconfigure(image_id, image=img)
# Creating Navigation Buttons
prev_button = Button(root, text="<<", command=prev_page)
prev_button.pack(side=LEFT)
next_button = Button(root, text=">>", command=next_page)
next_button.pack(side=RIGHT)
# Displaying the Window
root.mainloop()
This code includes the previous and next button functions, and both buttons are packed on opposite sides of the canvas. The img
variable is created initially with the first page of the PDF, and the image_id
variable stores the ID returned by the create_image
method.
Conclusion
In conclusion, adding PDF files to Tkinter GUI Python is straightforward with the help of the PyPDF2 library. We can read the PDF, convert it to an image, and display it on the Tkinter canvas. Also, we can add navigation buttons with functions to allow users to navigate between the pages of the PDF. Using these techniques, you can create efficient and effective GUI applications that display PDF files with ease.