lundi 20 juillet 2020

Gather input from multiple tkinter checkboxes created by a for loop

I made an application with tkinter which creates a list of checkboxes for some data. The checkboxes are created dynamically depending on the size of the dataset. I want to know of a way to get the input of each specific checkbox.

Here is my code, which you should be able to run. To open the second window of the app press on "Generate list controls"

from tkinter import *
import tkinter as tk
from tkinter import filedialog
from tkinter import scrolledtext
from tkinter import ttk
import queue


class ControlApp:

def __init__(self):
    self.root = tk.Tk()
    self._init_gui()
    self.root.title("Comp1 Clients")

def generate_controls(self):
    # General parameters
    self.new_window = Toplevel()
    # self.new_window.geometry("1500x800")
    self.new_window.title("List of controls")

    # Frame for the buttons
    self.frame_controls = Label(self.new_window, padx=5, pady=5)
    self.frame_controls.grid(row=0, column=0, sticky="nsew")

    # actually generating the control frame:
    self.headers = ["Control objective", "Comp1 Control", "Comp2 Test", "General Control", "In Scope?"]
    self.height = 21
    self.width= 5

    for j in range(self.width):
        self.label = Label(self.new_window, text=self.headers[j], foreground="black")
        self.label.grid(row=0, column=j, sticky="nsew", pady=1, padx=1)
        self.new_window.grid_columnconfigure(j, weight=1)

    for i in range(1, self.height):
        for j in range(self.width - 2):
            self.label = Label(self.new_window, text="Row: " + str(i) + ", Column: " + str(j+1), background="black",
                          foreground="white")
            self.label.grid(row=i, column=j, sticky="nsew", pady=1, padx=1)

            self.new_window.grid_columnconfigure(j, weight=1)

    for i in range(1, self.height):
        # create checkboxes for each row in the "general" column
        self.placeholder_check_gen = Checkbutton(self.new_window)
        self.placeholder_check_gen.grid(row=i, column=3, sticky="nsew", pady=1, padx=1)

    for i in range(1, self.height):
        # create checkboxes for each row in the "scope" column
        self.placeholder_scope = Checkbutton(self.new_window)
        self.placeholder_scope.grid(row=i, column=4, sticky="nsew", pady=1, padx=1)

def _init_gui(self):
    # Frame for parameters
    self.frame_parameters = LabelFrame(self.root, text="Parameters", padx=5, pady=5, width=250)
    self.frame_parameters.grid(row=0, column=0, padx=10, pady=10)

    # Frame for buttons
    self.frame_buttons = LabelFrame(self.root, padx=5, pady=5)
    self.frame_buttons.grid(row=1, column=0, padx=10, pady=10)

    self.clients = StringVar()
    self.clients.set("Client")
    self.drop_client = OptionMenu(self.frame_parameters, self.clients, "C1", "c2", "c3", "c4", "c5")
    self.drop_client.grid(row=1, column=1)

    self.reports = StringVar()
    self.drop_report = OptionMenu(self.frame_parameters, self.reports, "3000", "3402", "ISO", "GDPR")
    self.reports.set("Report Type")
    self.drop_report.grid(row=1, column=2)

    # Technology dropdown
    self.item0 = IntVar()
    self.item1 = IntVar()
    self.item2 = IntVar()
    self.item3 = IntVar()
    self.item4 = IntVar()
    self.item5 = IntVar()

    self.text_var = Menubutton(self.frame_parameters, text="Choose technology", relief=RAISED)
    self.text_var.grid(row=0, column=2)
    self.text_var.menu = Menu(self.text_var)
    self.text_var["menu"] = self.text_var.menu

    self.text_var.menu.add_checkbutton(label="Windows", variable=self.item0)
    self.text_var.menu.add_checkbutton(label="UNIX", variable=self.item1)
    self.text_var.menu.add_checkbutton(label="MS-SQL", variable=self.item2)
    self.text_var.menu.add_checkbutton(label="Oracle", variable=self.item3)
    self.text_var.menu.add_checkbutton(label="AS-400", variable=self.item4)
    self.text_var.menu.add_checkbutton(label="DB2", variable=self.item5)
    self.text_var.grid(row=1, column=4)

    # The generate controls button
    self.control_button = tk.Button(self.frame_buttons, text="Generate list controls",
                                    command=self.generate_controls)
    self.control_button.grid(row=0, column=1)

def run(self):
    self.root.update()
    self.root.minsize(self.root.winfo_width(), self.root.winfo_height())
    self.root.mainloop()


if __name__ == '__main__':
    app = ControlApp()
    app.run()

I looked over other answers and some people got away by defining a variable inside the checkbox settings "variable=x" and then calling that variable with a "show():" function that would have "variable.get()" inside. If anyone could please point me in the right direction or how I could proceed here. Thank you and much appreciated.




Aucun commentaire:

Enregistrer un commentaire