2017-10-16 101 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循环)。准备好得到任何帮助! 谢谢。