Python Forum
Tkinter don't get ver from file via pickle
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter don't get ver from file via pickle
#2
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()
Reply


Messages In This Thread
RE: Tkinter don't get ver from file via pickle - by Gribouillis - Jul-31-2019, 02:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 3,029 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  pickle problem DPaul 13 10,838 Sep-27-2022, 05:25 PM
Last Post: DPaul
  TypeError: cannot pickle n00sferatu 1 3,989 Dec-14-2021, 03:52 PM
Last Post: yakkaligiri
  Multiprocessing Can't pickle local object law 1 24,961 Aug-30-2021, 02:49 PM
Last Post: law
  Save/Loading using pickle Scordomaniac 4 4,607 Nov-24-2020, 06:11 PM
Last Post: Scordomaniac
  computing entropy using pickle files baran01 2 3,461 Dec-30-2019, 09:45 PM
Last Post: micseydel
  pickle docs say bytes in one place, strings in another Skaperen 2 3,106 Jul-29-2019, 05:13 PM
Last Post: Skaperen
  pickle error SheeppOSU 4 13,071 Apr-20-2019, 04:50 PM
Last Post: SheeppOSU
  Using pickle.dump Friend 1 3,947 Feb-15-2019, 04:39 PM
Last Post: metulburr
  I'm having trouble with an OOP version of Pickle functionality CodeWolf 2 3,432 Dec-19-2018, 05:41 PM
Last Post: CodeWolf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020