How to use num2date/ date2num with Tkinter mainloop()
Question: I have this code inside a tkinter `mainloop()`:

    
    
    self.raw_start_date = num2date(date2num(dt.datetime.strptime(self.end_date, "%Y-%m-%d")) - self.period)
    self.start_date = self.raw_start_date.strftime("%Y-%m-%d")
    

I get the following error:

> File "D:\Python35-32\lib\tkinter__init__.py", line 1949, in **getattr**
> return getattr(self.tk, attr) RecursionError: maximum recursion depth
> exceeded

Can someone please help out with this?


Answer: This is an artifact of subclassing `tkinter.Tk` and overriding the `__init__`
method without ever calling `Tk.__init__`:

    
    
    import tkinter
    
    class Application(tkinter.Tk):
        def __init__(self):
            "do out stuff, forget to call Tk.__init__(self) !"
            pass
    
    
    app = Application()
    
    app.any_possible_attribute_name #recursion error here
    

This happens because:

  1. `Tk.__init__` initializes the very important `.tk` attribute.
  2. Any attribute lookup (that cannot be resolved) is forwarded to the `.tk` attribute.

So normally if you did `app.thing` and `.thing` was not already defined then
it would try to return `app.tk.thing`, but when `app.tk` is not defined then
it tries to look up `app.tk.tk` which requires looking up `app.tk` which
causes the recursion error.

* * *

## To fix this

Just remember to call `Tk.__init__(self)` in your initialize method:

    
    
    import tkinter
    
    class Application(tkinter.Tk):
        def __init__(self):
            "do out stuff, just make sure to call Tk.__init__(self) !"
            tkinter.Tk.__init__(self)
    
    
    app = Application()
    
    app.any_possible_attribute_name
    #now we just get an AttributeError
    

