I have made a pdf viewer using tkinter. I am wondering if I can let the pdf file 'pop out' using Toplevel()
function. I tried to use lambda
to try to incorporate the Toplevel()
when using the button but the pop out window did not reflect anything. Here is the code I made:
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkPDFViewer import tkPDFViewer as pdf
import os
#make tk case
root = Tk()
root.geometry("1000x700 200 100")
root.title("PDF Viewer")
root.configure(bg="light blue")
#view frame
view_frame=Frame(root, bg="light blue", bd=5, width=400)
view_frame.pack(side=LEFT)
#to generate pop up window to view PDFs
def popup(filename):
win=Toplevel()
win.geometry("100x80")
win.title(filename)
v2=None
file = ''
#search for files
def viewpdf():
#make v2 global
global v2
filename=filedialog.askopenfilename(initialdir=os.getcwd(),
title="Select PDF File",
filetype=(("PDF File", ".pdf"),
("PDF File", ".PDF"),
("All File",".txt")))
if filename:
global file
file=filename
#destroy old file if it exists
if v2:
v2.destroy()
#create new pdf images
v1=pdf.ShowPdf()
#clear stored images
v1.img_object_li.clear()
#store new images
v2=v1.pdf_view(view_frame, pdf_location=open(filename,"r"),height=50, width=80)
v2.pack(pady=(0,0))
#set buttons
view_button=Button(view_frame, text='SEARCH FOR FILES', command=lambda: [viewpdf(), popup(file)], width=50, bd=5)
view_button.pack()
root.mainloop()
CodePudding user response:
Update: I have tried adding tkinter.TopLevel()
inside the def viewpdf()
and it worked! I have deleted the def popup()
as well.
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkPDFViewer import tkPDFViewer as pdf
import os
#make tk case
root = Tk()
root.geometry("1000x700 200 100")
root.title("PDF Viewer")
root.configure(bg="light blue")
#view frame
view_frame=Frame(root, bg="light blue", bd=5, width=400)
view_frame.pack(side=LEFT)
v2 = None
#search for files
def viewpdf():
#make v2 global
global v2
filename=filedialog.askopenfilename(initialdir=os.getcwd(),
title="Select PDF File",
filetype=(("PDF File", ".pdf"),
("PDF File", ".PDF"),
("All File",".txt")))
if filename:
#destroy old file if it exists
if v2:
v2.destroy()
#create new pdf images
v1=pdf.ShowPdf()
#clear stored images
v1.img_object_li.clear()
#set a new pop out window
newWindow=tkinter.Toplevel(view_frame)
#store new images
v2=v1.pdf_view(newWindow, pdf_location=filename, height=50, width=80)
v2.pack(pady=(0,0))