【发布时间】:2021-05-11 03:13:46
【问题描述】:
我正在使用 python 运行“任何文件类型到 PDF 转换器”代码。当我将 Word 文件转换为 PDF 时,它会在 Windows Shell 中显示一个进度条,如下所示:
但我希望这个进度条显示在 Tkinter 窗口中。 有没有办法做到这一点? 因为当我将它作为 exe 运行时,我不能让“-w”留在那里,否则程序会崩溃。
代码如下:
from tkinter import *
from PIL import Image
from tkinter import filedialog
def buttonclick():
root.filename=filedialog.askopenfilename(initialdir="Pictures", title="Select File", filetypes=(("All Files","*.*"),("PNG Files","*.png"),("JPG Files","*.jpg")))
try:
locus=root.filename.split(".")
dest=str(locus[0]+".pdf")
if str(locus[-1])=="docx":
from docx2pdf import convert
'''
import ttk as t
progressbar = t.Progressbar(orient=HORIZONTAL, length=200, mode='determinate')
progressbar.pack(side="bottom")
'''
convert(root.filename,dest)
#progressbar.start()
notifier=Label(root, text="File converted to PDF successfully!", fg="green", bg="#BFDFFF").pack()
elif str(locus[-1])=="pdf":
notifier=Label(root, text="PDF file Selected! Choose another file type.", fg="black", bg="#BFDFFF").pack()
elif str(locus[-1])=="":
notifier=Label(root, text="Please select a file!", fg="black", bg="#BFDFFF").pack()
else:
imge=Image.open(root.filename)
im1 = imge.convert('RGB')
im1.save(dest)
notifier=Label(root, text="File converted to PDF successfully!", fg="green", bg="#BFDFFF").pack()
except:
notifier=Label(root, text="An unexpected error occured!", fg="red", bg="#BFDFFF").pack()
root=Tk()
root.title("Any File to PDF Convertor")
root.config(bg="#BFDFFF")
root.geometry("300x200")
root.iconbitmap("D:\Coding\MyIcon.ico")
convert=Button(root, text="Select File",font=("Helvetica", 20), bg="#85FF97",command=lambda: buttonclick())
convert.pack(pady=20)
root.mainloop()
【问题讨论】:
-
基本上你不能将控制台进度条(由
tqdm模块在convert()函数中产生)移动到tkinter应用程序中。您可以尝试使用tqdm.tk模块重写convert()函数。 -
好的,谢谢,我会试试的! :)
标签: python-3.x tkinter progress-bar