1

I can select rows with the defaultdict in my tkinter program, but I don't know how to select the entire column. I understand the concept of defaultdict(list), but I don't know how to code to select a column. The defaultdict set up happens to part of a bigger program, so i must use this set up. if someone can help me with selecting the column, it would be much appreciated. With my limited knowledge, I know to access a dictionary value I need to have [][] to access its value, where would I put that in my code? for an example, if I want to put the red color in column[1], how would I do that?

import pulp
import tkinter as tk
from tkinter import ttk
from collections import defaultdict

class Application(ttk.Frame): #inherent from frame.

    def __init__(self, parent):
        tk.Frame.__init__(self, parent, bg="LightBlue4")
        self.parent = parent
        self.pack()
        self.labels_dict = defaultdict(list)
        self.GUI()

    def GUI(self):
        for i in range (9):
            for day in range(7):
                self.label = tk.Label(self, relief="ridge", width=11,     
                height=3)
                self.label.grid(row=i, column=day, sticky='nsew')
                self.labels_dict[i].append(self.label)

        self.button=tk.Button(self, text="Caluculate", bg="salmon")
        self.button.grid(row = 10, column = 6)

        for (i) in self.labels_dict:
            for element in self.labels_dict[1]:
                element.configure(bg = "red")


def main():
    root = tk.Tk()
    root.title("class basic window")
    root.geometry("1200x600")
    root.config(background="LightBlue4")
    app = Application(root)
    root.mainloop()

if __name__ == '__main__':
    main()
fishtang
  • 167
  • 3
  • 9

1 Answers1

1

To access a column, you can loop through the rows and take the n-th element of each row like

for row in self.labels_dict:
    element = self.labels_dict[row][1]
    element.configure(bg = "red")

To access a row, you don't need the double loop you have, you can simply take one row and loop through the elements of that row

for element in self.labels_dict[1]:
    element.configure(bg = "red")
fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
  • thanks for the insight and the correction. I knew I have to have [ ][ ], but never occur to me to do it this way. thanks again. – fishtang Dec 01 '18 at 06:36