0

I am creating Tkinter checkbuttons from a list of text. I want to set checkbutton enabled by default in some cases. I assign a variable and store the variable in a list to be used later to check the state of checkbutton.

However! The variable doesn't seem to be assigned to the checkbutton at all..! I don't know where is the problem.

This is the gui list: (item, text, state)

[('checkbox', 'Option 1', 1), ('checkbox', 'Option 2', 0), ('checkbox', 'Option 3', 1)]

And: var = ['' for i in range (len(gui))]

This is the code:

for i in range(len(gui)):
    if (gui[i][0] == 'checkbox'):
        var[i] = tk.IntVar(value=int(gui[i][2])
        created_gui[i] = tk.Checkbutton(window, text=gui[i][1], variable=var[i], command=stateChanged) 
        created_gui[i].pack(anchor = "w")

GUI is created like this

I want to set the checkbutton enabled by default in case gui[i][2] == 1 but it doesn't want to work!

Full Code:

import tkinter as tk
from tkinter import filedialog
import os

def stateChanged():
    pass

my_filetypes = [('all files', '.*'), ('text files', '.txt')]
filePath = filedialog.askopenfilename(initialdir=os.getcwd(), title="Please select a file:", filetypes=my_filetypes)
file = open(filePath, 'r')
lines = file.readlines()
gui = []

for line in lines:
    firstChar = line[0]
    text = line.strip()

    if (firstChar == '/'):
        if (text[-4:].lower() == 'true'):
            default = 1
            text = text[1:-4]
        
        elif (text[-5:].lower() == 'false'):
            default = 0
            text = text[1:-5]
        gui.append(('checkbox', text, default)) #Add checkbox to GUI
   
intVars = []
checkBtns = []
window = tk.Tk()

for i in gui:
    intVars.append(tk.IntVar(value=i[2]))
    created_gui = tk.Checkbutton(window, text=i[1], variable=intVars[-1], command=stateChanged) 
    created_gui.pack(anchor = "w")
    checkBtns.append(created_gui)

window.mainloop()

Full Code Result

1 Answer 1

1

you don't need len() and range() to loop through the list instead use in. To append to a list use .append

sample code:

import tkinter as tk

def stateChanged():
    pass

window = tk.Tk()
gui = [('checkbox', 'Option 1', 1), ('checkbox', 'Option 2', 0), ('checkbox', 'Option 3', 1)]


intVars = []
checkBtns = []

for i in gui:

    intVars.append(tk.IntVar(master=window, value=i[2]))
    created_gui = tk.Checkbutton(window, text=i[1], variable=intVars[-1], command=stateChanged) 
    created_gui.pack(anchor = "w")
    checkBtns.append(created_gui)

window.mainloop()

Sign up to request clarification or add additional context in comments.

10 Comments

@CoolCloud Sorry. I thought that the changes were minor (just tried to improve readability and make the code more stable). I didn't even know that OP had more than 1 Tk() objects.
@JacksonPro You might want to add master=window to the IntVar() because that was was part of the solution to OP's problem.
@TheLizzard I have no problem if ppl edit my code as long as they don't do it with bad intention. Also, I can't find two instances of Tk() in OP's code.
@JacksonPro I think that when OP was creating a small example to post here they might have removed the second case of Tk().
@CoolCloud ok I will do that in the future
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.