how to break looping in tkinter?

守給你的承諾、 提交于 2019-12-11 17:47:20

问题


this is my code.

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()

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?


回答1:


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()


来源:https://stackoverflow.com/questions/51638610/how-to-break-looping-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!