Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在过程中更改标签(Python、tkinter)_Python_Pdf_Tkinter_Label - Fatal编程技术网

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

在过程中更改标签(Python、tkinter),python,pdf,tkinter,label,Python,Pdf,Tkinter,Label,这是一个简单的PDF加密程序。在某些过程之前和之后,我想更改标签,但只有在一切都完成后,标签才会更改。例如,我想在选择文件/文件夹时将标签文本更改为正在处理的字符串。但它不想,只是在最后,当所有的工作都完成了 ''' encrypt pdf files ''' import PyPDF2 as p import os from tkinter import * from tkinter import ttk from tkinter.filedialog import askopenf

这是一个简单的PDF加密程序。在某些过程之前和之后,我想更改标签,但只有在一切都完成后,标签才会更改。例如,我想在选择文件/文件夹时将标签文本更改为正在处理的字符串。但它不想,只是在最后,当所有的工作都完成了

    '''
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()
我也想添加其他标签,但这些标签也没有及时出现,只出现在最后。
有什么想法吗?

您应该更新标签,例如:

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

您有多个标签,要更改哪一个?请提供特定的标签更新。我提到了标签,因为我只有一个标签名为label…:D但是是的,我可以更具体一些。我想用标签更换标签。谢谢!已解决。:)