Python - TKinter "times out"

时间:2016-04-04 16:30:42

标签: python tkinter timeout

This is my first post on stackoverflow but I have been using the answers for some time now as I've hit bumps in the code road. The dark day has come where I can't seem to find the answer so please let me know if you can help.

The program is pretty simple I'm using Tkinter to create a widget that just sits on my desktop and tells me how many files are in specific folders. It seems to work pretty well for about 10 minutes and then stops working. It doesn't crash or ping up an error message it just freezes until I realise that somethings wrong and then when I go to click on it or close it down it says it has stopped working.

Here's my code:

import Tkinter as tk
import time
import os
from Tkinter import Tk, Label, BOTH
from ttk import Frame, Style


class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        self.clock = tk.Label(self, text="")
        self.clock.pack()


        # start the clock "ticking"
        self.update_clock()


    def update_clock(self):

        ptc = len(os.listdir("Folder Dir"))
        if ptc != 0:
            label1 = Label(self, text="Pre TC", fg="red")
        else:
            label1 = Label(self, text="Pre TC")
        label1.place(x=0, y=0)
        if ptc != 0:
            label2 = Label(self, text=ptc, fg="red")
        else:
            label2 = Label(self, text=ptc)
        label2.place(x=90, y=0)


        pp = len(os.listdir("Folder Dir"))
        if pp != 0:
            label3 = Label(self, text="PP", fg="red")
        else:
            label3 = Label(self, text="PP")
        label3.place(x=0, y=65)
        if pp != 0:
            label4 = Label(self, text=pp, fg="red")
        else:
            label4 = Label(self, text=pp)
        label4.place(x=90, y=65)



        stc = len(os.listdir("Folder Dir"))
        if stc != 0:
            label5 = Label(self, text="Super TC", fg="red")
        else:
            label5 = Label(self, text="Super TC")
        label5.place(x=0, y=150)
        if stc!= 0:     
            label6 = Label(self, text=stc, fg="red")
        else:
            label6 = Label(self, text=stc)
        label6.place(x=90, y=150)



        wff = len(os.listdir("Folder Dir"))
        if wff != 0:
            label7 = Label(self, text="WIN FF", fg="red")
        else:
            label7 = Label(self, text="WIN FF")
        label7.place(x=0, y=230)

        if wff != 0:
            label8 = Label(self, text=wff, fg="red")
        else:
            label8 = Label(self, text=wff)
        label8.place(x=90, y=230)



        wa = len(os.listdir("Folder Dir"))
        if wa != 0:
            label9 = Label(self, text="Wave Agent", fg="red")
        else:
            label9 = Label(self, text="Wave Agent")
        label9.place(x=0, y=315)
        if wa != 0:
            label10 = Label(self, text=wa, fg="red")
        else:
            label10 = Label(self, text=wa)
        label10.place(x=90, y=315)



        bwf = len(os.listdir("Folder Dir"))
        if bwf != 0:
            label11 = Label(self, text="BWF", fg="red")
        else:
            label11 = Label(self, text="BWF")
        label11.place(x=0, y=395)
        if bwf != 0:
            label12 = Label(self, text=bwf, fg="red")
        else:
            label12 = Label(self, text=bwf)
        label12.place(x=90, y=395)



        swi = len(os.listdir("Folder Dir"))
        if swi != 0:
            label13 = Label(self, text="Switch", fg="red")
        else:
            label13 = Label(self, text="Switch")
        label13.place(x=0, y=480)
        if swi != 0:
            label14 = Label(self, text=swi, fg="red")
        else:
            label14 = Label(self, text=swi)
        label14.place(x=90, y=480)

        # call this function again in one second
        self.after(1000, self.update_clock)

def main():
    app = SampleApp()
    app.title('Counter')
    app.geometry("50x510+1000+8")
    app.mainloop()

if __name__== "__main__":
    main()

All help would be really apprciated!

Cheers!

1 个答案:

答案 0 :(得分:2)

Looks like you're creating 14 new widgets every second, but you never destroy any of the old ones. After ten minutes of this, you've got a window that has to keep track of 8,400 elements, which I expect would bog it down considerably.

I see two solutions:

  • .destroy() all previous widgets before you create more.
  • create 14 widgets exactly once at the beginning of the program, then .config them to change their text during every update_clock call.