首页 > 解决方案 > 在 Max Value Tkinter 处停止进度条

问题描述

我用 Tkinter 创建了一个进度条:

self.progressbar = ttk.Progressbar(self, orient="horizontal", length=300, maximum=100, mode="determinate")
self.progressbar.place(x=250, y=225)

当我按下按钮时我会启动它:

self.progressbar.start(10)

但我希望它在进度条的末尾自行停止,然后像这样做其他事情(在我的情况下打开一个文本文件):

def LaunchButton()
    self.progressbar.start(10)
    if end of progress bar
        self.progressbar.stop()
        open text file 

感谢您的帮助!

编辑@Saad:

我的启动按钮功能:

    def launchCallBack(self):

        self.dataName = self.EditText.get("1.0", 'end-1c')
        self.dataPath = self.PathText.get(ACTIVE)
        self.dataDirectory = self.Directory.get(ACTIVE)

        print(self.dataDirectory)

        if self.dataPath == '' :
            messagebox.showinfo("error", "Please select a XML (.aird) File before launching !")
        elif '.' in self.dataName :
            messagebox.showinfo("error", "You don't have to put extension (.ctm), just put the name of your file !")
        elif self.dataName == '' :
            messagebox.showinfo("error", "Please name your Text (.ctm) File before launching")
        elif self.dataDirectory == '' :
            messagebox.showinfo("error", "Please select a directory to put your Text (.ctm) File in !")
        else :
            self.textFileCreation()

函数 textFileCreation 是您的更新函数:

    def textFileCreation(self):

        self.process += 10
        self.progressbar['value'] = self.process
        if self.progressbar['value'] >= self.progressbar['maximum']:
            msg_succes = str("The file " + self.dataName + ".ctm has been created in the folder : " + self.dataDirectory)
            messagebox.showinfo("success", msg_succes)
            create_file(self.dataPath, self.dataName, self.dataDirectory)
            return
        else :
            self.after(100, self.textFileCreation())

标签: pythontkinterprogress-bar

解决方案


这是一个例子:

from tkinter import ttk
from tkinter import *

root = Tk()

P = ttk.Progressbar(root, orient="horizontal", length=300, maximum=100, mode="determinate")
P.pack()

L = Label(root, text="In Process")
L.pack()

process = 0
def update():
    global process
    process += 10
    P['value'] = process
    if P['value'] >= P['maximum']:

        L['text'] = "The Process is done!"
        # Here you can do anything when the  progress bar finishes.

        return  # This will end the after() loop
    root.after( 100, update )

update()

root.mainloop()

我也改变你的功能LaunchButton()以相应地工作。但是请self.process = 0__init__您的班级中声明

功能

def LaunchButton(self):
    self.process += 10
    self.progressbar['value'] = self.process
    if self.process >= self.progressbar['maximum']:

        # Here you can do anything when the progress bar finishes.
            # open text file

        return      # This will end the after() loop

    self.after(100, self.LaunchButton)


推荐阅读