首页 > 解决方案 > 如何在 Python TKinter 中修复 '.!frame4.!canvas'

问题描述

我正在使用 Pythons Tkinter 为学校项目制作战舰游戏,我制作了这些课程让玩家放置他们的船。这个想法是,您可以单击侧面区域中的一艘船,然后单击板上的按钮来放置船。问题是,按钮放置在对角线上,点击它们会返回初始菜单屏幕,这很奇怪

这是我第一次与 Tk 合作,所以我什至不知道从哪里开始

class ShipCreator:
    global board, BOARD_OFFSET
    ShipHolder = Frame(board)
    ShipHolder.place(x=800, y=BOARD_OFFSET)
    shipNum = 5
    activeShip = None

    def __init__(self, length, image, direction):
        # Length is the length integer, image an image, direction is 0 for facing up/down, 1 for facing left/right
        self.length = length
        self.imageTk = ImageTk.PhotoImage(image)
        self.direction = direction
        self.button = Button(ShipCreator.ShipHolder, image=self.imageTk, command=self.start_place)
        self.button.pack()

    def start_place(self):
        actionLabel.configure(text="Click where you'd like the bottom/left of the ship to be:")
        ShipCreator.activeShip = self

    def check_place(self, xcoord, ycoord):
        import Main
        can_place = True
        for i in range(self.length + 1):
            if self.direction == 0 and xcoord < len(Main.playerBoard):
                if ycoord + i < len(Main.playerBoard[xcoord]) and Main.playerBoard[xcoord][ycoord + i] != " ":
                    can_place = False
                    break
            elif ycoord < len(Main.playerBoard[xcoord + self.length]):
                if xcoord + i < len(Main.playerBoard) and Main.playerBoard[xcoord + i][ycoord] == " ":
                    can_place = False
                    break
        return can_place

    def place(self, xcoord, ycoord):
        import Main
        global playerBoard
        if self.check_place(xcoord, ycoord):
            playerBoard.create_image(78 * xcoord + 8, 78 * xcoord + 8 + BOARD_OFFSET, image=self.imageTk, anchor=NW)
            for i in range(self.length + 1):
                if self.direction == 0:
                    Main.playerBoard[xcoord][ycoord + i] = "S"
                else:
                    Main.playerBoard[xcoord + i][ycoord] = "S"
            del self.button
            ShipCreator.shipNum -= 1
            if ShipCreator.shipNum == 0:
                for i in range(10):
                    for j in range(10):
                        exec("del button" + str(i) + str(j))
                del ship1, ship2, ship3, ship4, ship5
                # TODO: Start actual game flow here
        else:
            ShipCreator.activeShip = None
            actionLabel.configure(text="Cannot place a ship there!")

class PlaceButton:
    def __init__(self, xcoord, ycoord):
        global BOARD_OFFSET
        self.xcoord = xcoord
        self.ycoord = ycoord
        self.button = Button(playerBoard, image=backgroundImageTK, command=self.place)
        self.button.place(x=78 * xcoord + 8, y=78 * xcoord + 8 + BOARD_OFFSET, anchor=NW, height=78, width=78)

    def place(self):
        if ShipCreator.activeShip is not None:
            ShipCreator.activeShip.place(self.xcoord, self.ycoord)

def open_game():  # Initializes the game, making the grid buttons for the players ships
    global shipImg1, shipImg2, shipImg3, shipImg4, shipImg5, ship1, ship2, ship3, ship4, ship5
    menu.pack_forget()
    board.pack(expand=True, fill=BOTH)
    for i in range(10):
        for j in range(10):
            exec("button" + str(i) + str(j) + " = PlaceButton(" + str(i + 1) + "," + str(j + 1) + ")")
    ship1 = ShipCreator(5, shipImg1, 0)
    ship2 = ShipCreator(4, shipImg2, 0)
    ship3 = ShipCreator(3, shipImg3, 0)
    ship4 = ShipCreator(4, shipImg4, 1)
    ship5 = ShipCreator(5, shipImg5, 1)

我希望船被放置在按钮的顶部,而不是它们被放置在按钮的下方,并带回初始菜单屏幕

标签: pythontkinter

解决方案


推荐阅读