tkinter进度条要如何实现代码运行完成进度操作

```python

如下代码要如何实现类似的进度条操作:

from collections import OrderedDict

dict1 = {‘01’: 17, ‘02’: 22, ‘03’: 17, ‘04’: 7, ‘05’: 14, ‘06’: 15, ‘07’: 14, ‘08’: 19, ‘09’: 19, ‘10’: 18, ‘11’: 13,
‘12’: 17, ‘13’: 12, ‘14’: 18, ‘15’: 16, ‘16’: 11, ‘17’: 17, ‘18’: 16, ‘19’: 15, ‘20’: 13, ‘21’: 10, ‘22’: 18,
‘23’: 10, ‘24’: 19, ‘25’: 17, ‘26’: 23, ‘27’: 20, ‘28’: 13, ‘29’: 10, ‘30’: 20, ‘31’: 18, ‘32’: 15, ‘33’: 19,
‘34’: 3, ‘35’: 7, ‘36’: 19, ‘37’: 5, ‘38’: 21, ‘39’: 11, ‘40’: 22, ‘41’: 11, ‘42’: 2}

dict2 = {‘01’: 6, ‘02’: 3, ‘03’: 9, ‘04’: 12, ‘05’: 8, ‘06’: 4, ‘07’: 6, ‘08’: 8, ‘09’: 7, ‘10’: 5, ‘11’: 9, ‘12’: 8}

def creat_groud(dict1, dict2):
red = []
path = ‘./123.txt’
red_list = sorted(dict1.items(), reverse=True, key=lambda x: x[1])
yellow_list = sorted(dict2.keys(), reverse=True, key=lambda x: dict2[x])
for i in red_list[0:42 - 4]:
for j in red_list[0:42 - 3]:
if j[0] == i[0] or i[1] < j[1]:
continue
for k in red_list[0:42 - 2]:
if k[0] == j[0] or k[0] == i[0] or i[1] < k[1] or j[1] < k[1]:
continue
for l in red_list[0:42 - 1]:
if l[0] == k[0] or l[0] == j[0] or l[0] == i[0] or i[1] < l[1] or j[1] < l[1] or k[1] < l[1]:
continue
for n in red_list[0:42]:
if n[0] == l[0] or n[0] == k[0] or n[0] == j[0] or n[0] == i[0] or i[1] < n[1] or j[1] < n[1] or
k[1] < n[1] or l[1] < n[1]:
continue
result_int = [int(i[0]), int(j[0]), int(k[0]), int(l[0]), int(n[0])]
result_int.sort()
str_result = [‘%02d’ % a for a in result_int]
string = ‘-‘.join(str_result)
red.append(string)
my_list = list(OrderedDict.fromkeys(red))
with open(path, ‘w’) as pf:
for red in my_list:
for yellow in yellow_list:
str = ‘Aug=’ + red + ‘ :’ + yellow + “\n”
pf.write(str)

creat_groud(dict1, dict2)

tkinter进度条要如何实现代码运行完成进度操作
tkinter进度条要如何实现代码运行完成进度操作
tkinter进度条要如何实现代码运行完成进度操作

用上述代码实现进度Toplevel窗口:

Jason990420
最佳答案

Using multi-thread to do your job, do remember not to update your GUI in the sub-thread.

Example Code

from time import sleep
from threading import Thread
from tkinter import *
import tkinter.ttk as ttk

def move_to_center(win):
    w1, h1 = root.winfo_screenwidth(), root.winfo_screenheight()
    win.update()
    w2, h2 = win.winfo_width(), win.winfo_height()
    win.geometry(f"{w2}x{h2}+{(w1-w2)//2}+{(h1-h2)//2}")

def on_closing():
    global running
    running = False
    sleep(0.1)
    root.destroy()

def job():
    global running, count
    count, limit = 0, 100
    while running:
        sleep(0.05)
        count += 2
        if count > limit:
            break
    running = False

def run():
    global running
    button.config(state=DISABLED)
    running = True
    Thread(target=job).start()
    root.after(20, check)

def check():
    if running:
        counter.set(count)
        label['text'] = f'{count}%'
        root.after(20, check)
    else:
        button.config(state=NORMAL)

font = ("Courier New", 24, 'bold')
root = Tk()

button = Button(root, text="RUN", font=font, bg='blue', fg='white', command=run)
button.pack(side=RIGHT, fill=Y, expand=0)

counter = IntVar()
counter.set(0)
style = ttk.Style()
style.theme_use("default")
style.configure("TProgressbar", thickness=50)
bar = ttk.Progressbar(root, orient=HORIZONTAL, length=400, variable=counter, style="TProgressbar")
bar.pack(side=LEFT, padx=5, pady=5)

label = Label(root, text="0%", justify=CENTER, width=4, font=font)
label.pack(side=LEFT)

root.protocol("WM_DELETE_WINDOW", on_closing)

move_to_center(root)
root.mainloop()

file

6个月前 评论
LiTtianS (楼主) 6个月前
Jason990420 (作者) 6个月前
LiTtianS (楼主) 5个月前
Jason990420 (作者) 5个月前
讨论数量: 5
Jason990420

Using multi-thread to do your job, do remember not to update your GUI in the sub-thread.

Example Code

from time import sleep
from threading import Thread
from tkinter import *
import tkinter.ttk as ttk

def move_to_center(win):
    w1, h1 = root.winfo_screenwidth(), root.winfo_screenheight()
    win.update()
    w2, h2 = win.winfo_width(), win.winfo_height()
    win.geometry(f"{w2}x{h2}+{(w1-w2)//2}+{(h1-h2)//2}")

def on_closing():
    global running
    running = False
    sleep(0.1)
    root.destroy()

def job():
    global running, count
    count, limit = 0, 100
    while running:
        sleep(0.05)
        count += 2
        if count > limit:
            break
    running = False

def run():
    global running
    button.config(state=DISABLED)
    running = True
    Thread(target=job).start()
    root.after(20, check)

def check():
    if running:
        counter.set(count)
        label['text'] = f'{count}%'
        root.after(20, check)
    else:
        button.config(state=NORMAL)

font = ("Courier New", 24, 'bold')
root = Tk()

button = Button(root, text="RUN", font=font, bg='blue', fg='white', command=run)
button.pack(side=RIGHT, fill=Y, expand=0)

counter = IntVar()
counter.set(0)
style = ttk.Style()
style.theme_use("default")
style.configure("TProgressbar", thickness=50)
bar = ttk.Progressbar(root, orient=HORIZONTAL, length=400, variable=counter, style="TProgressbar")
bar.pack(side=LEFT, padx=5, pady=5)

label = Label(root, text="0%", justify=CENTER, width=4, font=font)
label.pack(side=LEFT)

root.protocol("WM_DELETE_WINDOW", on_closing)

move_to_center(root)
root.mainloop()

file

6个月前 评论
LiTtianS (楼主) 6个月前
Jason990420 (作者) 6个月前
LiTtianS (楼主) 5个月前
Jason990420 (作者) 5个月前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!