如何在 python tkinter mainloop 中定义一个列表而不改变它的值
How to define a list in python tkinter mainloop without changing it's value
我试图制作一个 python 使用 PyPDF2 拆分 pdf 的程序。所以我必须定义一个列表来附加元素,但是当循环再次迭代时,列表的值变为空白列表。因此,该程序将制作一个没有任何内容的 pdf。这是我的代码
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()
有人请帮助。
我终于找到了答案。问题不在于列表,而是在我取spinbox小部件的初始值时引起的,这是修改后的代码。
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()
GUI 不是很好,但它仍然有效。
我试图制作一个 python 使用 PyPDF2 拆分 pdf 的程序。所以我必须定义一个列表来附加元素,但是当循环再次迭代时,列表的值变为空白列表。因此,该程序将制作一个没有任何内容的 pdf。这是我的代码
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()
有人请帮助。
我终于找到了答案。问题不在于列表,而是在我取spinbox小部件的初始值时引起的,这是修改后的代码。
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()
GUI 不是很好,但它仍然有效。