2016-04-04 11 views
0

これは私の最初のポストstackoverflowですが、私はコード道路でバンプをヒットしたとして、私は今いくつかの時間のための答えを使用しています。私が答えを見つけることができない暗い日が来たので、もしあなたが助けることができるなら私に知らせてください。Python - TKinter "タイムアウト"

私はTkinterを使ってデスクトップ上に置かれたウィジェットを作成し、特定のフォルダにいくつのファイルがあるか教えてくれています。それは約10分間かなりうまくいくと思われ、その後動作を停止します。間違っていることが分かり、それをクリックして閉じるか、それを閉じると、動作が停止したというメッセージが表示されるまで、エラーメッセージがクラッシュまたはポーリングされることはありません。すべてのヘルプは本当にapprciatedされるだろう

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() 

は、ここに私のコードです!

乾杯!

答えて

2

毎秒14個の新しいウィジェットを作成しているようですが、古いものは一切破壊しません。この10分後に、あなたは8,400個の要素を追跡しなければならないウィンドウを持っています。

私は2つのソリューションを参照してください。あなたがより多くを作成する前に、以前のすべてのウィジェット

  • .destroy()を。
  • プログラムの最初に14個のウィジェットを作成してから、.configと入力して、update_clockの呼び出しごとにテキストを変更します。
+0

新しいウィジェットはバックグラウンドなどで作成されていますか?プログラムを実行すると新しいウィンドウが表示されません –

+0

新しいウィジェットは古いウィジェットと同じウィンドウに表示されます。新しいウィジェットは古いものと同じ位置に配置され、その上に表示されるため、古いウィジェットは表示されなくなります。しかし、彼らはまだそこにいる。 – Kevin

+0

ああ、それは意味のトンを作る!すぐに返事をしてくれてありがとう!私はおそらく、あなたの答えから、私が必要としている他の情報を調べるのに十分であると思う。ありがとう! –