I want to save canvas all "object" not the graph of canvas to save. And load the saving to modify the object(maybe change position,color) I know that is no way to save widget object by pickle. So I want to save the entire config of object and recreate object when loading. Is any one know how to achieve pickle_save/pickle_load this two function which will load/save self.c.
from tkinter.ttk import *
from tkinter import *
import tkinter.filedialog
import pickle
class Test_Gui(Frame):
def __init__(self, root, run_class=None):
Frame.__init__(self, root)
self.run_class = run_class
self.root = root
root.geometry("+400+50")
root.title("This is ping status")
self.width = 600
self.height = 400
# create canvas at begin will be saved or replace by loading
self.c = Canvas(self, width=self.width, height=self.height,
bd=0, bg="green", highlightthickness=5, relief=RIDGE, borderwidth=2,
scrollregion=(0, 0, self.width, self.height))
self.c.pack()
self.canvas_bind()
self.setting_menu()
self.pack()
def setting_menu(self):
self.rootmenu = Menu(self.root, tearoff=False)
self.root.config(menu=self.rootmenu)
openmenu = Menu(self.rootmenu, tearoff=False)
self.rootmenu.add_cascade(label='Open', menu=openmenu, underline=0)
def pickle_save():
# Some method to save widget object/config
# save self.c to file
pass
def pickle_load():
# Some method to loading save
# destory self.c or origin and load file tot self.c
pass
openmenu.add_command(label='Save', command=pickle_save)
openmenu.add_command(label='Load', command=pickle_load)
openmenu.add_command(label='Exit', command=self.root.destroy)
def canvas_bind(self):
def left_click(event):
x, y = self.c.canvasx(event.x), self.c.canvasx(event.y)
x1, y1 = (x - 3), (y - 3)
x2, y2 = (x + 3), (y + 3)
self.c.create_oval(x1, y1, x2, y2, fill="red")
self.c.bind('<Button-1>', left_click)
if __name__ == '__main__':
root = Tk()
root.bind("<Escape>", lambda *x: root.destroy())
root.geometry("300x200+50+50")
top = Toplevel(root)
Test_Gui(root=top)
root.mainloop()