Python 类型错误:can';使用dill库时pickle\u tkinter.tkapp对象

Python 类型错误:can';使用dill库时pickle\u tkinter.tkapp对象,python,tkinter,compiler-errors,pickle,dill,Python,Tkinter,Compiler Errors,Pickle,Dill,所以,我有这样的课: class Category: def __init__(self,name): self.bt = Button(t, text = name, command = lambda : opencategory(name)) self.packing() def packing(self): self.bt.pack(fill = X) def pack_forg(self): self.bt.pack_forget() list_cat

所以,我有这样的课:

class Category:
def __init__(self,name):
    self.bt = Button(t, text = name, command = lambda : opencategory(name))
    self.packing()
def packing(self):
    self.bt.pack(fill = X)
def pack_forg(self):
    self.bt.pack_forget()
list_cat = [Category("a"), Category("b"), Category("c")]
然后创建类别对象列表,例如:

class Category:
def __init__(self,name):
    self.bt = Button(t, text = name, command = lambda : opencategory(name))
    self.packing()
def packing(self):
    self.bt.pack(fill = X)
def pack_forg(self):
    self.bt.pack_forget()
list_cat = [Category("a"), Category("b"), Category("c")]
我使用dill模块将此列表保存到文件中:

   if list_cat != []:
        with open("file.pkl","wb") as f2:
            dill.dump(list_cat, f2)
但是当我运行它时,它返回
TypeError:cannotpickle\u tkinter.tkapp
我对任何类型的解决方案都持开放态度,使用JSON、pickle、dill或其他任何东西

以下是完整的工作代码:

from tkinter import *
import os.path
from os import path
import dill

t = Tk()
t.title("test7")
t.geometry("100x300")
a = 1
list_cat = []

if path.exists("test777.pkl"):
    with open("test777.pkl","rb") as filee:
        list_cat = dill.load(filee)    

def opencategory(sampletext):
    print(sampletext)

class Category:  
    def __init__(self,sampletext):
        self.bt = Button(t, text = sampletext, command = lambda : opencategory(sampletext))
        self.packing()
    def packing(self):
        self.bt.pack(fill = X)
    def pack_forg(self):
        self.bt.pack_forget()


def save_c():
    if list_cat != []:
        with open("test777.pkl","wb") as file:
            dill.dump(list_cat, file)
def add_c():
    global a 
    list_cat.append(Category(a))
    a += 1


save = Button(t, text = "save", command = save_c)
save.pack(fill = X)
add = Button(t, text = "add", command = add_c)
add.pack(fill = X)


for i in list_cat:
    i.packing()

您正在尝试pickle对象列表。每个对象都包含一个小部件。不能对小部件进行pickle。这根本做不到。

我是dill的作者。我同意布莱恩·奥克利的观点。但更清楚的是,
\u tkinter
不是python(我相信它是C++)。问题是,任何python序列化程序都无法对非python对象进行pickle,除非位于其上方的python类给出序列化指令(就像
numpy
数组那样)<代码>Tkinter没有

您可以在
dill
中(在
dill.settings
中)尝试不同的序列化变体,但很可能会失败


您可以潜在地构建一个派生的小部件类,其中添加了序列化指令,通知序列化程序如何存储小部件的状态。这应该行得通——从本质上说,这就是任何类对象都可以序列化的方法。

任何东西不是也被命名为list_cat或f2吗?也请张贴完整的工作代码!