|
发表于 2020-6-11 12:41:22
|
显示全部楼层
本楼为最佳答案
本帖最后由 java2python 于 2020-6-15 00:43 编辑
写代码,其实每一步都会错,可以一步一步来:
比如你先加一个文本框,放在按钮的右边,先做到这一步,然后执行一下看看,有没有问题
然后,再把这个假的进度表示代码,填到你的程序中,这是假的,和你想运行的东西,没关联,看看能不能表示进度
最后才是把你要完成的动作,和进度关联起来。
进度表示GUI例子代码:
- import time
- import threading
- from tkinter import *
-
- def update_progress_bar():
- for percent in range(1, 101):
- inputload1.set(str(percent)+"/100")
- time.sleep(0.1)
-
- def run():
- th = threading.Thread(target=update_progress_bar)
- th.setDaemon(True)
- th.start()
-
- top = Tk()
- top.title('Progress Bar')
- top.geometry('800x500+290+100')
- top.resizable(False, False)
- top.config(bg='#535353')
-
- # 进度条
- inputload1 = StringVar()
- entry_process = Entry(top, show=None,width=6,textvariable = inputload1)
- entry_process.place(x=510,y=100)
- # 按钮
- button_start = Button(top, text='开始', fg='#F5F5F5', bg='#7A7A7A', command=run, height=1, width=15, relief=GROOVE, bd=2, activebackground='#F5F5F5', activeforeground='#535353')
- button_start.place(relx=0.45, rely=0.5, anchor=CENTER)
-
- top.mainloop()
复制代码
假如你的代码是这样:
- from tkinter import *
-
- limit = 1000000000
- mili_limit = limit//1000
- def heavy():
- for i in range(limit):
- #do_something()
- b = (i*100-300)//1000
- #if(i%mili_limit == 0):
- # process = str(i)+"/"+str(limit)
-
- top = Tk()
- top.title('Heavy task')
- top.geometry('500x300+290+100')
- top.resizable(False, False)
- top.config(bg='#ffffff')
-
- # 按钮
- button_start = Button(top, text='开始', fg='#F5F5F5', bg='#7A7A7A', command=heavy, height=1, width=15, relief=GROOVE, bd=2, activebackground='#F5F5F5', activeforeground='#535353')
- button_start.place(relx=0.45, rely=0.5, anchor=CENTER)
-
- top.mainloop()
复制代码
那么结合后,能表示执行进度的代码就是这样(你最好有文件比较器,看看两个文件哪里做了修改):
- import threading
- from tkinter import *
-
- limit = 1000000000
- mili_limit = limit//1000
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- def heavy():
- for i in range(limit):
- #do_something()
- b = (i*100-300)//1000
- if(i%mili_limit == 0):
- inputload1.set(str(i)+"/"+str(limit))
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- def run():
- th = threading.Thread(target=heavy)
- th.setDaemon(True)
- th.start()
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- top = Tk()
- top.title('Heavy task')
- top.geometry('500x300+290+100')
- top.resizable(False, False)
- top.config(bg='#ffffff')
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- # 进度文本
- inputload1 = StringVar()
- entry_process = Entry(top, show=None,width=25,textvariable = inputload1)
- entry_process.place(x=300,y=140)
- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
- # 按钮
- button_start = Button(top, text='开始', fg='#F5F5F5', bg='#7A7A7A', command=run, height=1, width=15, relief=GROOVE, bd=2, activebackground='#F5F5F5', activeforeground='#535353')
- button_start.place(relx=0.45, rely=0.5, anchor=CENTER)
-
- top.mainloop()
复制代码
|
|