Somshree Mukherjee
Somshree Mukherjee

Reputation: 49

Create an image consisting of boxes in 2 colours using python

I have an array

arr = [[1,0,0,1], 
       [1,1,1,1], 
       [1,1,1,0]]

I want to create an image that makes a green box where there is a 1 and a red box where there is a 0

P.S. I do not want to make a plot, but an image.

Using Tkinter, I am getting all the boxes in a single row, but I need it to be arranged in rows and columns like in the 2D array. In my code, I am not able to figure how to move to a new line after printing the boxes corresponding to a row

import tkinter as tk

root = tk.Tk()
arr = [[1,0,0,1], [1,1,1,1], [1,1,1,0]]
for i in range(len(arr)):
    for j in range(len(arr[i])):
        if arr[i][j]==0:
            w = tk.Label(root, text="red", bg="red", fg="white")
            w.pack(padx=5, pady=10)
        elif arr[i][j]==1:
            w = tk.Label(root, text="green", bg="green", fg="black")
            w.pack(padx=5, pady=20)
        w.pack(side=tk.LEFT)
    # need something here to move to a new line after printing each row
tk.mainloop()

Current Output : enter image description here

Upvotes: 1

Views: 599

Answers (3)

Saad
Saad

Reputation: 3430

Use grid() layout instead of pack(), you can also use place() layout but in that you've to do a lot of work. Also the size of the labels will be different and will not fill the whole cell of a grid, to do that use sticky = 'nsew', this will stretch the Label to fill the cell.

Here is the code

I used enumerate() function it returns the index of the of the array.

If you want to have all label's object then you can create a list or dictionary to access each of them separately. For example create a list in the beginning and do list.append(L) in the loop to each Label for future reference.

import tkinter as tk

arr = [[1,0,0,1], 
       [1,1,1,1], 
       [1,1,1,0]]

root = tk.Tk()

for row , ar in enumerate(arr):
    for col, color in enumerate(ar):
        bg = 'green' if color else 'red'
        fg = 'black' if color else 'white'
        L = tk.Label(root, text=bg, bg=bg, fg=fg)
        L.grid(row=row, column=col, sticky='nswe')

root.mainloop()

Output:

Output

Upvotes: 1

ncica
ncica

Reputation: 7206

Tkinter grid() Method:

The Grid geometry manager puts the widgets in a 2-dimensional table. The master widget is split into a number of rows and columns, and each “cell” in the resulting table can hold a widget.

use the grid method to tell the manager in which row and column to place them. You don’t have to specify the size of the grid beforehand; the manager automatically determines that from the widgets in it.

root = tk.Tk()
arr = [[1,0,0,1], [1,1,1,1], [1,1,1,0]]
for i in range(len(arr)):
    for j in range(len(arr[i])):
        if arr[i][j]==0:
            w = tk.Label(root, text="red", bg="red", fg="white").grid(row=i, column=j)
        elif arr[i][j]==1:
            w = tk.Label(root, text="green", bg="green", fg="black").grid(row=i, column=j)
tk.mainloop()

result:

enter image description here

Note that the widgets are centered in their cells. You can use the sticky option to change this; this option takes one or more values from the set N, S, E, W.

example:To align the labels to the left border, you could use W (west)

in your case:

w = tk.Label(root, text="red", bg="red", fg="white").grid(row=i, column=j, sticky=(W, E))

result:

enter image description here

Upvotes: 1

Rahul
Rahul

Reputation: 577

You should use .grid() layout manager

for i in range(len(arr)): #rows in arr
    for j in range(len(arr[i])): #cols in that row
        if arr[i][j]==0:
            w = tk.Label(root, text="red", bg="red", fg="white")
        elif arr[i][j]==1:
            w = tk.Label(root, text="green", bg="green", fg="black")
        w.grid(row = i, col = j)

This puts your label in a "2-Dimensional Table" consisting of rows and columns.
There is also padx and pady for padding
Comment if something can be improved.

Upvotes: 2

Related Questions