Save changed textbutton state back to dictionary value list index 0

非 Y 不嫁゛ 提交于 2021-01-29 10:11:58

问题


Good Day.

I have managed to load a dictionary from a text file with this code:

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

And I can create a tkinter UI with checkboxes that are on or off according to the dictionary values using the following code:

def makeUI(data_folder, critDict):
    top = Tk()
    varList=[]
    for key, val in critDict.items():
        myVar = IntVar()#create intVar for this checkbutton
        varList.append(myVar) #append new intvar to a list
        key  = Checkbutton(top, text = key, command = update) #create new checkbutton with text
        if val[0] == 1:
            key.select()#turn on checkbutton if the dict value is 1
        key.pack()
        T = Text(top, height=2, width=30)#create text box
        T.pack()
        T.insert(END, val[1])#fill text box with comments from dict
        text = T.get("1.0",'end-1c')#get text from text box


    button=Button(top, text="Update", command=save)
    button.pack()

    top.mainloop()

the code running the functions is:

data_folder = "C:\\Users\\NB\\Desktop\\checkbuttonTest\\"
critDict = loadDictFile(data_folder)
makeUI(data_folder, critDict)

The dictionary text file critDict3.txt contains the following string:

{
  "crit2": [
    0,
    "comments2"
  ],
  "crit3": [
    1,
    "comments3"
  ],
  "crit1": [
    1,
    "comments"
  ],
  "crit4": [
    1,
    "comments4"
  ]
}

This all seems to work fine.

But I am having a great deal of difficulty figuring out how to take the values of any changed checkbuttons (buttons whose state has been changed by the user) and save them back to the dictionary so the changed values can be written back out to the text file.

I think my problem is that, since I created the checkbuttons with a for loop, I'm not sure how to find the variable name of each intVar to get the check button state of the right check button to put into the right dictionary value list...if that makes sense.

If anyone can point me in the right direction, I would be most appreciative.


回答1:


Your buttons have the same text attribute as the keys in your dictionary, which is very useful. When you declare a tkinter variable and link it to a widget, you can store it in the widget's .var attribute. Later, you can get the widget's text using widget.cget('text') to link the checkbutton and variable to each dictionary key.

def makeUI(data_folder, critDict):
    top = Tk()
    checkbuttons = []
    for key, val in critDict.items():
        myVar = IntVar() # create intVar for this checkbutton
        key  = Checkbutton(top, text=key, variable=myVar) #create new checkbutton with text
        checkbuttons.append(key)
        key.var = myVar
        key.var.set(val[0])
        key.pack()
        T = Text(top, height=2, width=30)#create text box
        T.pack()
        T.insert(END, val[1])#fill text box with comments from dict
        text = T.get("1.0",'end-1c')#get text from text box


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

    top.mainloop()

Now if you want to access the variable associated with a dictionary key (and it's checkbutton), you can call key.var.get(), and then save the value to the appropriate dictionary key. Save could look something like this.

def save(critDict, checkbuttons):
    for each in checkbutton:
        key = each.cget('text')
        critDict[key] = each.var.get()


来源:https://stackoverflow.com/questions/59889157/save-changed-textbutton-state-back-to-dictionary-value-list-index-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!