I have a tkinter interface that needs to be automatically refreshed every 5 minutes. So far there is no problem, you would just have to do something like:
root.after(300000, function_to_run, args_of_fun_to_run)
The problem is that I have to do this for an "infinite" amount of time. The scenario is that the GUI will running on a PC attached to a TV displaying some info 24/7 in my office. This works for around 8 hours and then I get the following error:
Now, I know that the traceback goes all the way to a line in one of my custom modules that uses matplotlib. None of my custom modules uses any loops so I knw that the error does not directly come from one of them (please correct me if I'm wrong), so it must be from me repeating the function an infinite amount of time. This is the code from my main function:
from tkinter import *
from tkinter import ttk
import logging
import datetime
import sys
sys.path.append(r'C:UsersmeDesktopseprated sla screens')
import sla_main as sla
import main_frame as mf
import sla_grid as sg
import incoming_volume as iv
import outgoing_volume as ov
import read_forecast as fc
import get_headers as hd
import vol_graph as vg
import out_graph as og
import add_graph as ag
import sla_reduction_email as sre
###################################
###################################
###################################
###################################
runs = 0
def maininterface(f_size, pic_x, pic_y):
global runs
global root
start = str(datetime.datetime.now().date()) + ' ' + str(datetime.timedelta(hours=6))
screen = sla.slamain(start)
if runs == 0:
root = mf.mainframe(f_size)
sg.sla_grid(screen, f_size, root)
file = open('titles in queue.txt', 'r')
in_q = file.read()
file.close
ttk.Label(root, text=in_q, anchor=CENTER, width=15, font=('times', f_size, 'bold')).grid(column=6, row=2, sticky=E)
if runs > 0:
###################################
#deletes all rows before making the calculations
for label in root.grid_slaves():
if int(label.grid_info()["row"]) > 1:
label.grid_forget()
sg.sla_grid(screen, f_size, root)
file = open('titles in queue.txt', 'r')
in_q = file.read()
file.close
ttk.Label(root, text=in_q, anchor=CENTER, width=15, font=('times', f_size, 'bold')).grid(column=6, row=2, sticky=E)
###################################
#all this part is just info for the graph and the graph
incoming = iv.incomingvolume(start)
outgoing = ov.outgoingvolume(start)
forecast = fc.readforecast()
headers = hd.getheaders()
vg.volgraph(forecast, incoming, headers, pic_x, pic_y)
#og.outgraph(forecast, outgoing, headers, pic_x, pic_y)
ag.addgraph("vol_graph.png", root, 1)
#ag.addgraph("out_graph.png", root, 2)
runs = runs + 1
globals().update(locals())
print(str(datetime.datetime.now()))
root.after(300000, maininterface, f_size, pic_x, pic_y)
root.mainloop()
logging.basicConfig(level=logging.DEBUG, filename='error_log.txt')
try:
maininterface(28, 23.5, 6)
except:
logging.exception("Oops:")
What can I modify here to avoid this error??
Thanks!
EDIT:
As many have suggested, I have moved the mainloop call outside of the main function. The las few lines of my code look like this now:
try:
maininterface(28, 23.5, 6)
root.mainloop()
except:
logging.exception("Oops:")
The root.after call remains inside the function. After running it like this, it closes after 5 minutes. Does anyone know why the mainloop is not being called?
See Question&Answers more detail:
os