python - 如何在 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)
我希望船被放置在按钮的顶部,而不是它们被放置在按钮的下方,并带回初始菜单屏幕
解决方案
推荐阅读
- python - 运行时进度条不更新
- node.js - LARAVEL - “npm run watch”中缺少 npm 依赖项(true-case-path)
- database - UnicodeEncodeError:“charmap”编解码器无法在位置 36 编码字符“\ufffd”:字符映射到
- maven - 从另一个 pom 导入 maven 属性
- azure - AzureDevops Pipeline 并未因某些重要警告而失败
- xamarin - 嵌套网格 TapGestureRecognizer 不工作?
- algorithm - 生成满足条件的表格(比赛队伍分配)
- flask - 从浏览器调用时未创建芹菜任务,但仅在直接从 python 调用时创建
- model - 火炬服务预测模型调用问题
- browser - 浏览器如何处理二进制数据?