Reputation: 193
here is a section of a tkinter app I am working on, and I need to take an input of a list, and use that to create a window with an entry and checkbox for every item in the list. When the checkbox is clicked, I want to fill in the corresponding entry with a default value. However, the code I've written seems to think that all of the checkboxes correspond to the last entry widget. I need help iterating through the list and generating widgets that I can access in an outside function for every item in the list. Here is my code, using python 3.8.6 with IDLE:
import tkinter as tk
from tkinter import StringVar, IntVar
groups = ['A', 'B', 'C']
def update_var(item, e_dict, cb_val):
print(item)
print(e_dict[item].get())
print(cb_val[item].get())
if cb_val[item].get() == 1:
e_dict[item].set('CHECKBOX IS CLICKED')
def submit():
pass
class App(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.container = tk.Frame(self)
self.container.pack(side='top', fill='both', expand=True)
self.container.grid_rowconfigure(0, weight=1)
self.container.grid_columnconfigure(0, weight=1)
self.show_frame(MainWindow)
def show_frame(self, cont):
frame = cont(self.container, self)
frame.grid(row=0, column=0, sticky='nsew')
frame.tkraise()
class MainWindow(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
global group_dict
tk.Frame.__init__(self, parent, *args, **kwargs)
container = tk.Frame()
canvas = tk.Canvas(container)
scrollbar = tk.Scrollbar(container, orient='vertical', command=canvas.yview)
scrollable_frame = tk.Frame(canvas)
scrollable_frame.bind(
'<Configure>',
lambda e: canvas.configure(
scrollregion=canvas.bbox('all')
)
)
canvas.create_window((0,0), window=scrollable_frame, anchor='nw')
canvas.configure(yscrollcommand=scrollbar.set)
label1 = tk.Label(scrollable_frame, text='SAMPLE PROGRAM')
label1.config(font=('Arial', 20))
label1.grid(row=0, column=0, columnspan=3, padx=2, pady=2)
label2 = tk.Label(scrollable_frame, text='GROUP', width=13)
label2.config(font=('Arial', 15))
label2.grid(row=1, column=0, padx=2, pady=2)
label3 = tk.Label(scrollable_frame, text='INPUT', width=13)
label3.config(font=('Arial', 15))
label3.grid(row=1, column=1, padx=2, pady=2)
label4 = tk.Label(scrollable_frame, text='CHECKBOX', width=13)
label4.config(font=('Arial', 15))
label4.grid(row=1, column=2, padx=2, pady=2)
e_dict = {}
cb_val = {}
num_lines = 0
group_list = []
for i, item in enumerate(groups):
tk.Label(scrollable_frame, text=item, width=20).grid(row=i+2, column=0, padx=2, pady=2)
e_dict[item] = StringVar()
tk.Entry(scrollable_frame, textvariable=e_dict[item]).grid(row=i+2, column=1, padx=2, pady=2)
#group_list.append(key)
cb_val[item] = IntVar()
tk.Checkbutton(scrollable_frame, variable = cb_val[item], command=lambda: update_var(item, e_dict, cb_val)).grid(row=i+2, column=2, padx=2, pady=2)
num_lines += 2
submit_btn = tk.Button(scrollable_frame, text='SUBMIT', width=20, command = lambda: submit())
submit_btn.config(font=('Arial', 12))
submit_btn.grid(row=num_lines+1, column=1, columnspan=2)
container.place(x=0, y=0, width=490, height=640)
scrollbar.pack(side="right", fill="y")
canvas.pack(side='left', fill='both', expand=True)
if __name__ == '__main__':
root = App()
root.mainloop()
Upvotes: 0
Views: 16