我在工作的时候怎么用

2024-10-16 20:53:24 发布

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

函数运行tkinter冻结。 我想用Tkinter窗口运行它过程。而运行进度条 我想用特金特窗户。但是我不能 因为它冻结了tkinter。我如何使用根窗口时间。睡眠(10) 或其他功能正在工作

import tkinter.ttk as ttk
import tkinter as tk
import time

progress = 0


def loading(window=None):
    mpb = ttk.Progressbar(window, orient="horizontal", length=200, mode="determinate")
    mpb.place(y=0, x=0)
    mpb["maximum"] = 100
    mpb["value"] = progress
    print(progress)


def incrase():
    global progress
    print(progress)
    progress += 1
    time.sleep(10)  # for example, a function works here and tkinter freezes
    loading()       # i don't want tkinter freezes


root = tk.Tk()
loading(root)
ttk.Button(root, text='increase', command=incrase).place(x=0, y=25, width=90)

root.mainloop()

谢谢你的回答


Tags: importtimetkinterdefasplacerootwindow
1条回答
网友
1楼 · 发布于 2024-10-16 20:53:24

应该使用^{},通过它可以安排loading()函数在一段时间后调用。你知道吗

程序

以下是如何在程序中使用它:

import tkinter.ttk as ttk
import tkinter as tk
import time

progress = 0


def loading(window):
    mpb = ttk.Progressbar(window, orient="horizontal", length=200, mode="determinate")
    mpb.place(y=0, x=0)
    mpb["maximum"] = 100
    mpb["value"] = progress
    print(progress)


def incrase():
    global root
    global progress
    print(progress)
    progress += 1
    root.after(10, loading(root))  # schedule loading() 



root = tk.Tk()
loading(root)
ttk.Button(root, text='increase', command=incrase).place(x=0, y=25, width=90)

root.mainloop()

演示

以上运行程序截图:

enter image description here

相关问题 更多 >