在progressbar tkinter python运行一个周期后打开窗口

2024-10-01 07:34:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我想打开评论文件后,一个周期的progressbar,不能理解在互联网上给出的解决方案,因为我的代码很简单,所以简单的答案是必需的,因为我在Tkinter非常初学者

from tkinter import *
import tkinter.ttk as ttk
from tkinter import Label
import time

root = Tk()
root.geometry('350x215')
root.title("WELCOME")
root.configure(bg='skyblue')
label1=Label(root,text="Spam Checker",font= 
('Constantia',30),bg='skyblue').place(x=50,y=50)

label2=Label(root,text="(ML Approach)",font= 
(None,16),bg='skyblue').place(x=100,y=100)

pb = ttk.Progressbar(root,length=350, orient='horizontal', 
mode='determinate')
pb.place(y=190) 
pb.maximum=100

pb.start()

""""def openwindow():

     with open('main1.py') as source_file:
        `enter code here`exec(source_file.read())
"""
root.mainloop()

Tags: textfromimportsourcetkinterasplaceroot
1条回答
网友
1楼 · 发布于 2024-10-01 07:34:16

首先,必须将变量与ProgressBar关联:

progress_var = IntVar()
pb = ttk.Progressbar(root, length=350, variable=progress_var)

然后可以使用trace方法在每次更新进度条时运行函数,如here所述。你知道吗

当进度条达到100时,其值将重置为0。因此,当函数检测到值为0时,它会停止进度条并调用openwindow函数:

def on_progress_bar_updated(*args):
    # when the progress bar reaches 100, it wraps around to 0
    if progress_var.get() == 0:
        pb.stop()
        openwindow()

progress_var.trace('w', on_progress_bar_updated)

pb.start()

相关问题 更多 >