Search code examples
pythonfunctiontkintergettextbox

get text from tkinter text box in a different function


Good Day. I have a simple tkinter UI with 4 checkbuttons. Each has an associated text box. I have managed to create the UI by getting the checkbox state and the text in the textbox from a dictionary in a .txt file. I can also write the changed checkbutton state back to the file successfully with the update() function.

PROBLEM: I am having trouble figuring out how to get and write the changed text in the textbox back to the file. I need to get the textbox string for each textbox, but they are local to the makeUI() function. I am not sure how to pass all the textboxes to the update() function.

Suggestions on how to solve this would be most gratefully appreciated.

Below is the code:

from tkinter import *
from tkinter.scrolledtext import ScrolledText
import json

def loadDictFile(data_folder): 
    critDict = json.loads(open(data_folder+'critDict3.txt').read())
    return critDict

def update(critDict, checkbuttons, data_folder):
    for k in checkbuttons:
        key = k.cget('text')
        critDict[key][0] = k.var.get() 
        critDict[key][1] = commentBox.get("1.0",'end-1c') # <--HERE IS THE PROBLEM
    with open(data_folder+'critDict3.txt', 'w') as file:
        file.write(json.dumps(critDict))

def makeUI(data_folder, critDict):
    newDict = critDict
    top = Tk()
    checkbuttons={}
    for key, val in newDict.items():
        chkVar = IntVar()
        key  = Checkbutton(top, text = key, variable=chkVar) 
        key.var = chkVar
        key.var.set(val[0])
        key.pack()

        commentBox = ScrolledText(top, height=2, width=30)
        commentBox.pack()
        commentBox.insert(END, val[1])
        comment = commentBox.get("1.0",'end-1c')
        checkbuttons[key]=comment

    print('checkbuttons is', checkbuttons)

    button=Button(top, text="Update", command=lambda : update(critDict, checkbuttons, data_folder))
    button.pack()

    top.mainloop()


data_folder = "C:\\Users\\NB\\test\\data\\"
critDict = loadDictFile(data_folder)
makeUI(data_folder, critDict)

and here is the text of the disk file 'critDict3.txt

{
"crit1": [1, "comments"], "crit4": [1, "comment4"], "crit2": [1, "comment2"], "crit3": [1, "comments3"]
}

Solution

  • You could create list/dictionary with all chkVar and commentBox

     all_items[key] = [chkVar, commentBox]
    

    and sent to update()

     command=lambda:update(critDict, all_items, data_folder))
    

    and inside update() you can access it

     for key, [chkVar, commentBox]  in all_items.items():
    

    def makeUI(data_folder, critDict):
        top = Tk()
    
        all_items = {}
    
        for key, val in critDict.items():
            chkVar = IntVar(value=val[0])
            cb = Checkbutton(top, text=key, variable=chkVar) 
            cb.pack()
    
            commentBox = ScrolledText(top, height=2, width=30)
            commentBox.pack()
            commentBox.insert('end', val[1])
    
            all_items[key] = [chkVar, commentBox]
    
        button = Button(top, text="Update", command=lambda:update(critDict, all_items, data_folder))
        button.pack()
    
        top.mainloop()
    
    def update(critDict, all_items, data_folder):
    
        for key, [chkVar, commentBox]  in all_items.items():
            critDict[key] = [chkVar.get(), commentBox.get("1.0", 'end-1c')]
    
        with open(data_folder+'critDict3.txt', 'w') as file:
            file.write(json.dumps(critDict))