簡體   English   中英

Tkinter-如何將2D顏色數組顯示為圖像?

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

我對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)

生成以下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']]

我想在tkinter中表示,每個數組項都是給定顏色的25像素乘25像素的單元格,並且所有單元格無縫流動,沒有可見的邊界或間隙-它看起來應該像這樣:

它應該看起來像什么

我能夠找到的唯一解決方案是文本字段,而不是顏色。 在tkinter中有什么方法可以執行此操作,還是我需要使用更復雜的GUI框架?

一種方法是通過在tkinter.Canvas對象上創建以網格形式排列的相應矩形。 除了相對簡單之外,您還可以根據需要選擇單個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()

顯示:

腳本輸出的屏幕截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM