【问题标题】:how to display the progress bar of a function in an APP(GUI using tkinter)如何在APP中显示功能的进度条(使用tkinter的GUI)
【发布时间】:2021-09-14 18:53:09
【问题描述】:

我需要显示一个进度条来向用户指示正在发生的事情。我想在单击 CLASSIFY(如图所示)启动函数时显示它,然后该栏将在函数结束时消失。

这是函数:

pbar = ttk.Progressbar(gui1, orient="horizontal", length=300)
pbar.place(x=500, y=120)

def Classifyall():
    pbar.start(15)

    table = BeautifulTable()
    table.column_headers = ["File Name", "File Format"]
    directory_path = '/home/.../All files/*'
    file_list = glob.glob(directory_path)
    for file in file_list:
        filepath, file_extention = os.path.splitext(file)
        filename = filepath.rsplit('/', 1)[1]
        table.append_row([filename, file_extention])
    tx.insert(END, table)
    pbar.stop()

在这里我创建了一个包含功能的菜单栏

tool_menu = Menu(menu)
menu.add_cascade(label="Tool", menu=tool_menu)
tool_menu.add_command(label="Classify", command=Classifyall)

【问题讨论】:

  • 你是如何尝试实现进度条的?
  • 它试过这个:从进度条导入时间 import ProgressBar pbar = ProgressBar() def job(): Code ....
  • edit 问题并包括您的尝试

标签: python tkinter


【解决方案1】:

比较耗时的任务最好在线程中运行,否则会导致 tkinter 应用无响应。

我使用另一个函数start_classification() 在选择菜单项Classify 后执行。

在该函数内部:

  • 不确定模式下创建进度条
  • 启动进度条动画
  • 创建一个 tkinter 变量来保存来自Classifyall() 的结果
  • 启动子线程执行Classifyall()
  • 等待子线程完成
  • 销毁进度条
  • 使用Classifyall() 的结果更新文本框
import threading
from tkinter import ttk
...

# added argument var (a tkinter StringVar)
def Classifyall(var):
    table = BeautifulTable()
    table.column_headers = ["File Name", "File Format"]
    directory_path = '/home/.../All files/*'
    file_list = glob.glob(directory_path)
    for file in file_list:
        filepath, file_extention = os.path.splitext(file)
        filename = filepath.rsplit('/', 1)[1]
        table.append_row([filename, file_extention])

    # note that it is not safe to update tkinter widget in a child thread
    # so use the passed tkinter variable to save the result instead
    var.set(table)

def start_classification():
    # create a progress bar and start the animation
    pbar = ttk.Progressbar(gui1, orient='horizontal', length=300, mode='indeterminate')
    pbar.place(relx=0.5, rely=0.5, anchor='c')
    pbar.start()

    var = StringVar() # hold the result from Classifyall()
    # execute Classifyall() in a child thread
    threading.Thread(target=Classifyall, args=(var,)).start()
    # wait for the child thread to complete
    tx.wait_variable(var)

    pbar.destroy()
    tx.insert(END, var.get())

...

tool_menu = Menu(menu)
tool_menu.add_command(label='Classify', command=start_classification) # call start_classification() instead
menu.add_cascade(label='Tool', menu=tool_menu)

...

【讨论】:

    【解决方案2】:

    您可以使用tkinter.ttk.Progressbar:

    import tkinter
    import tkinter.ttk as ttk
    
    w = tkinter.Tk()
    pbar = ttk.Progressbar(w)
    

    它使用两种方式让它“进步”:

    1:自动进度

    使用start()stop() 方法,您可以让进度条以一定的速度自行前进。可以将参数传递给start(),指定每次更新之间的毫秒数。因此,调用pbar.start(50) 将每 50 毫秒将进度条提高 1%。调用 stop() 会停止进度条并将其进度重置为 0%。

    注意:进度条完成后,必须调用stop(),否则会重置为0%重新开始进度。

    2:手动进度

    进度条也可以通过调用step()方法手动进行。必须将参数传递给step(),指定进度百分比。因此,调用pbar.step(35) 会使进度条的进度增加 35%。是的,也允许使用负数:pbar.step(-35)

    【讨论】:

    • 我使用了这个方法(自动进度),因为它在我编辑的程序中显示,但不起作用
    • @DYAMB:您能否更具体地说明 什么 不起作用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 2021-12-28
    • 2014-09-06
    • 2021-11-28
    • 2017-11-14
    • 1970-01-01
    相关资源
    最近更新 更多