单击窗口时如何使 tkinter 的窗口不显示“无响应”

时间:2021-07-14 08:51:13

标签: python tkinter

当我的代码在 tkinter 窗口中单击运行按钮后运行时,当我单击它时它会显示“无响应”。 单击并显示加载进度时如何使其不响应? 这是我使用的加载代码。

from tkinter import *
from tkinter.ttk import *
import time
window = Tk()
window.geometry("400x200")
percent = StringVar()
text = StringVar()
def start():
    def task():
        bar['value']=0
        GB = 100
        download = 0
        speed = 1
        while(download<GB):
            time.sleep(0.05)
            bar['value']+=(speed/GB)*100
            download+=speed
            percent.set(str(int((download/GB)*100))+"%")
            text.set(str(download)+"/"+str(GB)+" GB completed")
            window.after(2000, None)
            window.update_idletasks()
        window.after(1, task)
    window.after(100, task)

bar = Progressbar(window,orient=HORIZONTAL,length=300)
bar.pack(pady=10)
percentLabel = Label(window,textvariable=percent).pack()
taskLabel = Label(window,textvariable=text).pack()
button = Button(window,text="download",command=start).pack()
window.mainloop()    

2 个答案:

答案 0 :(得分:0)

试试这个:

import tkinter as tk
from tkinter import ttk

window = tk.Tk()
window.geometry("400x200")
percent = tk.StringVar(master=window)
text = tk.StringVar(master=window)

def start():
    download = 0
    def task(download):
        bar["value"] = download
        GB = 100
        speed = 1
        if download <= GB:
            bar["value"] += (speed/GB)*100
            percent.set(str(int((download/GB)*100))+"%")
            text.set(str(download)+"/"+str(GB)+" GB completed")
        window.after(1000, task, download+1)
    window.after(1000, task, 0)

bar = ttk.Progressbar(window,orient="horizontal", length=300)
bar.pack(pady=10)
percentLabel = tk.Label(window, textvariable=percent)
percentLabel.pack()
taskLabel = tk.Label(window, textvariable=text)
taskLabel.pack()
button = tk.Button(window, text="download", command=start)
button.pack()
window.mainloop()  

答案 1 :(得分:0)

我已经从 task 中取出函数 start 并实现了一个 Button disable 以防止多次按下按钮。

还制作了2.x和3.x版本的更新百分比和下载输出代码

try:
    import tkinter as tk
    from tkinter import ttk
except ImportError:
    # Can't test it but according to this: https://stackoverflow.com/a/23984633/11106801
    import Tkinter as tk
    import ttk

window = tk.Tk()
window.geometry("400x200")
percent = tk.StringVar(master=window)
text = tk.StringVar(master=window)

def task(download):
    bar['value'] = download
    GB = 100
    speed = 1
    if GB >= download:
        bar['value'] += speed/GB*100
        percent.set("{} %".format(int(download/GB*100)))
        text.set("{} / {}  GB completed".format(download, GB))
    window.after(100, task, download+1)

def start():
    button.config(state="disabled")
    task(0)

bar = ttk.Progressbar(window, orient="horizontal", length=300)
bar.pack(pady=10)

percentLabel = tk.Label(window, textvariable=percent)
percentLabel.pack()

taskLabel = tk.Label(window, textvariable=text)
taskLabel.pack()

button = tk.Button(window, text="download", command=start)
button.pack()

window.mainloop()