回路包装盒

2024-06-16 10:28:28 发布

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

我正在运行一个缓慢的过程,使用由我的脚本组合在一起的LaTeX来构建一组pdf。在

PDF构建在for循环中。我想显示一个状态窗口,它将为循环经过的每个学生添加一行,以便您可以看到进度。我一直在用print来做这件事,但是我想要一个能与我移动到的Tkinter接口很好地集成的东西。在

我有这个:

ReStatuswin = Toplevel(takefocus=True)
ReStatuswin.geometry('800x300')
ReStatuswin.title("Creating Reassessments...")
Rebox2 = MultiListbox(ReStatuswin, (("Student", 15), ("Standard", 25), ("Problems", 25) ))
Rebox2.pack(side = TOP)

OKR = Button(ReStatuswin, text='OK', command=lambda:ReStatuswin.destroy())
OKR.pack(side = BOTTOM)

然后循环:

^{pr2}$

然后,在循环内部,在PDF制作完成后

    Rebox2.insert(END, listy)

它很好地插入了行,但只有在整个循环完成之后,它们才会显示出来(以及ReBox2窗口本身)。在

你知道是什么原因导致延迟显示吗?在

谢谢!在


Tags: 脚本forpdftkinter过程状态学生side
1条回答
网友
1楼 · 发布于 2024-06-16 10:28:28

是的,据我所知,有两个问题。首先,不需要用每个新条目更新显示。第二,不是用按钮触发for循环,而是在启动时运行它(这意味着在循环退出之前不会创建显示)。然而不幸的是,我不能真正地使用你给出的代码,因为它是一个更大的东西的片段。但是,我做了一个小脚本,应该演示如何做你想做的事情:

from Tkinter import Button, END, Listbox, Tk
from time import sleep

root = Tk()

# My version of Tkinter doesn't have a MultiListbox
# So, I use its closest alternative, a regular Listbox
listbox = Listbox(root)
listbox.pack()

def start():
    """This is where your loop would go"""

    for i in xrange(100):
        # The sleeping here represents a time consuming process
        # such as making a PDF
        sleep(2)

        listbox.insert(END, i)

        # You must update the listbox after each entry
        listbox.update()

# You must create a button to call a function that will start the loop
# Otherwise, the display won't appear until after the loop exits
Button(root, text="Start", command=start).pack()

root.mainloop()

相关问题 更多 >