问题描述
限时送ChatGPT账号..这是我的代码.
from PyPDF2 import PdfFileReader
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
root = tk.Tk()
label_list = []
def get_info(path):
with open(path, 'rb') as f:
pdf = PdfFileReader(f)
info = pdf.getDocumentInfo()
page = pdf.getPage(4)
label_list[0].config(text = "Title")
label_list[1].config(text = info.title)
label_list[2].config(text = "Author")
label_list[3].config(text = info.author)
label_list[4].config(text = "Subject")
label_list[5].config(text = info.subject)
label_list[6].config(text = "Abstract")
label_list[7].config(text = page.extractText())
save = tk.Button(root, text="Save")
save.pack()
def browsefunc():
filename = filedialog.askopenfilename()
pathlabel.config(text=filename)
get_info(filename)
browsebutton = tk.Button(root, text="Choose a File", command=browsefunc)
browsebutton.pack()
pathlabel = tk.Label(root)
pathlabel.pack()
for i in range(8):
label_list.append(tk.Label(root, text=""))
label_list[i].pack()
root.mainloop()
我添加了按钮保存,当我选择文件时,按钮会显示,当我选择一个新文件时,我有 2 个保存按钮等等.
i added button save which the button will show when i choose the file, and when i choose a new file, i got 2 save buttons and more.
如何破坏数据,当我选择一个新文件时,我只有一个按钮?
how can i break the data, and when i choose a new file i only have 1 button?
推荐答案
这里有一种方法可以让按钮在需要时显示,然后在不需要时去掉.
Here is one way you can get the button to show when you need it and then get rid of it when you don't.
我们可以有一个跟踪变量来查看我们当前是否有一个打开的按钮,当我们使用该按钮时,我们可以将按钮作为函数的一部分删除.因此,在我们需要时创建,并在完成时销毁.
We can have a tracking variable to see if we currently have a button open and when we use the button we can get rid of the button as part of the function. So create when we need it and destroy when its done.
from PyPDF2 import PdfFileReader
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
root = tk.Tk()
label_list = []
track_save_button = False
def save_func():
global track_save_button, save
# your code for saving data here
save.destroy()
track_save_button = False
def get_info(path):
global track_save_button, save
if track_save_button == False:
save = tk.Button(root, text="Save", command=save_func)
save.pack()
track_save_button = True
with open(path, 'rb') as f:
pdf = PdfFileReader(f)
info = pdf.getDocumentInfo()
page = pdf.getPage(4)
label_list[0].config(text = "Title")
label_list[1].config(text = info.title)
label_list[2].config(text = "Author")
label_list[3].config(text = info.author)
label_list[4].config(text = "Subject")
label_list[5].config(text = info.subject)
label_list[6].config(text = "Abstract")
label_list[7].config(text = page.extractText())
else:
print("Save Button is already active!")
def browsefunc():
filename = filedialog.askopenfilename()
pathlabel.config(text=filename)
get_info(filename)
browsebutton = tk.Button(root, text="Choose a File", command=browsefunc)
browsebutton.pack()
pathlabel = tk.Label(root)
pathlabel.pack()
for i in range(8):
label_list.append(tk.Label(root, text=""))
label_list[i].pack()
root.mainloop()
这篇关于如何打破 tkinter 中的循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论