我想创建一个草稿游戏板。 起初,当我在“板”画布上直接绘制正方形和棋子时,一切正常。由于我之后必须操纵这些棋子,我希望通过创建每个方块和棋子作为对象来更明确。 所以我试图创建一个包含黑色或白色方块的棋盘,其中包含圆圈(棋子)。然后一切都搞砸了。我不知道为什么。即便如此,它看起来也很合乎逻辑(对我而言)。
我认为这与使用pack方法有关。
以下是代码:
from tkinter import *
class Parent(Tk):
def getRoot(self):
return(self.body)
def setTitle(self,title):
self.title(title)
def run(self):
self.mainloop()
class Drawing:
def __init__(self,root,width,height):
self.zone=Canvas(root, width=width, height=height)
def put(self,root,row,col):
self.zone.grid(root,row=row,column=col)
def getDrawing(self):
return(self.zone)
def rectangle(self,coordX1,coordY1,coordX2,coordY2,color):
self.zone.create_rectangle(coordX1,coordY1,coordX2,coordY2,fill=color, outline="black")
def circle(self,coordX1,coordY1,coordX2,coordY2,color):
self.zone.create_oval(coordX1,coordY1,coordX2,coordY2,fill=color,outline="black")
if __name__=="__main__":
root=Parent()
root.setTitle("Draughts")
board=Drawing(root,400,400)
size=40
logicBoard=[[0, -1, 0, -1, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, 0, -1, 0, -1, 0, -1, 0],
[0, -1, 0, -1, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, 0, -1, 0, -1, 0, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]]
DIMENSION=10
for i in range(DIMENSION):
for j in range(DIMENSION):
coordX1 = (i * size)
coordY1 = (j * size)
coordX2 = coordX1 + size
coordY2 = coordY1 + size
if(not(i%2==j%2)):#if the square is black (on the board)
color="black"
else:
color="white"
case=Drawing(board.getDrawing(),40,40)
case.rectangle(coordX1,coordY1,coordX2,coordY2,color)
case.getDrawing().pack()
if(logicBoard[i][j]>0):
pawnColor="white"
elif(logicBoard[i][j]<0):
pawnColor="black"
if (not(i%2==j%2)):
pawn=Drawing(case.getDrawing(),40,40)
pawn.circle(0,0,30,30,pawnColor)
pawn.getDrawing().pack()
board.getDrawing().pack()
root.run()
谢谢!
编辑:
这就是我得到的:
答案 0 :(得分:2)
问题是你在每次迭代中创建一个新的Canvas
并在其上调用pack()
,而不是使用你在开始时创建的那个。最后,您使用课程Drawing
创建新的Drawings
。我建议你只使用一个类代表所有的棋盘,用方法绘制正方形和圆圈。
我已经改变了椭圆的颜色以获得更好的对比度:
from tkinter import Tk, Canvas
from itertools import product
class Board(Tk):
def __init__(self, width, height, cellsize):
Tk.__init__(self)
self.cellsize = cellsize
self.canvas = Canvas(self, width=width, height=height)
self.canvas.bind("<Button-1>", self.onclick)
self.canvas.pack()
def draw_rectangle(self, x1, y1, x2, y2, color):
self.canvas.create_rectangle(x1, y1, x2, y2, fill=color, outline="black")
def draw_circle(self, x1, y1, x2, y2, color):
self.canvas.create_oval(x1, y1, x2, y2, fill=color, outline="black")
def onclick(self, event):
i = int(event.x / self.cellsize)
j = int(event.y / self.cellsize)
print "You clicked on cell (%s, %s)" % (i, j)
if __name__=="__main__":
size = 40
board = Board(400, 400, size)
board.title("Draughts")
logicBoard = [[0, -1, 0, -1, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, 0, -1, 0, -1, 0, -1, 0],
[0, -1, 0, -1, 0, -1, 0, -1, 0, -1],
[-1, 0, -1, 0, -1, 0, -1, 0, -1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 0, 1, 0, 1, 0, 1, 0, 1],
[1, 0, 1, 0, 1, 0, 1, 0, 1, 0]]
for (i, j) in product(range(10), range(10)):
coordX1 = (i * size)
coordY1 = (j * size)
coordX2 = coordX1 + size
coordY2 = coordY1 + size
color = "white" if i%2 == j%2 else "black"
board.draw_rectangle(coordX1, coordY1, coordX2, coordY2, color)
cell = logicBoard[i][j]
if cell != 0:
pawnColor = "red" if cell > 0 else "blue"
board.draw_circle(coordX1, coordY1, coordX2, coordY2, pawnColor)
board.mainloop()
修改强>
如果要跟踪点击次数,将处理函数绑定到画布并计算点数会更清晰,更简单,而不是使用自己的事件处理程序创建大量画布。