import json
from tkinter import *
import os
listai = json.load(open("listai.txt"))
listap = json.load(open("listap.txt"))
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("dicionario.txt", "w")
dict[item] = value
d.write(json.dumps(dict))
listai.append(item)
try:
listap.append(float(value))
except ValueError:
listap.append(int(value))
i = open("listai.txt", "w").write(json.dumps(listai))
p = open("listap.txt", "w").write(json.dumps(listap))
#dictAdd("cereja", 3.9)
def dictcreator():
global listai, listap
for x in range(len(listai)):
dict[listai[x]] = listap[x]
d = open("dicionario.txt", "w").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"{addtopwidth}x{addtopwidth}+{xwpos}+{ywpos}")
addtop.resizable(height = 0, width = 0)
addtop.config(bg="lightgrey")
addmainlabel = Label(addtop, text="Adicionar alimento novo", font="arial, 19", fg="green", bg="lightgrey").grid(pady=10, padx=8, row=0, column=0, columnspan=2)
addlabeln = Label(addtop, text="Alimento -->", bg="lightgrey", font="arial, 10").grid(row=1, column=0, padx=2, pady=8)
addentryn = Entry(addtop, bg="lightgrey", textvariable = addentryname).grid(row=1, column=1, padx=2, pady=5)
addlabelp = Label(addtop, text="Preço-->", bg="lightgrey", font="arial, 10").grid(row=2, column=0, padx=2, pady=8)
addentryp = Entry(addtop, bg="lightgrey", textvariable = addentryprice).grid(row=2, column=1, padx=2, pady=5)
addbutton = Button(addtop, text="Adicionar", font="arial, 10", fg="green", relief=GROOVE, bg="lightgrey", command = lambda: dictAdd(str(addentryname.get()), str(addentryprice.get())))
addbutton.grid(pady= 20, padx=10, row=3, column=0, columnspan=2)
addtop.mainloop()
我收到错误:当我按下名为“ addbutton”的按钮时,“ TypeError:键必须为str,int,float,bool或None,而不是Button”如果需要,这里是终端的更多输出:
laranja
5.7
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Asus\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "c:/Users/Asus/PycharmProjects/lista de compras/lista de compras.py", line 57, in <lambda>
addbutton = Button(addtop, text="Adicionar", font="arial, 10", fg="green", relief=GROOVE, bg="lightgrey", command = lambda: dictAdd(str(addentryname.get()), str(addentryprice.get())))
File "c:/Users/Asus/PycharmProjects/lista de compras/lista de compras.py", line 16, in dictAdd
dictcreator()
File "c:/Users/Asus/PycharmProjects/lista de compras/lista de compras.py", line 36, in dictcreator
d = open("dicionario.txt", "w").write(json.dumps(dict))
File "C:\Users\Asus\AppData\Local\Programs\Python\Python38-32\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Users\Asus\AppData\Local\Programs\Python\Python38-32\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\Asus\AppData\Local\Programs\Python\Python38-32\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
TypeError: keys must be str, int, float, bool or None, not Button
我想知道是否有人可以告诉我如何解决此错误:D。如果有人向我解释解决此错误的方法,我将非常感激。
为了更好地理解代码,基本上,这是一个添加/删除/添加购物车商品的程序,文件“ listai.txt”是所有食品的数组/列表。文件“ listap.txt”是所有价格的数组/列表。基本上,我所做的就是创建一个名为dictcreator的函数,该函数创建字典(称为“ dicionario.txt”,但该字典是字典,但使用葡萄牙语),例如,从listai.txt获得项目1,从listap.txt获得价格1,聚在一起。
外部文件:
listai:[“ cereja”,“ banana”,“ pera”,“ maca”,“ pessego”]
listap:[“ 1.2”,“ 1.9”,“ 2.3”,“ 3.1”,“ 3.9”]
dicionario:{“ cereja”:1.2,“ banana”:1.9,“ pera”:2.3,“ maca”:3.1,“ pessego”:3.9}]
[在python中,字典键可以是任何可哈希的对象,而在json中,它只能是str, int, float, bool or None
。如果需要,请尝试使用dill
或pickle
保存tkinter对象(不需要)。希望对您有所帮助!