python - 在 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())
解决方案
这是一个例子:
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)
推荐阅读
- c# - 停止执行方法
- c++ - 亮度键未触发 LowLevelKeyboardProc 挂钩
- django - 带有 libapache2-mod-wsgi-py3 的 Docker 映像“httpd”
- database-design - 如何设计架构以读取同时更新的巨大数据库表
- sql - 如何为上夜班的员工安排退房时间
- php - 如何限制回声字段中的单词
- java - 将时间表详细信息保存在 sql 表中的多行中
- asp.net-mvc - 你如何在asp.net mvc中添加一个部分
- android - 如何使用 SQLiteOpenHelper 类从 sqlite 数据库中删除项目
- asp.net-core - 如何使用 Postman 调用 Identity Server 4 进行登录