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
#1
Hi!

This i s my first post, i got a problem with code what i make. Problem is that pickle dont get ver. when is changed in tkinter. I use many steps to check when ver. is 0 or 1. Maybe something wrong with declaration of var?

here is repls: https://repl.it/repls/CadetblueMysteriousTechnician

import tkinter as tk
import pickle

s1=s2=s3=0

class Cbuttons:
   def __init__(self):
       self.root = tk.Tk()
       self.root.title("Checklist")
       self.CheckVar1 = tk.IntVar() 
       self.CheckVar2 = tk.IntVar() 
       self.CheckVar3 = tk.IntVar() 
       self.main()


   def save(self):
     print('To save:',s1,s2,s3)
     with open('asy.txt', 'wb') as f:
       pickle.dump([s1,s2,s3], f)
       print('Saved!',s1,s2,s3)

   def loading(self):
     with open('asy.txt', 'rb') as f:
       s1,s2,s3= pickle.load(f)
       print(s1,s2,s3)

   def confirm(self):
       if self.CheckVar1.get():
          s1=1
          pickle.dump(s1, open("asy.txt", "wb"))

       else:
          s1=0
       if self.CheckVar2.get():
         s2=1
       else:
         s2=0
       if self.CheckVar3.get():
         s3=1
       else:
         s3=0
       print('chek status:',s1,s2,s3)
       self.save()  
 
   def main(self):
       C1 = tk.Checkbutton(self.root, text="Option A", variable=self.CheckVar1, anchor='w', onvalue=1,
                           offvalue=0, height=1, width=40, command=self.confirm)
       C2 = tk.Checkbutton(self.root, text="Option B", variable=self.CheckVar2, anchor='w', onvalue=1,
                           offvalue=0, height=1, width=40, command=self.confirm)
       C3 = tk.Checkbutton(self.root, text="Option C", variable=self.CheckVar3, 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()



   #https://python-forum.io/Thread-Tkinter-Print-if-all-checkboxes-marked
Reply
#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
#3
(Jul-31-2019, 02:30 PM)Gribouillis Wrote: The problem comes from using global variables s1, s2, s2. Try this version

I understood, ty Heart for your help. When we need another group (checkbox) name: "d" we add something like that or something else?
In my program that code is for setup window. That kind will take all choices what user make. I thinking, that file 'asy.txt' keeps all variables simply numbers and choices. So i try make two groups:
1st- variables only numbers exp.: (ax,ay,aR,aG,aB -1st group, bx,by,bR,bG,bB -2nd group)<- that is for main program (he will load that)
2nd - choises only predifine via user like that code from setup window ( s1,s2,s3 - 1st group, d1,d2 - 2nd group)

So my question is ho wwe make groups in file in array or jsut like you show me?

here i add second group its correct?:
import os
import tkinter as tk
import pickle
  
s = [0, 0, 0]
d = [0, 0]
  
class Cbuttons:
   def __init__(self):
       self.root = tk.Tk()
       self.root.title("Checklist")
       self.checkvars = [tk.IntVar() for i in range(5)]
       self.main()
  
  
   def save(self):
     print('To save:', *s)
     with open('asy.txt', 'wb') as f:
       pickle.dump([s,d], f)
       print('Saved:', *s,*d)
  
   def loading(self):
     if not os.path.exists('asy.txt'):
         with open('asy.txt', 'wb') as f:
             pickle.dump([s,d], f)
     with open('asy.txt', 'rb') as f:
       s[:] = pickle.load(f)
       d[:] = pickle.load(f)
       for v, n in zip(self.checkvars, [s,d]):
           v.set(n)
       print('Loaded:', *s,*d)
  
   def confirm(self):
       s[:] = [1 if v.get() else 0 for v in self.checkvars]
       d[:] = [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)
       D1 = tk.Checkbutton(
           self.root, text="Option ba",
           variable=self.checkvars[3], anchor='w', onvalue=1,
                           offvalue=0, height=1, width=40, command=self.confirm)
       D2 = tk.Checkbutton(
           self.root, text="Option bb",
           variable=self.checkvars[4], anchor='w', onvalue=1,
                           offvalue=0, height=1, width=40, command=self.confirm)
       C1.pack()
       C2.pack()
       C3.pack()
       D1.pack()
       D2.pack()
       b = tk.Button(self.root, text="OK", command=self.loading)
       b.pack()
  
       self.root.mainloop()
   
   
if __name__ == '__main__':
   Cbuttons()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  TypeError: cannot pickle ‘_asyncio.Future’ object Abdul_Rafey 1 361 Mar-07-2024, 03:40 PM
Last Post: deanhystad
  pickle problem DPaul 13 4,700 Sep-27-2022, 05:25 PM
Last Post: DPaul
  TypeError: cannot pickle n00sferatu 1 2,640 Dec-14-2021, 03:52 PM
Last Post: yakkaligiri
  Multiprocessing Can't pickle local object law 1 15,936 Aug-30-2021, 02:49 PM
Last Post: law
  Save/Loading using pickle Scordomaniac 4 3,024 Nov-24-2020, 06:11 PM
Last Post: Scordomaniac
  computing entropy using pickle files baran01 2 2,412 Dec-30-2019, 09:45 PM
Last Post: micseydel
  pickle docs say bytes in one place, strings in another Skaperen 2 2,139 Jul-29-2019, 05:13 PM
Last Post: Skaperen
  pickle error SheeppOSU 4 10,945 Apr-20-2019, 04:50 PM
Last Post: SheeppOSU
  Using pickle.dump Friend 1 2,939 Feb-15-2019, 04:39 PM
Last Post: metulburr
  I'm having trouble with an OOP version of Pickle functionality CodeWolf 2 2,353 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