【发布时间】:2020-11-11 11:59:33
【问题描述】:
我想保存画布所有“对象”而不是要保存的画布图形。 并加载保存以修改对象(可能更改位置、颜色) 我知道这不是通过泡菜保存小部件对象的方法。 所以我想保存对象的整个配置并在加载时重新创建对象。有谁知道如何实现pickle_save/pickle_load这两个函数来加载/保存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()