Search code examples
pythonloopssavepickleatexit

Why isn't atexit registering in python?


I have a loop in Tkinter:

def main():
    #Global Variables
    windows = []
    buttons = []
    labels = []
    messageboxes = []
    global theme
    theme = 0
    listboxes = []
    global register
    register = []
    global path
    path = ""
    # Lotsa' Code
    Tkinter.mainloop()

if __name__ == "__main__":
    main()

def save_f():
    global register
    outFile = open('FobbySave.txt', 'wb')
    pickle.dump(register, outFile)
    outFile.close()
global register     
#At Quit
atexit.register(save_f)

atexit fails. But when I try to print register it has no problem. save_f worked when I put it in the Tkinter loop, but atexit didn't. So can somebody tell me what am I doing wrong?

P.S.

Sorry forgot to write atexit the first time. But it's in my code.

Edit: Orginal code down here

import pickle
import atexit
def save_f():
    global register
    outFile = open('Something.txt', 'wb')
    pickle.dump(register, outFile)
    outFile.close()
atexit.register(save_f)

Solution

  • OK turns out the problem was that I needed atexit.register(save_f) instead of atexit.register(save_f()).

    You're not supposed to make a function call!