【问题标题】:How to Display Progress bar from Windows shell in TKinter?如何在 TKinter 中从 Windows shell 显示进度条?
【发布时间】: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


【解决方案1】:

为什么不试试进度条小部件。查看此代码。由于您已接受答案,因此我没有费心调整我的解决方案以适应您的情况:

from tkinter import *
from tkinter.ttk import *
import os



root = Tk()

# I set the length and maximum as shown to demonstrate the process in the 
# proceeding function. Pay attention to the increment r
progress = Progressbar(root, orient = HORIZONTAL,
            length = 200/5, maximum=200/5, mode = 'determinate')

# Function 


def my_func():
    t=0
    r= 1/5   
    for i in range(200):
        print(i) #whatever function interests you
        t=t+r
        progress['value'] = t
        root.update_idletasks()
  

progress.pack()

# Button
Button(root, text = 'Start', command = bar).pack(pady = 10)


mainloop()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-28
    • 2011-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多