title: Run Python with Tkinter (sometimes) headless OR replacement for root.after()
body: <p>I have working code below.</p>
  
  <p>I have a set of machines operated with Python.  I have a gui in Tkinter but very often these machines are run headless with the python code auto-starting at boot.</p>
  
  <p>I really like the design pattern of using root.after() to start multiple tasks and keep them going.  My problem is that this comes from the Tkinter library and when running headless the line "root=Tk()" will throw an error.</p>
  
  <p>I have two questions</p>
  
  <ol>
  <li>Can I perform some trick to have the code ignore the fact there is no display?</li>
  </ol>
  
  <p>OR</p>
  
  <ol start="2">
  <li>Is there a library that will match the design pattern of Tkinter "root.after(time_in_ms,function_to_call)".</li>
  </ol>
  
  <p>I did try to poke around in the underlying code of Tkinter to see if there was simply another library wrapped by Tkinter but I don't have the skill to decode what is going on in that library.</p>
  
  <p>This code works with a display connected: (it prints hello 11 times then ends)</p>
  
  <pre><code>from Tkinter import *
  
  #   def __init__(self, screenName=None, baseName=None, className='Tk', useTk=1, sync=0, use=None):
  
  root = Tk()  # error is thrown here if starting this command in headless hardware setup
  
  h = None
  count = 0
  c = None
  
  
  def stop_saying_hello():
      global count
      global h
      global c
      if count &gt; 10:
          root.after_cancel(h)
          print "counting cancelled"
      else:
          c = root.after(200, stop_saying_hello)
  
  
  def hello():
      global h
      global count
      print "hello " + str(count)
      count += 1
      h = root.after(1000, hello)
  
  
  h = root.after(1000, hello)  # time in ms, function
  c = root.after(200, stop_saying_hello)
  
  
  root.mainloop()
  </code></pre>
  
  <p>If this is run headless - in an ssh session from a remote computer then this error message is returned</p>
  
  <blockquote>
    <p>Traceback (most recent call last):   File "tkinter_headless.py", line
    5, in 
        root = Tk()   File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1813, in <strong>init</strong>
        self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
    _tkinter.TclError: no display name and no $DISPLAY environment variable</p>
  </blockquote>