如何将函数中不断变化的字符串插入Tkinter中的标签中



我以前用一个简单的循环问过类似的问题,但是,我正在为一个更复杂的脚本而挣扎。我试图插入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()

我想成为一个标签的部分是进步。它显示了脚本的进度。我试了好几种方法,但总是出现错误,或者号码没有更新。请帮帮伙计们,我已经想了很久了,我对特金特有点不了解。

顺便说一句,整个脚本如下所示:

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

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

这里有一种更改标签文本的方法(使用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()

如果在循环中创建小部件,则StringVar特别好(IMO(。

Btw完全工作的代码:

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

最新更新