简体   繁体   English

Tkinter-如何将2D颜色数组显示为图像?

[英]Tkinter - how to display a 2d array of colors as an image?

I am somewhat new to Python and completely new to Tkinter. 我对Python有点陌生,对Tkinter则完全陌生。 I have the following python code: 我有以下python代码:

class Level:

    def __init__(self, name, height, width):

        self.name = name
        self.height = height
        self.width = width
        self.tiles = makeTiles(height, width)   

    class Tile:

        def __init__(self, x, y, type, color, isOccupied, enemy):

            self.x = x
            self.y = y
            self.type = type
            self.color = color
            self.isOccupied = isOccupied
            self.enemy = enemy

def makeLevel(name, height, width):

    level = Level(name, height, width)

    return level

def makeTiles(height, width):

    tiles = [[Level.Tile(x, y, "Surface", "red", False, "None") for x in range(width)] for y in range(height)]

    return tiles

test1 = makeLevel("test", 10, 10)

for x in range(10):
    for y in range(10):
        if x % 2 == 0 and y % 2 == 0:
            test1.tiles[x][y].color = "black"
        elif x % 2 == 0:
            test1.tiles[x][y].color = "blue"
        elif y % 2 == 0:
            test1.tiles[x][y].color = "yellow"

colorMatrix = [[0 for x in range(10)] for y in range(10)]

for x in range(10):
    for y in range(10):
        colorMatrix[x][y] = test1.tiles[x][y].color

print(colorMatrix)

which generates the following 2d array of colors: 生成以下2d颜色数组:

[['black', 'blue', 'black', 'blue', 'black', 'blue', 'black', 'blue', 'black', 'blue'], ['yellow', 'red', 'yellow', 'red', 'yellow', 'red', 'yellow', 'red', 'yellow', 'red'], ['black', 'blue', 'black', 'blue', 'black', 'blue', 'black', 'blue', 'black', 'blue'], ['yellow', 'red', 'yellow', 'red', 'yellow', 'red', 'yellow', 'red', 'yellow', 'red'], ['black', 'blue', 'black', 'blue', 'black', 'blue', 'black', 'blue', 'black', 'blue'], ['yellow', 'red', 'yellow', 'red', 'yellow', 'red', 'yellow', 'red', 'yellow', 'red'], ['black', 'blue', 'black', 'blue', 'black', 'blue', 'black', 'blue', 'black', 'blue'], ['yellow', 'red', 'yellow', 'red', 'yellow', 'red', 'yellow', 'red', 'yellow', 'red'], ['black', 'blue', 'black', 'blue', 'black', 'blue', 'black', 'blue', 'black', 'blue'], ['yellow', 'red', 'yellow', 'red', 'yellow', 'red', 'yellow', 'red', 'yellow', 'red']]

Which I would like to represent in tkinter with each array item being a 25 pixel by 25 pixel cell of the given color, and all of the cells flowing seamlessly together with no visible borders or gaps - it should look something like this: 我想在tkinter中表示,每个数组项都是给定颜色的25像素乘25像素的单元格,并且所有单元格无缝流动,没有可见的边界或间隙-它看起来应该像这样:

Image of what it should look like 它应该看起来像什么

The only solutions I've been able to find involve text fields, not colors. 我能够找到的唯一解决方案是文本字段,而不是颜色。 Is there any way to do this in tkinter, or do I need to use a more complex GUI framework? 在tkinter中有什么方法可以执行此操作,还是我需要使用更复杂的GUI框架?

One way you could do it by creating corresponding rectangles arranged in a grid on a tkinter.Canvas object. 一种方法是通过在tkinter.Canvas对象上创建以网格形式排列的相应矩形。 Besides being relatively simple, you can make it so individual Tile s can be selected, if you wish. 除了相对简单之外,您还可以根据需要选择单个Tile

try:
    import tkinter as tk  # assume Python 3
except ImportError:
    import Tkinter as tk  # nope, Python 2

class Level:
    def __init__(self, name, height, width):
        self.name = name
        self.height = height
        self.width = width
        self.tiles = makeTiles(height, width)

    class Tile:
        def __init__(self, x, y, type, color, isOccupied, enemy):
            self.x = x
            self.y = y
            self.type = type
            self.color = color
            self.isOccupied = isOccupied
            self.enemy = enemy

def makeLevel(name, height, width):
    level = Level(name, height, width)
    return level

def makeTiles(height, width):
    tiles = [[Level.Tile(x, y, "Surface", "red", False, "None") for x in range(width)] for y in range(height)]
    return tiles

test1 = makeLevel("test", 10, 10)
for x in range(10):
    for y in range(10):
        if x % 2 == 0 and y % 2 == 0:
            test1.tiles[x][y].color = "black"
        elif x % 2 == 0:
            test1.tiles[x][y].color = "blue"
        elif y % 2 == 0:
            test1.tiles[x][y].color = "yellow"

colorMatrix = [[0 for x in range(10)] for y in range(10)]
for x in range(10):
    for y in range(10):
        colorMatrix[x][y] = test1.tiles[x][y].color

#print(colorMatrix)

width, height = 400, 400

root = tk.Tk()
root.title("colorMatrix")

frame = tk.Frame()
frame.pack()

canvas = tk.Canvas(frame, width=width, height=height)
rows, cols = len(colorMatrix), len(colorMatrix[0])

rect_width, rect_height = width // rows, height // cols
for y, row in enumerate(colorMatrix):
    for x, color in enumerate(row):
        x0, y0 = x * rect_width, y * rect_height
        x1, y1 = x0 + rect_width-1, y0 + rect_height-1
        canvas.create_rectangle(x0, y0, x1, y1, fill=color, width=0)
canvas.pack()

root.mainloop()

Display: 显示:

脚本输出的屏幕截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM