Hello!
Please tell me how you can save a dictionary with a picture of pil with pickle.
Without pickle, everything works well, and when I connect pickle it does not work, it gives an error:
TypeError: cannot pickle '_tkinter.tkapp' object
I solved the problem with saving the image in the dictionary, but another appeared - the image does not appear in canvas. I know that it’s kind of because of garbage collection, but I don’t remember how to solve it. Tell me please.
Please tell me how you can save a dictionary with a picture of pil with pickle.
Without pickle, everything works well, and when I connect pickle it does not work, it gives an error:
TypeError: cannot pickle '_tkinter.tkapp' object
import tkinter as tk from tkinter import* from PIL import Image, ImageTk import pickle root = tk.Tk() root.title('WS_2736_01') root.geometry('500x500+2+10') def create_gamer(): id_gamer = '001' name = 'name' gamer = {'id': id_gamer, 'name': name} planet_num = 1 planet_coord = (100, 100) planet_image = fon01_im planet = {'num': planet_num, 'coord': planet_coord, 'image': planet_image} gamers = {'gamer': gamer, 'planet' : planet} with open('C:\\ws4\\general\\gamers_prim.pickle', 'wb') as f: pickle.dump(gamers, f) create_map(gamers) def create_map(gamers): im_001 = canvas1.create_image(gamers['planet']['coord'], image = gamers['planet']['image']) canvas1 = Canvas(root, width = 500, height = 500, bg = "#00EFDD", highlightthickness = 0) canvas1.place(x = 0, y = 0) fon01 = Image.open('C:\\ws4\\general\\gamer_image.PNG').convert('RGBA') fon01.putalpha(100) fon01_im = ImageTk.PhotoImage(fon01) create_gamer() root.mainloop()can I somehow save the dictionary with pictures in any way, not necessarily pickle?
I solved the problem with saving the image in the dictionary, but another appeared - the image does not appear in canvas. I know that it’s kind of because of garbage collection, but I don’t remember how to solve it. Tell me please.
import tkinter as tk from tkinter import* from PIL import Image, ImageTk import pickle root = tk.Tk() root.title('WS_2736_01') root.geometry('500x500+2+10') def create_gamer(): id_gamer = '001' name = 'name' gamer = {'id': id_gamer, 'name': name} planet_num = 1 planet_coord = (100, 100) planet_image = Image.open('C:\\ws4\\general\\gamer_image.PNG').convert('RGBA') planet = {'num': planet_num, 'coord': planet_coord, 'image': planet_image} gamers = {'gamer': gamer, 'planet' : planet} with open('C:\\ws4\\general\\gamers_prim.pickle', 'wb') as f: pickle.dump(gamers, f) with open('C:\\ws4\\general\\gamers_prim.pickle', 'rb') as f: uni = pickle.load(f) if gamers == uni: print('OK') print(gamers['planet']['image']) #<PIL.Image.Image image mode=RGBA size=60x60 at 0x201220BC730> print(uni['planet']['image']) #<PIL.Image.Image image mode=RGBA size=60x60 at 0x20121CFF6D0> create_map(gamers) def create_map(gamers): fon_003 = gamers['planet']['image'] fon_003.putalpha(100) fon003_im = ImageTk.PhotoImage(fon_003) im_001 = canvas1.create_image(gamers['planet']['coord'], image = fon003_im) canvas1 = Canvas(root, width = 500, height = 500, bg = "#00EFDD", highlightthickness = 0) canvas1.place(x = 0, y = 0) create_gamer() root.mainloop()