Markas Povilaika
Markas Povilaika

Reputation: 67

How to insert a changing string from a function into a label in Tkinter?

I've asked a similar question before with a simple loop, however, I am struggling with a more complex script. Area of the function which I am trying to insert into a label in the GUI:

def all():
    ##INPUTS FROM THE USER 
    #User Inputs
    vin_list_input1 = vin.get()
    vin_list_input = [item for item in vin_list_input1.split()]
    adobe_input = path.get()
    #Conversion
    listvin = vin_list_input
    listnum = []

    ##MAIN FUNCTION THAT FINDS NEEDED PAGES
    #Pdf Object
    object = PyPDF2.PdfFileReader(adobe_input)
    # Get number of pages
    NumPages = object.getNumPages()
    def find_pg():
        for vin in listvin:
            # Extract text and do the search
            for i in range(0, NumPages):
                PageObj = object.getPage(i)
                Text = PageObj.extractText()
                if re.search(vin,Text):
                    listnum.append(i)
                    listnum.append(i+1)
            progress = (len(listnum)/2)/len(listvin)*100 #<----THIS PART INTO LABEL
            print(round(progress,2),end="\r")
    find_pg()

The part that I want to be a label is progress. It shows the progress of the script. I've tried several things but I keep on getting an error or the number does not update. Please help guys, I've been trying to figure this out for ages, I am a bit of a noob with Tkinter.

Btw the whole script is shown below:

def all():
    ##INPUTS FROM THE USER 
    #User Inputs
    vin_list_input1 = vin.get()
    vin_list_input = [item for item in vin_list_input1.split()]
    adobe_input = path.get()
    #Conversion
    listvin = vin_list_input
    listnum = []

    ##MAIN FUNCTION THAT FINDS NEEDED PAGES
    #Pdf Object
    object = PyPDF2.PdfFileReader(adobe_input)
    # Get number of pages
    NumPages = object.getNumPages()
    def find_pg():
        for vin in listvin:
            # Extract text and do the search
            for i in range(0, NumPages):
                PageObj = object.getPage(i)
                Text = PageObj.extractText()
                if re.search(vin,Text):
                    listnum.append(i)
                    listnum.append(i+1)
            progress = (len(listnum)/2)/len(listvin)*100
            print(round(progress,2),end="\r")
    find_pg()

    ##CONCATING ALL OF THE FILES
    adobe_input = adobe_input[:-4]

    pdf_file_path = adobe_input + '.pdf'
    file_base_name = pdf_file_path.replace('.pdf', '')

    pdf = PdfFileReader(pdf_file_path)

    pages = listnum 
    pdfWriter = PdfFileWriter()

    for page_num in pages:
        pdfWriter.addPage(pdf.getPage(page_num))

    with open('{0}_subset.pdf'.format(file_base_name), 'wb') as f:
        pdfWriter.write(f)
        f.close()

    ##SAVING THE NEW FILE AND SENDING IT TO THE PRINTER
    os.startfile(adobe_input + "_subset" + ".pdf" , 'print')

##INTERFACE TKINTER
window = Tk()
window.title("Ctrl + F Automator ™")
window.geometry("592x150")

l_emp1 = Label(window, text = "",justify = LEFT).grid(sticky = W, row = 0, column =0)
l_emp2 = Label(window, text = "",justify = LEFT).grid(sticky = W, row = 3, column =0)

l_vin_list = Label(window, text = "Enter VIN's: ", justify = LEFT).grid(sticky = W, row = 1, column =1)
l_path = Label(window, text = "Enter PDF path: ",justify = LEFT).grid(sticky = W, row = 2, column =1)

vin = StringVar()
path = StringVar()
e1 = tk.Entry(window, text = vin, width = 80)
e2 = tk.Entry(window, text = path, width = 80)
e1.grid(row=1, column=2)
e2.grid(row=2, column=2)

b_enter = Button(window, text ="Conirm & Launch Script", command = all, width = 80, height = 1).place(x=10, y=80)

window.mainloop()

Upvotes: 0

Views: 564

Answers (3)

Markas Povilaika
Markas Povilaika

Reputation: 67

Btw the code that worked completely:

def all():
    ##INPUTS FROM THE USER 
    #User Inputs
    vin_list_input1 = vin.get()
    vin_list_input = [item for item in vin_list_input1.split()]
    adobe_input = path.get()
    #Conversion
    listvin = vin_list_input
    listnum = []

    ##MAIN FUNCTION THAT FINDS NEEDED PAGES
    #Pdf Object
    object = PyPDF2.PdfFileReader(adobe_input)
    # Get number of pages
    NumPages = object.getNumPages()
    def find_pg():
        for vin in listvin:
            # Extract text and do the search
            for i in range(0, NumPages):
                PageObj = object.getPage(i)
                Text = PageObj.extractText()
                if re.search(vin,Text):
                    listnum.append(i)
                    listnum.append(i+1)
            progress = (len(listnum)/2)/len(listvin)*100
            progress = round(progress,2)
            progress = str(progress) + '%'
            #print(round(progress,2),end = "\r")
            l_progress.config(text = progress)
            window.update_idletasks() 
    find_pg()

    ##CONCATING ALL OF THE FILES
    adobe_input = adobe_input[:-4]

    pdf_file_path = adobe_input + '.pdf'
    file_base_name = pdf_file_path.replace('.pdf', '')

    pdf = PdfFileReader(pdf_file_path)

    pages = listnum # page 1, 3, 5
    pdfWriter = PdfFileWriter()

    for page_num in pages:
        pdfWriter.addPage(pdf.getPage(page_num))

    with open('{0}_subset.pdf'.format(file_base_name), 'wb') as f:
        pdfWriter.write(f)
        f.close()

    ##SAVING THE NEW FILE AND SENDING IT TO THE PRINTER
    os.startfile(adobe_input + "_subset" + ".pdf" , 'print')

##INTERFACE TKINTER
window = Tk()
window.title("Ctrl + F Automator ™")
window.geometry("592x155")
window.wm_iconbitmap('desktop/Gu/logo.ico')

l_emp1 = Label(window, text = "",justify = LEFT).grid(sticky = W, row = 0, column =0)
l_emp2 = Label(window, text = "",justify = LEFT).grid(sticky = W, row = 3, column =0)

l_vin_list = Label(window, text = "Enter VIN's: ", justify = LEFT).grid(sticky = W, row = 1, column =1)
l_path = Label(window, text = "Enter PDF path: ",justify = LEFT).grid(sticky = W, row = 2, column =1)

l_load = Label(window, text = "Search Complete:",justify = LEFT).place(x = 10,y =120)

l_progress = Label(text = '0.0' + '%')
l_progress.place(x=106,y=120)

vin = StringVar()
path = StringVar()
e1 = tk.Entry(window, text = vin, width = 80)
e2 = tk.Entry(window, text = path, width = 80)
e1.grid(row=1, column=2)
e2.grid(row=2, column=2)

b_enter = Button(window, text ="Conirm & Launch Script", command = all, width = 80, height = 1).place(x=10, y=80)

window.mainloop()

Upvotes: 0

Matiiss
Matiiss

Reputation: 6156

Here is a way to change label text (using StringVar):

from tkinter import Tk, Label,Button, StringVar


def clicked():
    text_var.set('changed')
    
    
root = Tk()

text_var = StringVar()
text_var.set('Click button to change text')
lbl = Label(textvariable=text_var)
btn = Button(text='click', command=clicked)

lbl.pack()
btn.pack()

root.mainloop()

StringVars are especially great (IMO) if creating widgets in a loop.

Upvotes: 1

Tkinter Lover
Tkinter Lover

Reputation: 855

Change a label's text with config(text=progress):

from tkinter import *

def clicked():
    l.config(text='changed!')
    
    
root = Tk()

l = Label(text='Click button to change text')
b = Button(text='click', command=clicked)

l.pack()
b.pack()

mainloop()

Upvotes: 1

Related Questions