python-3.x - 如何将函数中的更改字符串插入到 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()
我想成为一个标签的部分是进步。它显示了脚本的进度。我已经尝试了几件事,但我不断收到错误或号码没有更新。请帮助大家,我一直在努力解决这个问题,我对 Tkinter 有点菜鸟。
顺便说一句,整个脚本如下所示:
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()
推荐阅读
- angular - 获取组件中动态模板的内容
- android - Android Recyclerview 无法从数据库中获取数据
- scala - 自由单子应该基于的函子在哪里
- html - 当显示变大时,引导导航栏折叠按钮不会消失
- python - Python 在函数参数中输入枚举类型
- dart - 使用级联表示法时无法在 StringBuffer 上调用 `toString()`
- javascript - 使用 slick 创建自定义帖子滑块
- java - 正则表达式删除“一些文本(一些文本)”之间的数据?
- amadeus - 生产中未找到数据域错误
- python - 如何告诉 Python 等到来自 os.system() 的 Windows 命令完成?