Python Forum
Checkbutton code not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Checkbutton code not working
#1
Hello
I'm trying to determine the states of checkboxes depending whether or not they had been checked the previous time the programme was run. I do not know what i'm doing wrong.

from tkinter import *
to_do_list = ['a', 'b', 'c', 'd']
already_done = []
fh = open('mem.txt', 'r')
for line in fh:
	already_done.append(line[:-1])

class checker():

	global already_done

	def __init__(self, root, name):
		self.root = root
		self.name = name
		if self.name in already_done:
			self.CheckVar = IntVar(value=1)
		else:
			self.CheckVar = IntVar(value=0)		
        self.checkbutton = Checkbutton(self.root, text = name, variable = self.CheckVar)	
		self.checkbutton.pack()
		
root = Tk()
for item in to_do_list:
	c = checker(root, item)

root.mainloop()
Inputs please :)
Thank you.
Reply
#2
What does mem.txt look like?
what is the error (post traceback verbatim)
One thing for sure self.CheckVar should be declared prior to the if statement and then set in the if,
like
from tkinter import *
to_do_list = ['a', 'b', 'c', 'd']
already_done =
fh = open('mem.txt', 'r')
for line in fh:
    already_done.append(line[:-1])
 
class checker():
 
    global already_done
 
    def __init__(self, root, name):
        self.root = root
        self.name = name
        self.checkvar = IntVar(0)
        if self.name in already_done:
            self.CheckVarvalue.set(1)
        self.checkbutton = Checkbutton(self.root, text = name, variable = self.CheckVar)    
        self.checkbutton.pack()
         
root = Tk()
for item in to_do_list:
    c = checker(root, item)
root.mainloop()
Reply
#3
mem.txt has two lines, with a on one and b on another.
There is no error message..
When i select something and close, mem.txt has those options in.
When i re-open the GUI, there is a new, unchecked list of buttons though.

İmage


file:///home/tr/Pictures/sc1.png

file:///home/tr/Pictures/sc1.png

İmage


This should work..
Reply
#4
You're better off using a dictionary, it makes the code much easier to follow:
import tkinter as tk


class checker():
    def __init__(self, root):
        self.root = root
        self.to_do_list = ['a', 'b', 'c', 'd']
        self.ckbtns = {}
        self.read_file()

    def read_file(self):
        with open('mem.txt') as fh:
            for line in fh:
                this_item = line[:-1]
                if this_item not in self.ckbtns:
                    self.ckbtns[this_item] = {}
                    self.ckbtns[this_item]['var'] = tk.IntVar()
                    self.ckbtns[this_item]['var'].set(1)
                    self.ckbtns[this_item]['ckbtn'] = tk.Checkbutton(self.root, text=this_item,
                                                                  variable=self.ckbtns[this_item]['var'])
                    self.ckbtns[this_item]['ckbtn'].pack()

def main():
    root = tk.Tk()
    ck = checker(root)
    root.mainloop()

if __name__ == '__main__':
    main()
   
If done this way, with just the following you could save the button list to a json file add the new method to the class :

import json

    # Add to class body:
    def save_form(self):
        with open('Form.json', 'w') as fj:
            json.dump(self.ckbtns, fj)
reading the dictionary back in is easy, just:
    def load_dict(self):
        with open('Form.json', 'w') as fj:
            self.ckbtns = json.load(fj)
You would then have to iterate through the dictionary and execute a pack on each buton
Reply
#5
Quote:When i re-open the GUI, there is a new, unchecked list of buttons though.
That is because you were creating a new class for every entry in the file.
There should only be a single class, as in the code above.

See the post just above which works.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 954 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 877 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,003 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,072 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  My Code isn't working... End3r 4 1,922 Mar-21-2022, 10:12 AM
Last Post: End3r
  Errors when trying to disable tkinter checkbutton rrick_88 7 2,838 Feb-17-2022, 10:30 PM
Last Post: deanhystad
  I don't undestand why my code isn't working. RuyCab 2 1,980 Jun-17-2021, 03:06 PM
Last Post: RuyCab
  code is not working , can anybody help? RandomPerson69 4 2,901 Mar-22-2021, 04:24 PM
Last Post: deanhystad
  Short code for EventGhost not working Patricia 8 3,668 Feb-09-2021, 07:49 PM
Last Post: Patricia
  Code no longer working yk303 14 10,103 Dec-21-2020, 10:58 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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