嘿伙计们我有一个简单的Chessboard用python制作Tkinter,我试图在特定的行和列上添加棋子的图像,但画布不断重叠图像。请帮忙!
这是我的董事会代码:
class GameBoard(tk.Frame):
def __init__(self, parent, rows=8, columns=8, size=75, color1="white",
color2="lightgrey"):
'''size is in pixels'''
self.rows = rows
self.columns = columns
self.size = size
self.color1 = color1
self.color2 = color2
self.pieces = {}
canvas_width = columns * size
canvas_height = rows * size
tk.Frame.__init__(self, parent)
self.canvas = tk.Canvas(self, borderwidth=0, highlightthickness=0,
width=canvas_width, height=canvas_height, background="white")
self.canvas.pack(side="top", fill="both", expand=True, padx=2, pady=2)
这是我的图像放置代码,我已将图像放置在600乘600,仅用于测试目的。
self.canvas.WhiteKnight = tk.PhotoImage(file = 'E:\\Final Project +
Report\\Pieces\\WhiteKnight.png')
self.canvas.create_image(600,600, image = self.canvas.WhiteKnight, anchor = 'c')
Here you can see the image showing but being overlapped
非常感谢任何帮助!
答案 0 :(得分:1)
与大多数图形工具一样,放置在画布上的项目具有z-index(或者用tkinter的说法,即堆叠顺序)。默认情况下,项目是"堆叠"按照它们的创建顺序。
您似乎是先创建图像,然后是最后一块板。一个简单的解决方案是颠倒顺序并首先创建板。
如果这不切实际,您可以使用画布的lift
方法将一个或多个部分提升到其他部分之上。
例如:
item_id = self.canvas.create_image(...)
<other canvas objects created here>
self.canvas.lift(item_id)
如果您为商品提供了标签(例如&#34; piece&#34;),您可以使用该标签将所有商品抬高到其他所有商品上。
这是一个完整的示例,使用文本而不是图像来保持示例简单。运行程序,然后单击&#34; lift&#34;按钮,看到图像出现在电路板中间。
import tkinter as tk
image_data = '''
R0lGODlhEAAQAOYAAElJSU5OTlFRUVJSUlNTU1hYWFtbW2FhYWJiYmRkZGtra21t
bW5ubm9vb3FxcXl5eYCAgIGBgYKCgoODg4WFhYeHh4mJiYyMjI+Pj5ycnJ6enqCg
oKGhoaOjo6Wlpaampqenp6ioqKqqqqurq6ysrLCwsLGxsbKysrW1tbe3t7m5ubq6
ury8vL29vb6+vr+/v8DAwMHBwcLCwsPDw8TExMXFxcbGxsfHx8jIyMnJycrKysvL
y8zMzM3Nzc7Ozs/Pz9DQ0NHR0dLS0tTU1NXV1dbW1tjY2NnZ2dvb29zc3N3d3d7e
3uLi4uTk5OXl5efn5+np6e3t7QAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAAAACH5BAkAAFIALAAAAAAQABAAAAfDgFKCgzs3NIOIiVI3Tk0k
ioJBPjpSMktKJ1IkIIhASVFIMi5FQyUkO08piDRJQ0dIpEdCOzgPDohDPDkrGRwy
NjEpFoI4NDBGPSwSghgzMj0XFRM0UEU5Ph6IJDQrNkoKL0xCNj0miCEyKTBCBx0Y
Gz82PBrMMSwqCQUEgiQ1MTU3RICI4QKFCEQjPhCpsSNIjhs8arTYwQARiyUfJlCg
IWMBgw9CIAxA1CCBlAmFEEgpEAAAJCkRWpww8DJRAQEjEwUCADs=
'''
def do_lift():
canvas.lift("piece")
root = tk.Tk()
canvas = tk.Canvas(root, width=400, height=400)
button = tk.Button(root, text="Lift", command=do_lift)
button.pack(side="top")
canvas.pack(side="top", fill="both", expand=True)
# create items before the board
image = tk.PhotoImage(data=image_data)
canvas.create_image(130, 130, image=image, anchor="center", tags=(("piece",)))
canvas.create_image(150, 150, image=image, anchor="center", tags=(("piece",)))
canvas.create_image(170, 170, image=image, anchor="center", tags=(("piece",)))
canvas.create_image(170, 130, image=image, anchor="center", tags=(("piece",)))
canvas.create_image(130, 170, image=image, anchor="center", tags=(("piece",)))
# create the board
color = "black"
for x in range(0, 300, 20):
for y in range(0, 300, 20):
canvas.create_rectangle(x,y,x+20, y+20, fill=color)
color = "bisque" if color == "black" else "black"
root.mainloop()