0

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!

1
  • You can save the two IntVar to the corresponding dictionary by replacing Checked and Booked by row['Checked'] and row['Booked'] respectively. Commented May 7, 2020 at 9:27

1 Answer 1

1

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!

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

4 Comments

This code will throw an error since key is undefined. You should change it to d["Seat"] = 2
Why are you changing every element in the dictionary? Your example seems more complex than it needs to be.
Your code will still throw an error. It will complain that seat is not defined. Why aren't you just doing d["Seat"] = 2?
@BryanOakley I forgot the quotations sorry

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.