def test_trace(self): v = Variable(self.root) vname = str(v) trace = [] def read_tracer(*args): trace.append(('read', ) + args) def write_tracer(*args): trace.append(('write', ) + args) tr1 = v.trace_add('read', read_tracer) tr2 = v.trace_add(['write', 'unset'], write_tracer) self.assertEqual(sorted(v.trace_info()), [(('read', ), tr1), (('write', 'unset'), tr2)]) self.assertEqual(trace, []) v.set('spam') self.assertEqual(trace, [('write', vname, '', 'write')]) trace = [] v.get() self.assertEqual(trace, [('read', vname, '', 'read')]) trace = [] info = sorted(v.trace_info()) v.trace_remove('write', tr1) # Wrong mode self.assertEqual(sorted(v.trace_info()), info) with self.assertRaises(TclError): v.trace_remove('read', 'spam') # Wrong command name self.assertEqual(sorted(v.trace_info()), info) v.get() self.assertEqual(trace, [('read', vname, '', 'read')]) trace = [] v.trace_remove('read', tr1) self.assertEqual(v.trace_info(), [(('write', 'unset'), tr2)]) v.get() self.assertEqual(trace, []) trace = [] del write_tracer gc.collect() v.set('eggs') self.assertEqual(trace, [('write', vname, '', 'write')]) trace = [] del v gc.collect() self.assertEqual(trace, [('write', vname, '', 'unset')])
def test_trace(self): v = Variable(self.root) vname = str(v) trace = [] def read_tracer(*args): trace.append(('read',) + args) def write_tracer(*args): trace.append(('write',) + args) tr1 = v.trace_add('read', read_tracer) tr2 = v.trace_add(['write', 'unset'], write_tracer) self.assertEqual(sorted(v.trace_info()), [ (('read',), tr1), (('write', 'unset'), tr2)]) self.assertEqual(trace, []) v.set('spam') self.assertEqual(trace, [('write', vname, '', 'write')]) trace = [] v.get() self.assertEqual(trace, [('read', vname, '', 'read')]) trace = [] info = sorted(v.trace_info()) v.trace_remove('write', tr1) # Wrong mode self.assertEqual(sorted(v.trace_info()), info) with self.assertRaises(TclError): v.trace_remove('read', 'spam') # Wrong command name self.assertEqual(sorted(v.trace_info()), info) v.get() self.assertEqual(trace, [('read', vname, '', 'read')]) trace = [] v.trace_remove('read', tr1) self.assertEqual(v.trace_info(), [(('write', 'unset'), tr2)]) v.get() self.assertEqual(trace, []) trace = [] del write_tracer gc.collect() v.set('eggs') self.assertEqual(trace, [('write', vname, '', 'write')]) trace = [] del v gc.collect() self.assertEqual(trace, [('write', vname, '', 'unset')])
def SaveVar(var: _TK.Variable, master=None, value=None, name=None, filename='data.pkl') \ -> (_TK.Variable): """Save tkinter variable data in a pickle file and load the same value when the program is executed next time. #### If the content of the file changes, it might not load correct values \ to the assigned variables. To avoid this issue use `name` to \ refer to the exact assigned values later. ### Args: - `var`: Give the `tkinter.Variable` class like (`tk.StringVar`, `tk.IntVar`). - `master`: Parent widget. - `value`: Set value. - `name`: Set a name to group variables or to refer to assigned value when loaded. - `filename`: Set the name of the save file. (To make the file invisible in the \ directory start the name of the file with "." like ".cache-savevar") ### Return: - returns the tk.Variable instance passed to `var` argument. ### Example: root = tk.Tk() var1 = SaveVar(tk.StringVar,'Enter Username','Var1','.cache-savevar') var2 = SaveVar(tk.StringVar,'Enter Password','Var2','.cache-savevar') tk.Entry(root,textvariable=var1).pack() tk.Entry(root,textvariable=var2).pack() root.mainloop()""" def update_val(*args): """Internal function for updating the value for variable""" try: # try/except , if the file doesn't exists. open1 = open(filename, 'rb') tmpdict = pkl.load(open1) # load saved dictionary data. # Block of code to check for the right value. if tmpdict.get(str(var)): old, default = tmpdict.get(str(var)) new = var.get() if new != default: var.set(new) elif new == default and not startup[0]: var.set(default) else: var.set(old) tmpdict.update({str(var): (var.get(), defaultval)}) open1.close() except Exception as e: tmpdict = {} tmpdict[str(var)] = (var.get(), defaultval) open2 = open(filename, 'wb') pkl.dump(tmpdict, open2) startup[0] = False open2.close() startup = [True] if not(filename.endswith('.pickle') or filename.endswith('.pkl')) \ and not filename.startswith('.'): filename = filename + '.pkl' var = var(master=master, value=value, name=name) defaultval = var.get() # get a default value of the variable update_val() for mode, cbname in (var.trace_info()): if mode[0] == 'write' and update_val.__name__ in cbname: try: var.trace_remove('write', cbname) except: pass res = var.trace_add('write', update_val) return var