gpt4 book ai didi

python - 制作 Tkinter Photoimage 使 python 程序崩溃

转载 作者:行者123 更新时间:2023-12-02 22:29:41 25 4
gpt4 key购买 nike

我正在使用 tkinter 编写一个扫雷游戏,一切似乎都正常,但每次点击多次后 python 都会崩溃,我相信这是因为我为用于标记的“标记”添加了图像哪里有矿井。下面是绘制所有单元格的函数,每次移动/单击都会调用该函数。

def draw_cake_cell(canvas, row, col):
# draws different cells on the board (blank cell, numbers, cake flags)
margin = 5
cellSize = 30
board = canvas.data['board']
mask = canvas.data['mask']
cake_count = canvas.data['cake_count']
player_board = canvas.data['player_board']
left = margin + col*cellSize
top = margin + row*cellSize
bottom = top + cellSize
right = left+cellSize
flag_img = PhotoImage(file="flag.gif")
flag_label = Label(image = flag_img)
flag_label.image = flag_img
cake_img = PhotoImage(file = "cakeflag.gif")
cake_label = Label(image = cake_img)
cake_label.image = cake_img
canvas.create_rectangle(left,top, right, bottom, fill = 'gray')
if board[row][col] == -1:
if canvas.data['isGameOver'] == True:
canvas.create_image((left+right)/2,(top+bottom)/2,image = cake_img)
elif mask[row][col] == -2:
canvas.create_image((left+right)/2,(top+bottom)/2,image = flag_img)
else:
if cake_count[row][col] > 0 and player_board[row][col] == 1:
value = cake_count[row][col]
canvas.create_rectangle(left,top,right,bottom, fill = 'white')
canvas.create_text((left+right)/2,(top+bottom)/2 , text = str(value), font = ('Helvetica',12))
if mask[row][col] == -2:
canvas.create_image((left+right)/2,(top+bottom)/2,image = flag_img)
if cake_count[row][col] == 0 and player_board[row][col] == 1:
canvas.create_rectangle(left,top,right,bottom, fill = 'white')

最佳答案

由于您使用的是 python3,因此 (left+right)/2float,而不是 int。也许这就是问题所在 - 使用 (left+right)//2

关于python - 制作 Tkinter Photoimage 使 python 程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16389512/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com