我对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像素单元格,并且所有单元格无缝地流动在一起,没有可见的边框或间隙 - 它应该看起来像这样:
Image of what it should look like
我能找到的唯一解决方案涉及文本字段,而不是颜色。有什么方法可以在tkinter中执行此操作,还是需要使用更复杂的GUI框架?
答案 0 :(得分:2)
您可以通过在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()
显示: