我试图做一个运行ttk进度栏直到创建文件的函数。似乎Widget.after引起了APPCRASH,但我不知道为什么。请帮忙!

def FilePgBar(title, file):
    if root:
        root.withdraw()
        boxRoot = Toplevel(master=root)
        boxRoot.withdraw()
    else:
        boxRoot = Tk()
        boxRoot.withdraw()

    boxRoot.protocol('WM_DELETE_WINDOW', denyWindowManagerClose )
    boxRoot.title(title)
    boxRoot.iconname('Dialog')
    boxRoot.geometry(rootWindowPosition)
    boxRoot.minsize(400, 100)
    pgBar = ttk.Progressbar(boxRoot, orient=HORIZONTAL, length=300, mode='indeterminate')
    pgBar.grid(row=1, column=0)
    pgBar.pack()
    pgBar.start()
    def checkfile():
        if os.path.exists(file):
            pgBar.stop()
            pgBar.destroy()
            boxRoot.deiconify()
            boxRoot.mainloop()
            boxRoot.destroy()
            if root: root.deiconify()
        else:
            boxRoot.after(100, checkfile)

    checkfile()

我想从其他脚本中调用此函数,所以我不确定使用类

编辑:我编辑了代码。我不再得到APPCRASH,但是运行程序时什么也没发生。

最佳答案

Python将参数传递给函数之前先对其求值。所以当它遇到

boxRoot.after(100, checkfile(file))

它评估checkfile(file)(调用checkfile),并在调用checkfile(file)之前用函数返回的值替换boxRoot.after

由于checkfile(file)没有return语句,因此默认情况下返回None。从而
boxRoot.after(100, None)

被叫。由于boxRoot.after的第二个参数应该是可调用的,因此会引发错误。

而是传递函数对象checkfile本身:
def checkfile():
    if os.path.exists(file):
        pgBar.stop()
        pgBar.destroy()
        boxRoot.destroy()
        if root: root.deiconify()
    else:
        # You need to update the progress bar
        boxRoot.after(100, checkfile)

这允许boxRoot.after函数从boxRoot.after内部调用该函数,而不是在调用after之前。

您可能会执行以下操作:
import os
import Tkinter as tk
import ttk

class App(object):
    def __init__(self, master, *args, **kwargs):
        self.master = master
        self.button = tk.Button(master, text='Stop', command=self.stop)
        self.button.pack()
        self.progress = ttk.Progressbar(master, orient="horizontal",
                                        length=200, mode="determinate")
        self.progress.pack()
        self.progress["value"] = 0
        self.progress["maximum"] = 100
        self.filename = '/tmp/out'
        if os.path.exists(self.filename):
            os.unlink(self.filename)
        self.checkfile()

    def checkfile(self):
        self.progress["value"] += 1
        if (not os.path.exists(self.filename)
            and self.progress["value"] < self.progress["maximum"]):
            self.master.after(100, self.checkfile)

    def stop(self):
        with open(self.filename, 'w') as f: pass

root = tk.Tk()
app = App(root)
root.mainloop()

关于windows - 尝试使用Progressbar运行tkinter时Python崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27593261/

10-13 06:05