J.Bikker
J.Bikker

Reputation: 1

Update data using tkinter gui

I'm new to tkinter and I want to build a GUI to update data, using two checkboxes: 'Checked', and 'Booked'. I'm struggling to define the command to update the data.

In reality there are a lot more rows, so I want to show them next to each other, grouped by Catagory

data = [{'Number': 1, 'Catagory': 'A', 'Color':'Red'},
    {'Number': 2, 'Catagory': 'A', 'Color':'Blue'},
    {'Number': 3, 'Catagory': 'B', 'Color':'Blue'},
    {'Number': 4, 'Catagory': 'C', 'Color':'Red'},
    {'Number': 5, 'Catagory': 'C', 'Color':'Yellow'}]

from tkinter import *
root = Tk()

row_a = 1
row_b = 1
row_c = 1

Label(root, text='A').grid(row=0,column=1)
Label(root, text='B').grid(row=0,column=3)
Label(root, text='C').grid(row=0,column=6)

for row in data:
    if row['Catagory'] == 'A':
        Label(root, text=row['Number']).grid(row=row_a,column=0)
        Checked = IntVar()
        Booked = IntVar()
        Checkbutton(root,variable=Checked).grid(row=row_a, column = 1)
        Checkbutton(root,variable=Booked).grid(row=row_a, column = 2)  
        row_a +=1

    elif row['Catagory'] == 'B':
        Label(root, text=row['Number']).grid(row=row_b,column=3)
        Checked = IntVar()
        Booked = IntVar()
        Checkbutton(root,variable=Checked).grid(row=row_b, column = 4)
        Checkbutton(root,variable=Booked).grid(row=row_b, column = 5)
        row_b +=1

    elif row['Catagory'] == 'C':
        Label(root, text=row['Number']).grid(row=row_c,column=6)
        Checked = IntVar()
        Booked = IntVar()
        Checkbutton(root,variable=Checked).grid(row=row_c, column = 7)
        Checkbutton(root,variable=Booked).grid(row=row_c, column = 8)
        row_c +=1


Button(root, text = 'Update data').grid(row=max([row_a,row_b,row_c]),column=0,columnspan=8)    
root.mainloop() 

What need to happen next, is to update the data after clicking the button. So, each item needs to get two new key-value pairs: 'Checked':1,'Booked':1 if both checkboxes are ticked.

Any help would me much appreciated!

Upvotes: 0

Views: 84

Answers (1)

10 Rep
10 Rep

Reputation: 2270

To update a dictionary, you can use this simple code:

d = {"Seat": 1}
print(d)
d["Seat"] = 2#this can be whatever you need it to be. 
print(d)

The output will be:

{'Seat': 1}
{'Seat': 2}

Using this, you can adjust the key of the dictionary when the "update data" button is clicked.

Hopefully this helps!

Upvotes: 1

Related Questions