0

这是我正在开发的 tkinter 应用程序的一部分,我需要输入一个列表,并使用它来创建一个窗口,其中包含列表中每个项目的条目和复选框。单击复选框时,我想用默认值填写相应的条目。但是,我编写的代码似乎认为所有复选框都对应于最后一个条目小部件。我需要帮助遍历列表并生成可以在外部函数中为列表中的每个项目访问的小部件。这是我的代码,使用带有 IDLE 的 python 3.8.6:

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()
4

0 回答 0