How to use num2date/ date2num with Tkinter mainloop()
<p>I have this code inside a tkinter <code>mainloop()</code>:</p>

<pre><code>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")
</code></pre>

<p>I get the following error:</p>

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

<p>Can someone please help out with this?</p>

<p>This is an artifact of subclassing <code>tkinter.Tk</code> and overriding the <code>__init__</code> method without ever calling <code>Tk.__init__</code>:</p>

<pre><code>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
</code></pre>

<p>This happens because: </p>

<ol>
<li><code>Tk.__init__</code> initializes the very important <code>.tk</code> attribute.</li>
<li>Any attribute lookup (that cannot be resolved) is forwarded to the <code>.tk</code> attribute.</li>
</ol>

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

<hr>

<h2>To fix this</h2>

<p>Just remember to call <code>Tk.__init__(self)</code> in your initialize method:</p>

<pre><code>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
</code></pre>

python tkinter tail-recursion