TypeError:使用莳萝库时无法腌制_tkinter.tkapp对象

问题描述

所以,我有这样的课:

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")]

我使用莳萝模块将该列表保存到文件中

   if list_cat != []:
        with open("file.pkl","wb") as f2:
            dill.dump(list_cat,f2)

但是当我运行它时,它返回TypeError: can't pickle _tkinter.tkapp 我愿意接受使用JSON,泡菜,​​莳萝或其他任何方法的任何解决方案。

这是完整的工作代码:

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()

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)