[英]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 我想创建一个图像,使一个绿色框(其中有一个1)和一个红色框(其中有一个0)成为
PS 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. 使用Tkinter,我将所有盒子都放在一行中,但是我需要像2D数组那样将它们排列成行和列。 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()
You should use .grid()
layout manager 您应该使用
.grid()
布局管理器
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 还有
padx
和pady
用于填充
Comment if something can be improved. 评论是否可以改进。
Tkinter grid() Method:
Tkinter grid()方法:
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: 结果:
Note that the widgets are centered in their cells.
请注意,小部件在其单元格中居中。 You can use the sticky option to change this;
您可以使用sticky选项更改此设置。 this option takes one or more values from the set N, S, E, W.
此选项从N,S,E,W集合中选取一个或多个值。
example:To align the labels to the left border, you could use W (west) 例如:要将标签与左边框对齐,可以使用W(西)
in your case: 在您的情况下:
w = tk.Label(root, text="red", bg="red", fg="white").grid(row=i, column=j, sticky=(W, E))
result: 结果:
Use grid()
layout instead of pack()
, you can also use place()
layout but in that you've to do a lot of work. 使用
grid()
布局而不是pack()
,您也可以使用place()
布局,但是您需要做很多工作。 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. 同样,标签的大小将有所不同,并且不会填充整个网格,请使用
sticky = 'nsew'
,这将拉伸Label
来填充单元。
Here is the code 这是代码
I used enumerate()
function it returns the index of the of the array. 我使用了
enumerate()
函数,它返回数组的索引。
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. 例如,在开头创建一个
list
,然后在循环中对每个Label
执行list.append(L)
,以供将来参考。
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: 输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.