简体   繁体   English

在过程中更改标签(Python、tkinter)

[英]Change Label during process (Python, tkinter)

This is a simple PDF encrypter.这是一个简单的 PDF 加密器。 Before and after some processes I want to change a label, but it changes only, if everything is done.在某些过程之前和之后,我想更改标签,但只有在一切都完成后才会更改。 For example I want to change the label text to the string in processing, when the file/folder was choosed. For example I want to change the label text to the string in processing, when the file/folder was choosed. But it didn't want to, only at the very end, when all the jobs are done.但它不想,只在最后,当所有工作都完成时。

    '''
encrypt pdf files
'''

import PyPDF2 as p
import os
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askopenfilename, askdirectory
from tkinter.messagebox import showerror

class MainWindow(Frame):

    def __init__(self):
        Frame.__init__(self)
        self.choice = 'Choose a file or a folder'
        self.processing = 'Encrypting file(s)'

        self.master.title('Viktor PDF titkositoja :)')
        self.master.rowconfigure(5, weight=1)
        self.master.columnconfigure(5, weight=1)
        self.grid(sticky=W+E+N+S)

        self.button = Button(self, text='File', command=self.enc_file, width=30)
        self.button.grid(row=1, column=0, sticky=W+E, padx=15, pady=6)

        self.button = Button(self, text='Folder', command=self.enc_folder, width=30)
        self.button.grid(row=1, column=1, sticky=W+E, padx=15, pady=6)

        self.labelText = StringVar()
        self.label = Label(self, textvariable=self.labelText)
        self.labelText.set(self.choice)
        self.label.grid(row=2, column=0, columnspan=2, sticky=W+E, padx=3, pady=10)

        self.row = 2
        self.copy_to = os.path.join('/home', 'zaturek', 'Documents')


    def reset_labels(self):
        list = self.grid_slaves()
        for l in list[:-3]:
            l.destroy()
        self.labelText.set(self.choice)


    def enc_file(self):
        self.reset_labels()

        fname = askopenfilename(filetypes=(('PDF files', '.pdf'),
                                           ('All files', '*.*') ))
        if fname:
            try:
                self.labelText.set(self.processing + '\n' + fname)
                self.enc(fname)
            except Exception as e:
                showerror(title='Error', message=e)


    def enc_folder(self):
        self.reset_labels()

        dname = askdirectory()

        if dname:
            # self.labelText.set('Fajlok titkositasa a mappaban: ' + '\n' + dname)

            for f in os.listdir():
                if os.path.isfile(f) and '.pdf' in f:
                    try:
                        self.labelText.set(self.processing + '\n' + f)
                        self.enc(f)
                    except Exception as e:
                        showerror(title='error', message=e)
                        print(f)


    def enc(self, f):
        #self.label.config(text='Fajl(ok) titkositasa folyamatban' + '\n' + f)

        read_pdf = p.PdfFileReader(f)
        write_pdf = p.PdfFileWriter()

        if read_pdf.isEncrypted == False:

            try:
                for i in range(0, read_pdf.getNumPages()):
                    write_pdf.addPage(read_pdf.getPage(i))

                write_pdf.encrypt('1234')

                with open(self.new_fname(f), 'wb') as out:
                    write_pdf.write(out)
                #print('itt')
                self.nlabel2 = Label(self, text=f + ' - kesz.')
                self.nlabel2.grid(row=self.row+1, column=0, columnspan=2, sticky=W+E)
                self.row += 1
            except Exception as e:
                m = 'Valami nem stimmelt. Ellenorizd es probald ujra. Ha akkor sem megy, hivj fel!'
                showerror(title='error', message=f + '\n' + e)
                print(e)
        else:
            self.nlabel3 = Label(self, text=f + ' - mar kodolva volt.')
            self.nlabel3.grid(row=self.row+1, column=0, columnspan=2, sticky=W+E)
            self.row += 1


    def new_fname(self, f):
        t = os.path.split(f)[1].split('.')
        #print(t[0])
        t[0] = t[0] + '_e'
        nf = str.join('.', (t[0], t[1]))
        #print(os.path.join(self.copy_to, nf))
        return os.path.join(self.copy_to, nf)


if __name__ == '__main__':
    MainWindow().mainloop()

I would like to add other labels too, but those aren't appears either in time, only at the end.我也想添加其他标签,但这些标签没有及时出现,只出现在最后。 Any idea?任何的想法?

You should update the label, eg:您应该更新标签,例如:

self.labelText.set(self.processing + '\n' + fname)
self.label.update()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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