title: How to fix tkinter TypeError: keys must be str, int, float, bool or None, not Button?
body: <pre><code>import json 
  from tkinter import *
  import os
  
  listai = json.load(open(&quot;listai.txt&quot;))
  listap = json.load(open(&quot;listap.txt&quot;))
  dict = {}
  
  
  
  def dictAdd(item, value):
       global listai, listap
       print(item) #i did this so i could see where it was getting the error
       print(value)
       if item not in listai:
            dictcreator()
            d = open(&quot;dicionario.txt&quot;, &quot;w&quot;)
            dict[item] = value
            d.write(json.dumps(dict))
            listai.append(item)
            try:
                 listap.append(float(value))
            except ValueError:
                 listap.append(int(value))
            i = open(&quot;listai.txt&quot;, &quot;w&quot;).write(json.dumps(listai))
            p = open(&quot;listap.txt&quot;, &quot;w&quot;).write(json.dumps(listap))
  
  #dictAdd(&quot;cereja&quot;, 3.9)
  
  
  
  def dictcreator():
       global listai, listap
       for x in range(len(listai)):
            dict[listai[x]] = listap[x]
       d = open(&quot;dicionario.txt&quot;, &quot;w&quot;).write(json.dumps(dict))
  
  #dictcreator()
  
  
  def Add():
       addtop = Toplevel()
       addtopheight = 300
       addtopwidth = 300
       addentryname = StringVar()
       addentryprice = StringVar()
       xwpos = int(mainwindow.winfo_screenwidth()/2 - addtopwidth/2)
       ywpos = int(mainwindow.winfo_screenheight()/2 - addtopheight/2)
       addtop.geometry(f&quot;{addtopwidth}x{addtopwidth}+{xwpos}+{ywpos}&quot;)
       addtop.resizable(height = 0, width = 0)
       addtop.config(bg=&quot;lightgrey&quot;)
       addmainlabel = Label(addtop, text=&quot;Adicionar alimento novo&quot;, font=&quot;arial, 19&quot;, fg=&quot;green&quot;, bg=&quot;lightgrey&quot;).grid(pady=10, padx=8, row=0, column=0, columnspan=2)
       addlabeln = Label(addtop, text=&quot;Alimento --&gt;&quot;, bg=&quot;lightgrey&quot;, font=&quot;arial, 10&quot;).grid(row=1, column=0, padx=2, pady=8)
       addentryn = Entry(addtop, bg=&quot;lightgrey&quot;, textvariable = addentryname).grid(row=1, column=1, padx=2, pady=5)
       addlabelp = Label(addtop, text=&quot;Preço--&gt;&quot;, bg=&quot;lightgrey&quot;, font=&quot;arial, 10&quot;).grid(row=2, column=0, padx=2, pady=8)
       addentryp = Entry(addtop, bg=&quot;lightgrey&quot;, textvariable = addentryprice).grid(row=2, column=1, padx=2, pady=5)
       addbutton = Button(addtop, text=&quot;Adicionar&quot;, font=&quot;arial, 10&quot;, fg=&quot;green&quot;, relief=GROOVE, bg=&quot;lightgrey&quot;, command = lambda: dictAdd(str(addentryname.get()), str(addentryprice.get())))
       addbutton.grid(pady= 20, padx=10, row=3, column=0, columnspan=2)
       addtop.mainloop()
  def mainwindow():
       global mainwindow
       width = 700
       height = 400
       mainwindow = Tk()
       xwpos = int(mainwindow.winfo_screenwidth()/2 - width/2)
       ywpos = int(mainwindow.winfo_screenheight()/2 - height/2)
       mainwindow.geometry(f&quot;{width}x{height}+{xwpos}+{ywpos}&quot;)
       mainwindow.config(bg=&quot;lightgrey&quot;)
       mainwindow.title(&quot;Lista de Compras&quot;)
       mainwindow.resizable(height = 0, width = 0)
   
       mainlabel = Label(mainwindow, text=&quot;Lista de Compras&quot;, fg=&quot;grey&quot;, width=18, bg=&quot;lightgrey&quot;, font=&quot;arial, 32&quot;, justify=&quot;right&quot;).grid(pady=15, padx = 10, row=0, column=0, columnspan = 4)
       Button(mainwindow, text=&quot;Add&quot;, fg=&quot;green&quot;, width=5, command=Add, relief=GROOVE).grid(padx= 20, pady=2, row=0, column=5)
  
       def edit():
            edittop = Toplevel()
            edittopheight = 200
            edittopwidth = 200
            xwpos = int(mainwindow.winfo_screenwidth()/2 - edittopwidth/2)
            ywpos = int(mainwindow.winfo_screenheight()/2 - edittopheight/2)
            edittop.geometry(f&quot;{edittopwidth}x{edittopwidth}+{xwpos}+{ywpos}&quot;)
            Label(edittop, text=&quot;w&quot;).grid(row=0, column=0)
            edittop.mainloop
   
   
       for x in range(1, len(listai) + 1):
            listai[x-1] = Label(mainwindow, text=f&quot;{listai[x-1]} --&gt; {listap[x-1]}€&quot;, font=&quot;18&quot;, fg=&quot;black&quot;, bg=&quot;lightgrey&quot;, justify=&quot;left&quot;, width=15).grid(padx = 10, pady = 2, row=x, column=1)
            listai[x-1] = Entry(mainwindow, fg=&quot;black&quot;, bg=&quot;lightgrey&quot;, textvariable=f&quot;{listai[x-1]}tv&quot;).grid(padx = 5, pady=2, row=x, column=2)
            listai[x-1] = Button(mainwindow, text=&quot;Edit&quot;, fg=&quot;purple&quot;, width=5, command=edit, relief=GROOVE).grid(padx= 20, pady=2, row=x, column=4)
            listai[x-1] = Button(mainwindow, text=&quot;Eliminate&quot;, fg=&quot;red&quot;, width=7, command=edit, relief=GROOVE).grid(padx= 20, pady=2, row=x, column=5)
  
            listai[x-1] = Button(mainwindow, text=&quot;Eliminate&quot;, fg=&quot;red&quot;, width=7, command=edit, relief=GROOVE)#.grid(padx= 20, pady=2, row=x, column=5)
            listai[x-1] = Button(mainwindow, text=&quot;Eliminate&quot;, fg=&quot;red&quot;, width=7, command=edit, relief=GROOVE)#.grid(padx= 20, pady=2, row=x, column=5)
  
        
       mainwindow.mainloop()
  
  mainwindow()
  </code></pre>
  <p>I get the error:</p>
  <blockquote>
  <p>&quot;TypeError: keys must be str, int, float, bool or None, not Button&quot;,</p>
  </blockquote>
  <p>when I press the button called &quot;addbutton&quot;. If needed here is a little more output of the terminal:</p>
  <pre><code>laranja
  5.7
  Exception in Tkinter callback
  Traceback (most recent call last):
    File &quot;C:\Users\Asus\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py&quot;, line 1883, in __call__
  return self.func(*args)
    File &quot;c:/Users/Asus/PycharmProjects/lista de compras/lista de compras.py&quot;, line 57, in &lt;lambda&gt;
  addbutton = Button(addtop, text=&quot;Adicionar&quot;, font=&quot;arial, 10&quot;, fg=&quot;green&quot;, relief=GROOVE, bg=&quot;lightgrey&quot;, command = lambda: dictAdd(str(addentryname.get()), str(addentryprice.get())))
    File &quot;c:/Users/Asus/PycharmProjects/lista de compras/lista de compras.py&quot;, line 16, in dictAdd
  dictcreator()
    File &quot;c:/Users/Asus/PycharmProjects/lista de compras/lista de compras.py&quot;, line 36, in dictcreator
      d = open(&quot;dicionario.txt&quot;, &quot;w&quot;).write(json.dumps(dict))
    File &quot;C:\Users\Asus\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py&quot;, line 231, in dumps
      return _default_encoder.encode(obj)
    File &quot;C:\Users\Asus\AppData\Local\Programs\Python\Python38-32\lib\json\encoder.py&quot;, line 199, in encode
      chunks = self.iterencode(o, _one_shot=True)
    File &quot;C:\Users\Asus\AppData\Local\Programs\Python\Python38-32\lib\json\encoder.py&quot;, line 257, in iterencode
      return _iterencode(o, 0)
  TypeError: keys must be str, int, float, bool or None, not Button
  </code></pre>
  <p>I was wondering if someone could tell me how to fix this error :D.
  I really appreciate if someone explains to me a way to fix this error.</p>
  <p>To better understanding of the code, basically this is a program to add/remove/add shopping cart items, the file &quot;listai.txt&quot; is a array/list of all the food items.
  the file &quot;listap.txt&quot; is a array/list of all the prices.
  basically what I did was creating a function called dictcreator that creates a dictionary(called &quot;dicionario.txt&quot;, which is dictionary but in Portuguese) getting, for example, the item 1 from listai.txt and the price 1 from listap.txt and getting those together.</p>
  <p><strong>EXTERNAL FILES:</strong></p>
  <p>listai: [&quot;cereja&quot;,&quot;banana&quot;,&quot;pera&quot;,&quot;maca&quot;,&quot;pessego&quot;]</p>
  <p>listap: [&quot;1.2&quot;, &quot;1.9&quot;, &quot;2.3&quot;, &quot;3.1&quot;, &quot;3.9&quot;]</p>
  <p>dicionario: {&quot;cereja&quot;: 1.2, &quot;banana&quot;: 1.9, &quot;pera&quot;: 2.3, &quot;maca&quot;: 3.1, &quot;pessego&quot;: 3.9}</p>