2017-10-16 124 views
0

我有一個關於python堆棧溢出的問題。我正在用Python使用Tkinter和其他4個線程編寫應用程序。但問題是,工作一小時後,我得到堆棧溢出錯誤和應用程序崩潰。我的問題:Python堆棧溢出tkinter和線程

  1. Tkinter中的「after」方法是否會影響系統並導致堆棧溢出問題?我必須每0.5秒使用它來更新我的GUI。
  2. 變量(有很多變量)是否會影響堆棧內存?我需要刪除(刪除)他們嗎?
  3. 有什麼辦法可以特別知道哪個線程和函數使溢出?因爲我在其中一個線程中有一個遞歸函數。

謝謝!

class App(): 
def __init__(self, master): 
    self.master = master 
    notebook=ttk.Notebook(master,width=800,height=480) 
    frame1=ttk.Frame(notebook,style='TNotebook') 
    frame2=ttk.Frame(notebook,style='TNotebook') 
    frame3=ttk.Frame(notebook,style='TNotebook') 
    frame4=ttk.Frame(notebook,style='TNotebook') 
    notebook.add(frame1,text='p1') 
    notebook.add(frame2,text='p2') 
    notebook.add(frame3,text='p3') 
    notebook.add(frame4,text='p4') 
    notebook.pack() 

    self.varopt34=StringVar(master) 
    self.varopt34.set(datashowgui[27].strip()) 
    self.opt34=OptionMenu(frame3,self.varopt34,'0','1') 
    self.opt34.config(background="gray80",font="Harabara",width=1,height=1,highlightbackground="white",activebackground="gray80",fg="white") 
    self.opt34.place(x=370,y=220) 

    varopt1=StringVar(master) 
    varopt1.set(fglines[5]) 
    opt1=OptionMenu(frame4,varopt1,id1,id2,id3,id4,id5,command=self.forganize1) 
    opt1.place(x=150,y=18) 
    opt1.config(background="gray80",font="Harabara",width=11,height=2,highlightbackground="white",activebackground="gray80",fg="white") 

    tkButtonSave=tk.Button(frame3,image=clockimage16,text="Save Settings and Reboot",compound=CENTER,command=self.savesettings,bg="white",highlightbackground="white",activebackground="white",fg="white",font=("Harabara",15),width=250,height=20) 
    tkButtonSave.place(x=500,y=320) 

    tkButtonReset=tk.Button(frame4,image=clockimage16,text="Reset Sensors and Reboot",compound=CENTER,command=self.resetsensors,bg="white",highlightbackground="white",activebackground="white",fg="white",font=("Harabara",15),width=250,height=20) 
    tkButtonReset.place(x=500,y=320) 

    tkButtonQuit=tk.Button(master,image=clockimage16,text="Quit",compound=CENTER,command=self.quitwindow,bg="white",highlightbackground="white",activebackground="white",fg="white",font=("Harabara",20),width=100,height=20) 
    tkButtonQuit.place(x=600,y=420) 

    self.update_reading() 


def savesettings(self): 
    f=open("tempconfigreference.txt","r") 
    datasaveref=f.readlines() 
    f.close() 
    f=open("tempconfig.txt","w+") 
    f.writelines(datasaveref) 
    f.close()  


def quitwindow(self):  
    self.master.destroy() 

def resetsensors(self): 
    f=open("temps.txt","r") 
    lines=f.readlines() 
    f.close() 
    f=open("organizedtemps.txt","w+") 
    f.writelines(lines) 
    f.close() 


def savesensors(self): 
    #print('savesensorsisrunning') 
    organizetemp.dosave()  
    #&self.after(100 , savesensors)  


def update_reading(self): 
    now=datetime.now() 
    nowpersian=jalali.Gregorian(now.year,now.month,now.day).persian_tuple() 
    self.master.after(580 , self.update_reading) 
    self.master.after(850 , self.savesensors) 
def display(threadName): 

#print (threadName) 
root = Tk() 
app = App(root) 
root.mainloop() 

是的,當然,這是控制GUI的線程之一的示例代碼和摘要。 這裏是一個有遞歸函數的線程(它被修剪成易於閱讀):

def startcommunicate(thread_name): 
def cpu_temp(): 
    htot1=open("finaltemps.txt","r") 
    linesorganized=htot1.readlines() 
    htot1.close() 
    if linesorganized: 
     cpu_temp=linesorganized[0] 
    else: 
     cpu_temp=0 
    return cpu_temp 
try: 
    run (host='169.254.176.82', port=80, quiet=True) 
except: 
    time.sleep(5) 
    startcommunicate('Thread-1')   
+0

您是如何使用?如果您只是更新標籤或輸入字段,那麼after方法應該沒有問題。我們需要看到一些可測試的代碼示例,以證明一個很好的答案。請閱讀並提供[最小,完整和可驗證示例](https://stackoverflow.com/help/mcve) –

+0

您能否顯示實際的錯誤信息? –

+0

當然是,「致命的python錯誤無法從計算器恢復」。我在嵌入式設備上工作,我想知道爲什麼我沒有得到遞歸錯誤,而我沒有設置它的上限!? – user3271199

回答

0

我發現這個問題。 這是「startcommunicate」遞歸調用函數。 但我應該找到一個更好的方式來編寫提到的線程(也許使用while循環)。準備好得到任何幫助! 謝謝。