Jul-31-2019, 02:30 PM
The problem comes from using global variables s1, s2, s2. Try this version
import os import tkinter as tk import pickle s = [0, 0, 0] class Cbuttons: def __init__(self): self.root = tk.Tk() self.root.title("Checklist") self.checkvars = [tk.IntVar() for i in range(3)] self.main() def save(self): print('To save:', *s) with open('asy.txt', 'wb') as f: pickle.dump(s, f) print('Saved:', *s) def loading(self): if not os.path.exists('asy.txt'): with open('asy.txt', 'wb') as f: pickle.dump(s, f) with open('asy.txt', 'rb') as f: s[:] = pickle.load(f) for v, n in zip(self.checkvars, s): v.set(n) print('Loaded:', *s) def confirm(self): s[:] = [1 if v.get() else 0 for v in self.checkvars] print('chek status:', *s) self.save() def main(self): C1 = tk.Checkbutton( self.root, text="Option A", variable=self.checkvars[0], anchor='w', onvalue=1, offvalue=0, height=1, width=40, command=self.confirm) C2 = tk.Checkbutton( self.root, text="Option B", variable=self.checkvars[1], anchor='w', onvalue=1, offvalue=0, height=1, width=40, command=self.confirm) C3 = tk.Checkbutton( self.root, text="Option C", variable=self.checkvars[2], anchor='w', onvalue=1, offvalue=0, height=1, width=40, command=self.confirm) C1.pack() C2.pack() C3.pack() b = tk.Button(self.root, text="OK", command=self.loading) b.pack() self.root.mainloop() if __name__ == '__main__': Cbuttons()