如何打破 tkinter 中的循环?

编程入门 行业动态 更新时间:2024-10-26 18:23:48
本文介绍了如何打破 tkinter 中的循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-30 05:43:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390098.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!