1

I'm having a problem to load the checkbox state from a text file that contain '0' and '1'.

inside "test.txt" file :

1
0
1
0

This is what I'm expecting the outcome to be as '1' represent checked box and '0' represent unchecked box

enter image description here

Below is the code I'm working on :

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("180x90")
name1 = ["Mike", "Harry", "Siti", "Jenn"]

def loadstates():
    f = open("test.txt", "r")
    list_a = []
    list_a = f.readlines()
    return list_a
    f.close()

def createCheckboxes():
    for x, y in zip(st, name1):
        check = ttk.Checkbutton(root, text=y, variable=x)
        if x=='0':
            check.select()
        else:
            check.deselect()
        check.pack(anchor=tk.W)

st = loadstates()
createCheckboxes()
root.mainloop()

But it gives out error :

Traceback (most recent call last): File "C:/Users/jmamuham/PycharmProjects/LogBook/load_state.py", line 24, in createCheckboxes() File "C:/Users/jmamuham/PycharmProjects/LogBook/load_state.py", line 20, in createCheckboxes check.deselect() AttributeError: 'Checkbutton' object has no attribute 'deselect'

Any idea why .select() and .deselect() gives me this error?

By the way, am I using the correct approach to repopulate the checkbox state using 1 and 0?

stovfl
  • 14,998
  • 7
  • 24
  • 51
zwair
  • 25
  • 1
  • 6
  • `f.close()` after `return` will be nevere executed. – furas Jan 24 '20 at 07:49
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Jan 24 '20 at 07:49
  • 2
    `ttk.Checkbutton` doesn't have `select()` and `deselect()`. But `tkinter.Checkbutton()` has `select()` and `deselect()`. – furas Jan 24 '20 at 07:54
  • @furas Noted, I've edited the full error message. Thanks for the input on ttk.Checkbutton() and tk.checkbutton(). Apparently the error comes from this. – zwair Jan 24 '20 at 08:05
  • ***"variable=x"***: This will not work, read up on [Checkbutton option `variable=`](http://effbot.org/tkinterbook/checkbutton.htm#Tkinter.Checkbutton.config-method) – stovfl Jan 24 '20 at 08:47

2 Answers2

1

There's an even easier way than select() and deselect()! If you properly link a checkbutton to a tkinter int or boolean variable, the checkbutton will automatically check and uncheck if it's given 1/True or 0/False values, respectively. Here's how:

import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("180x90")
name1 = ["Mike", "Harry", "Siti", "Jenn"]

def loadstates():
    f = open("test.txt", "r")
    list_a = []
    list_a = f.readlines()
    f.close()
    return [int(i) for i in list_a] # Make sure your values are integers, not strings

def createCheckboxes():
    for value, y in zip(st, name1):
        x = tk.IntVar() # This is a tkinter variable. BooleanVar() will also work here
        x.set(value) # When modifying values of a tkinter variable, always use .set()
        check = ttk.Checkbutton(root, text=y, variable=x)
        check.var = x # Link the variable to the checkbutton so it isn't thrown out by garbage collection
        check.pack(anchor=tk.W)

st = loadstates()
createCheckboxes()
root.mainloop()
schwartz721
  • 767
  • 7
  • 19
  • `f.close()` after `return` will be nevere executed. – furas Jan 24 '20 at 07:49
  • thanks @schwartz721 . By the way, why we need the `check.var = x` when `ttk.Checkbutton` already have the `variable = x` ? – zwair Jan 26 '20 at 03:04
  • 1
    @zwair it has to do with pythons automatic garbage collection. If python thinks a variable isn’t being used, it will erase that memory space and forget about the variable. While it’s obvious to us that the variable is still in use (cuz it’s linked to the Checkbutton), it isn’t obvious to python. Saving the variable as an attribute of an object that python knows to keep makes the variable worth keeping. Here’s a link to the question I asked about this exact topic last week: https://stackoverflow.com/questions/59797094/tkinter-checkbutton-not-updating-when-changing-variable – schwartz721 Jan 26 '20 at 10:19
0
import tkinter as tk
root = tk.Tk()
root.geometry("180x90")
name1 = ["Mike", "Harry", "Siti", "Jenn"]

def loadstates():
    f = open("test.txt", "r")
    list_a = []
    list_a = f.readlines()
    f.close()
    return list_a

def createCheckboxes():
    for x, y in zip(st, name1):
        check = tk.Checkbutton(root, text=y, variable=x)
        if x.strip()=='0':
            check.select()
        else:
            check.deselect()
        check.pack(anchor=tk.W)

st = loadstates()
createCheckboxes()
root.mainloop()

Use tk.Checkbutton

Use x.strip()=='0'

ganit44
  • 517
  • 1
  • 4
  • 16