简体   繁体   中英

How to define a list in python tkinter mainloop without changing it's value

I was trying to make a python program for splitting pdfs using PyPDF2 . So I had to define a list to append the elements but when the loop iterates again the value of the list changes to a blank list. Therefore the program will make a pdf with nothing in it. Here is my code

import tkinter as tk
from PIL import Image, ImageTk
from PyPDF2 import PdfFileReader, PdfFileWriter
from tkinter import filedialog
from tkinter import messagebox
  
elements = []
root7 = tk.Tk()
root7.geometry("600x600")
root7.title("PDF Viewer")
root7.configure(bg="#202020")

    
image7 = Image.open("split.jpg")
new_img7 = image7.resize((600,400))
photo7 = ImageTk.PhotoImage(new_img7)


def open1():
    pdf = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("pdf files","*.pdf"),("all files","*.*")))
    elements.append(pdf)

    w = tk.Spinbox(root7, from_=0, to=100, width=3)
    w.place(x=150, y=510)
    elements.append(int(w.get()))

    w1 = tk.Spinbox(root7, from_=0, to=100, width=3)
    w1.place(x=409, y=510)
    elements.append(int(w1.get()))
    
def split():
    pdfs = PdfFileReader(elements[0], "rb")
    pdf_writer = PdfFileWriter()

    for page in range(elements[1], elements[2]):
        pdf_writer.addPage(pdfs.getPage(page))

    output_fname = "splitted.pdf"

    with open(output_fname, 'wb') as out:
        pdf_writer.write(out)

    messagebox.showinfo("Splitted","PDF Saved in Desktop as splitted.pdf")
    

lbl7 = tk.Label(root7,image=photo7)
lbl7.place(x=0, y=0)

button7 = tk.Button(root7, text= 'Open PDF', command=open1, bg="#B8B8B8")
button7.place(width=300, height=50, x=150, y=450)

button8 = tk.Button(root7, text= 'Split PDF', command=split, bg="#B8B8B8")
button8.place(width=300, height=50, x=150, y=540)


root7.update()
root7.mainloop()

Someone please help.

I had finally found the answer. The problem was not with the list, but it was caused when I took the initial value of the spinbox widget, here is the modified code.

import tkinter as tk
from PIL import Image, ImageTk
from PyPDF2 import PdfFileReader, PdfFileWriter
from tkinter import filedialog
from tkinter import messagebox
  
w = 0
w1 = 0
root7 = tk.Toplevel()
root7.geometry("600x600")
root7.title("PDF Splitter")
root7.configure(bg="#202020")
elements = []

    
image7 = Image.open("split.jpg")
new_img7 = image7.resize((600,400))
photo7 = ImageTk.PhotoImage(new_img7)


def open1():
    global w, w1
    pdf = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("pdf files","*.pdf"),("all files","*.*")))
    elements.append(pdf)

    w = tk.Spinbox(root7, from_=0, to=100, width=3)
    w.place(x=150, y=510)
    

    w1 = tk.Spinbox(root7, from_=0, to=100, width=3)
    w1.place(x=409, y=510)
    elements.append(int(w1.get()))
    
def split():
    global w, w1
    pdfs = PdfFileReader(elements[0], "rb")
    pdf_writer = PdfFileWriter()

    elements.append(int(w.get()))
    elements.append(int(w1.get()))

    w2 = int(w.get())
    w3 = int(w1.get())

    for page in range(45):
        pdf_writer.addPage(pdfs.getPage(w2-1))
        w2+=1
        if (w2-1)==(w3):
            break

    output_fname = "splitted.pdf"

    with open(output_fname, 'wb') as out:
        pdf_writer.write(out)

    messagebox.showinfo("Splitted","PDF Saved in Desktop as splitted.pdf")
    

lbl7 = tk.Label(root7,image=photo7)
lbl7.place(x=0, y=0)

button7 = tk.Button(root7, text= 'Open PDF', command=open1, bg="#B8B8B8")
button7.place(width=300, height=50, x=150, y=450)

button8 = tk.Button(root7, text= 'Split PDF', command=split, bg="#B8B8B8")
button8.place(width=300, height=50, x=150, y=540)


root7.update()
root7.mainloop()

The GUI is not good, but it still works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM