如何添加tkinter进度栏不确定功能

发布于 2025-02-03 20:14:06 字数 960 浏览 3 评论 0原文

from tkinter import *
from tkinter import ttk

def DOTEST_GUI():
    GUI = Tk()
    w = 1920
    h = 1080
    ws = GUI.winfo_screenwidth()
    hs = GUI.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    GUI.geometry(f'{w}x{h}+{x:.0f}+{y:.0f}')

    def start_p():
        progress.start(5)

    def stop_P():
        progress.stop()

    def print_cf(event = None):
        import time
        print('s')
        start_p()
        time.sleep(5)
        stop_P()

    B_TEST = ttk.Button(GUI, text = "test", width = 15, command = print_cf)
    B_TEST.pack()

    progress = ttk.Progressbar(GUI, orient = HORIZONTAL, length = 100, mode = 'indeterminate')
    progress.pack(pady = 10)

    GUI.bind("<Return>", print_cf)

    GUI.focus()
    GUI.mainloop()

DOTEST_GUI()

遵循此代码进度栏无法正常运行。

我尝试删除stop_p(),它在time.sleep(5)的5秒后工作。

我希望它开始运行5秒钟,直到stop_p()代码。

from tkinter import *
from tkinter import ttk

def DOTEST_GUI():
    GUI = Tk()
    w = 1920
    h = 1080
    ws = GUI.winfo_screenwidth()
    hs = GUI.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    GUI.geometry(f'{w}x{h}+{x:.0f}+{y:.0f}')

    def start_p():
        progress.start(5)

    def stop_P():
        progress.stop()

    def print_cf(event = None):
        import time
        print('s')
        start_p()
        time.sleep(5)
        stop_P()

    B_TEST = ttk.Button(GUI, text = "test", width = 15, command = print_cf)
    B_TEST.pack()

    progress = ttk.Progressbar(GUI, orient = HORIZONTAL, length = 100, mode = 'indeterminate')
    progress.pack(pady = 10)

    GUI.bind("<Return>", print_cf)

    GUI.focus()
    GUI.mainloop()

DOTEST_GUI()

follow this code progress bar is not running properly.

I tried to remove stop_P(), it's work after 5 second of time.sleep(5).

I would like it to start running progress 5 second until the stop_P() code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2025-02-10 20:14:06

如果要在一定的时间内运行progressbar,然后停止它,则可以执行这样的操作。合并print_cf函数以启动并停止进度,并设置5个范围,在两者之间睡1秒,然后停止progressbar。将其放在线程上可以让您做一些比睡一秒钟并打印一些东西要耗时的事情。

from tkinter import *
from tkinter import ttk
import time
import threading



                    
def DOTEST_GUI():
    GUI = Tk()
    w = 1920
    h = 1080
    ws = GUI.winfo_screenwidth()
    hs = GUI.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    GUI.geometry(f'{w}x{h}+{x:.0f}+{y:.0f}')


    def run_thread():
        execute_thread = threading.Thread(target=print_cf)
        execute_thread.start()

        
    def print_cf(event = None):
        progress.start()
        print('s')
        for i in range(5):
            print(i)
            time.sleep(1)
            if i ==4:
                progress.stop()


    B_TEST = ttk.Button(GUI, text = "test", width = 15, command = run_thread)
    B_TEST.pack()

    progress = ttk.Progressbar(GUI, orient = HORIZONTAL, length = 100, mode = 'indeterminate')
    progress.pack(pady = 10)

    GUI.bind("<Return>", print_cf)

    GUI.focus()
    GUI.mainloop()
        

DOTEST_GUI()

If you want to run a Progressbar for a set amount of time and then stop it, you could do something like this. Consolidate the print_cf function to start and stop the progress and set a range of 5, sleep for 1 second in between and then stop the Progressbar. Placing it on a thread would allow you to do something more time consuming than sleeping one second and printing something.

from tkinter import *
from tkinter import ttk
import time
import threading



                    
def DOTEST_GUI():
    GUI = Tk()
    w = 1920
    h = 1080
    ws = GUI.winfo_screenwidth()
    hs = GUI.winfo_screenheight()
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    GUI.geometry(f'{w}x{h}+{x:.0f}+{y:.0f}')


    def run_thread():
        execute_thread = threading.Thread(target=print_cf)
        execute_thread.start()

        
    def print_cf(event = None):
        progress.start()
        print('s')
        for i in range(5):
            print(i)
            time.sleep(1)
            if i ==4:
                progress.stop()


    B_TEST = ttk.Button(GUI, text = "test", width = 15, command = run_thread)
    B_TEST.pack()

    progress = ttk.Progressbar(GUI, orient = HORIZONTAL, length = 100, mode = 'indeterminate')
    progress.pack(pady = 10)

    GUI.bind("<Return>", print_cf)

    GUI.focus()
    GUI.mainloop()
        

DOTEST_GUI()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文