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

Python Tkinter-如何将2d颜色数组显示为图像?,python,arrays,tkinter,Python,Arrays,Tkinter,我对Python有些陌生,对Tkinter也完全陌生。我有以下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:

我对Python有些陌生,对Tkinter也完全陌生。我有以下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)
这将生成以下二维颜色数组:

[['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']]
我想用tkinter表示,每个数组项都是给定颜色的25像素乘25像素的单元,所有单元无缝地流动在一起,没有可见的边界或间隙-它应该看起来像这样:


我能找到的唯一解决方案是文本字段,而不是颜色。在tkinter中有什么方法可以做到这一点,或者我需要使用更复杂的GUI框架吗?

一种方法是在
tkinter.Canvas
对象上创建网格中排列的相应矩形。除了相对简单之外,如果您愿意,还可以将其设置为可以选择单独的
Tile
s

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()
显示:


一种方法是在
tkinter.Canvas
对象上创建网格中排列的相应矩形。除了相对简单之外,如果您愿意,还可以将其设置为可以选择单独的
Tile
s

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()
显示: