Python 棋盘赢了';t显示 代码: 变量值:

Python 棋盘赢了';t显示 代码: 变量值:,python,python-3.x,tkinter,python-chess,Python,Python 3.x,Tkinter,Python Chess,文件 [black-bishop.png]、[black-bishop2.png]、[black-king.png]、[black knight.png]、[black knight.png]、[black queen.png]、[black rook.png]、[black rook2.png]、[black pawn.png]、[black pawn.png]、[black pawn.png]、[black pawn.png]、[black pawn.png]、[black pawn.png

文件

[black-bishop.png]、[black-bishop2.png]、[black-king.png]、[black knight.png]、[black knight.png]、[black queen.png]、[black rook.png]、[black rook2.png]、[black pawn.png]、[black pawn.png]、[black pawn.png]、[black pawn.png]、[black pawn.png]、[black pawn.png]、[black pawn.png]、[black pawn,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,white pawn.png,white pawn.png,white pawn.png,white pawn.png,white pawn pawn.png,white pawn pawn.png,white pawn pawn.png,white pawn pawn.png,white pawn.png,white pawn,white pawn.png“,”white queen.png“,”white rook.png“,”white-rook2.png“]

我知道这不是正确的顺序,但我现在只想把它显示在黑板上

输出:

问题: 我希望在前2行和后2行的每个方块上都能找到碎片。

  • 为什么不是这样
  • 还有,为什么车周围有一个黑匣子?我的图像中没有这个黑匣子
  • 编辑: 使用@Boendal answer,我得到以下结果:

    为什么有些方块中间有黑色方块(尤其是黑色方块) 碎片在哪里

    编辑#2: 暗方块是由于图像不透明造成的


    通过使用找到的信息将它们转换为透明,修复了此问题。

    您缺少的是保留图像的引用

    import tkinter, os
    from PIL import ImageTk, Image
    
    def sortFileImages(files):
        black = [i for i in files if 'black' in i]
        white = [i for i in files if 'white' in i]
        pawns = [[i]*7 for i in files if 'pawn' in i]
        return black + pawns[0] + [' ']*32 + pawns[1] + white
    
    files = sortFileImages(files=os.listdir("web/images/"))
    root = tkinter.Tk()
    
    index = 0
    for r in range(8):
        for c in range(8):
            if files[index] != ' ':
                img = ImageTk.PhotoImage(Image.open('web/images/{}'.format(files[index])).resize((64,64)))
            else:
                img = ImageTk.PhotoImage(Image.new('RGB', (64,64)))
            label = tkinter.Label(root, image=img, borderwidth=8)
    
            # Color the grid squares
            if (r%2 == 0 and c%2 == 0) or (r%2 == 1 and c%2 == 1):
                label.configure(background='grey')
            else:
                label.configure(background='white')
            
            label.grid(row=r,column=c)
            index += 1
    
    root.mainloop()
    
    应该可以解决问题。你也可以更换你的if

    label = tkinter.Label(root, image=img, borderwidth=8)
    label.image=img
    

    参考:

    @编辑有关黑色背景的信息:

    如果我使用你的代码,我得到了整个电路板与黑色方块,但我没有图像。如果我使用以下代码(对我来说,它将始终进入其他部分):

    我用64x64完全透明的图像填充空白区域。它对我来说很好,没有奇怪的大小和黑色方块

    您需要一个全透明的64x64图像。请使用photoshop或gimp创建一个


    关于数字后面的黑色方块,我认为数字(黑兵和塔)有问题。

    如果有人能告诉我如何包含我使用的png文件(件)在上面的问题中,那就太好了!我想不出一种通过问题界面的方法…好吧,我们来看看你的代码。很可能那时我们甚至不需要-第二双/无数双眼睛会发现你没有发现的错误。在这个网站上复制关于消失Tkinter图像的所有问题:你没有保存对您的图像的引用(除了最近的一个),因此它们会被垃圾收集。正如@jasonharper所说的,您必须保留所有
    img
    (即,在列表中或在回答中使用
    label.img
    like)将
    PhotoImage
    保存在内存中。如果您将新的
    PhotoImage
    分配给
    img
    ,则它会从内存中删除以前的图像,而您无法看到它。这是《谢谢》中的一个错误,它似乎解决了仅提供一张图片的问题。但是,我仍然在一些坐标方格的中心有黑色正方形,如我的ed所示这是怎么回事?我查过了,你所做的基本上是说如果没有碎片,那么那里就什么都没有,这使得电路板看起来有点奇怪。我检查了代码,它确实进入了
    if
    16次,然后
    else
    32次,然后
    if
    16次,就像预期的那样。@lbragile用完全透明的ima填充ge 64x64根据您的建议使其正常工作!非常感谢。
    if (r%2 == 0 and c%2 == 0) or (r%2 == 1 and c%2 == 1):
    
    if r%2 == c%2:
    
    if files[index] != ' ':
        img = ImageTk.PhotoImage(Image.open('web/images/{}'.format(files[index])).resize((64,64)))
        label = tkinter.Label(root, image=img, borderwidth=8)
        label.image = img
    else: # Always jump into else because my files is filled with ' '
        label = tkinter.Label(root, image='web/images/{}'.format("blank.png"), borderwidth=8)