簡體   English   中英

帶有tkinter和線程的Python堆棧溢出

[英]Python Stack Overflow with tkinter and threading

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

  1. Tkinter中的“之后”方法可能會影響系統並使堆棧溢出問題嗎? 我必須每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')        

我發現了問題。 它被遞歸地稱為“ startcommunicate”函數。 但是我應該找到一種更好的方式來編寫提到的線程(也許使用while循環)。 准備獲得任何幫助! 謝謝。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM