title: Python - show an XML file on a TreeView structure
body: <p>I have this code that tranverse a dict object and put it on a TreeView. For simple xml it gets ok but with complex xml it doesn't work.</p>
  
  <p>The problem is with the walk_dict function, but I can't get it right.</p>
  
  <pre><code>#-*- encoding: utf8 -*-
  from Tkinter import *
  from ttk import Treeview
  import xmltodict
  
  class App:
  
    def __init__(self, root):
  
      try:
        self.tagsoup = xmltodict.parse(file(sys.argv[1],'r').read())
        self.tree = Treeview(root, height=30)
        self.tree.pack()
        self.last = ''
        self.walk_dict(self.tagsoup)
      except Exception as e:
        print e
  
    def walk_dict(self, d,depth=0):
      for k,v in sorted(d.items(),key=lambda x: x[0]):
        if isinstance(v, dict):
          self.last = k
          self.tree.insert('', 'end', k, text = k)
          self.walk_dict(v,depth+1)
        else:
          self.tree.insert(self.last, 'end', k, text = k)
          self.tree.insert(k, 'end', v, text = v)
  
  root = Tk()
  App(root)
  root.mainloop()
  </code></pre>
  
  <p>The xml I am feeding is this:</p>
  
  <pre><code>&lt;menu&gt;
      &lt;opcao1&gt;Aspargos&lt;/opcao1&gt;
      &lt;opcao2&gt;Tomate frito&lt;/opcao2&gt;
      &lt;opcao3&gt;Abóbora caramelizada&lt;/opcao3&gt;
      &lt;opcao4&gt;Churrasco de ovelha&lt;/opcao4&gt;
      &lt;opcao5&gt;Pizza&lt;/opcao5&gt;
      &lt;opcao6&gt;
          &lt;lol&gt;Copter&lt;/lol&gt;
          &lt;meh&gt;lolcopter&lt;/meh&gt;
          &lt;bla&gt;
              &lt;woo&gt;foo&lt;/woo&gt;
          &lt;/bla&gt;
      &lt;/opcao6&gt;
  &lt;/menu&gt;
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/E28Za.jpg" alt="This is the output">
  This is the output. Note that opcao6 is rendered out of the tree, and its children are rendered below.</p>