Tkinter按钮出现在旧按钮后面,而不是前面

2024-10-01 13:35:29 发布

您现在位置:Python中文网/ 问答频道 /正文

背景:我正在制作一个战舰游戏,当用户点击AI战舰时,我想将按钮从默认颜色改为“爆炸”。PNG

现在问题来了。我有这个按钮网格,我想知道如何将新按钮放在旧按钮前面?它适用于一个按钮网格,但不适用于一个

更具体地说,在AI水下和玩家水下This image should help.

这是更新按钮的代码

def draw_new_button_hit(row, column):


    global redraw_gameboard
    global Player
    global AI_player
    global AI_surface_cell
    global player_underwater_cell

    script_dir = os.path.dirname(__file__)
    rel_path = "explode.png"
    image = Image.open(os.path.join(script_dir, rel_path))
    image = image.resize((50,50), Image.ANTIALIAS)

    imtk = ImageTk.PhotoImage(image, master = redraw_gameboard)

    new_button = Button(redraw_gameboard,
                        image=imtk,
                        height = 20+10, 
                        width = 20+16,
                        command= already_shot, state = DISABLED)

    new_button.image = imtk

    new_button.lift()
    new_button.grid(row = row, column = column)

这对AI&;玩家界面。但不是水下滑板

这是创建我的板的代码

import tkinter as tk 
from tkinter import *
from tkinter import messagebox 
import os 
from random import randint
import random
from PIL import Image, ImageTk

Player = {'Carrier':[(1, 11, 1), (1, 12, 1), (1, 13, 1), (1, 14, 1)], 'Submarine': [(1, 0, 0), (1, 1, 0), (1, 2, 0)]}
AI_player = {'Carrier':[(12,12,1), (12,13,1), (12,14,1), (12,15,1)], 'Submarine': [(12,6,0), (12,7,0), (12,8,0)]}


button_height = 2
button_width = 4

def redraw_boards():
    global redraw_gameboard
    global Player
    global AI_player

    Player["Player Surface"] = {}
    Player["Player Underwater"] = {}
    AI_player["AI Surface"] = {}
    AI_player["AI Underwater"] = {}

    redraw_gameboard = tk.Tk()
    redraw_gameboard.title("Battleship Game")
    redraw_gameboard.geometry("1000x1000")
    redraw_gameboard.resizable(False, False)

    Label(redraw_gameboard, text="Player Underwater", height = 3, width = 15).grid(row=0, column=0, columnspan=10)
    Label(redraw_gameboard, text="Player Surface", height = 3, width = 15).grid(row=0, column=12, columnspan=10)

    # Preparing spacings between the grids
    Label(redraw_gameboard, text="", height = 20, width = 4).grid(row=1, column=10, rowspan=10)
    Label(redraw_gameboard, text="AI Underwater", height = 3, width = 15).grid(row=11, column=0, columnspan=10)
    Label(redraw_gameboard, text="AI Surface", height = 3, width = 15).grid(row=11, column=11, columnspan=10)
    #Label(redraw_gameboard, text="", height = 4, width = 1).grid(row=11, column=0, columnspan=10)


    for i in range(12, 22):
        AI_player["AI Underwater"][i] = {}
        for j in range(10):
            AI_player["AI Underwater"][i][j] = {}
            AI_player["AI Underwater"][i][j]["Presence"] = None
            AI_underwater_cell = Button(redraw_gameboard, 
                                        height = button_height, 
                                        width = button_width,
                                        highlightbackground="#000080",
                                        command=lambda row=i, column=j, depth = 0: shoot(row, column, depth))
            AI_underwater_cell.grid(row=i, column=j)
            AI_underwater_cell.lower()

    for i in range(1, 11):
        Player["Player Underwater"][i] = {}
        for j in range(10):
            Player["Player Underwater"][i][j] = {}
            Player["Player Underwater"][i][j]["Presence"] = None
            player_underwater_cell = Button(redraw_gameboard, 
                                            height = button_height, 
                                            width = button_width, 
                                            command=cannot_shoot,
                                            highlightbackground="#000080")
            player_underwater_cell.grid(row=i, column=j)
            player_underwater_cell.lower()


    for i in range(1, 11):
        Player["Player Surface"][i] = {}
        for j in range(11, 21):
            Player["Player Surface"][i][j] = {}
            Player["Player Surface"][i][j]["Presence"] = None
            player_surface_cell = Button(redraw_gameboard, 
                                         height = button_height, 
                                         width = button_width, 
                                         command=cannot_shoot,
                                         highlightbackground="#1E90FF")
            player_surface_cell.grid(row=i, column=j) 


    for i in range(12, 22):
        AI_player["AI Surface"][i] = {}
        for j in range(11, 21):
            AI_player["AI Surface"][i][j] = {}
            AI_player["AI Surface"][i][j]["Presence"] = None
            AI_surface_cell = Button(redraw_gameboard, 
                                     height = button_height, 
                                     width = button_width, 
                                     highlightbackground="#1E90FF",
                                     command=lambda row=i, column=j, depth = 1: shoot(row, column, depth))

            AI_surface_cell.grid(row=i, column=j)


    for i in range(len(Player["Carrier"])):
        player_ship_location = Button(redraw_gameboard,
                                      height = button_height, 
                                      width = button_width, 
                                      command=cannot_shoot,
                                      highlightbackground="#2E8B57")

        player_ship_location.grid(row = Player["Carrier"][i][0], column = Player["Carrier"][i][1])

    for i in range (len(Player["Submarine"])):
        player_ship_location = Button(redraw_gameboard,
                                      height = button_height, 
                                      width = button_width, 
                                      command=cannot_shoot,
                                      highlightbackground="#2E8B57")

        player_ship_location.grid(row = Player["Submarine"][i][0], column = Player["Submarine"][i][1])

    print (redraw_gameboard.grid_slaves)
    redraw_gameboard.mainloop()

Tags: forcellcolumnbuttonwidthsurfaceaigrid