Не отображается картинка в ai_label, tkinter

Форумчане! В ai_label не отображается картинка, почему?

Вот код:

import tkinter as tk
from PIL import ImageTk
from random import randint

root = tk.Tk()
root.geometry('700x512')
root.resizable(0, 0)
player_selection = ''
psr = ['p', 's', 'r']
ai_selection = ''
game_result = ''
index = 0
ai_image, player_image = '', ''
imgPaper = ImageTk.PhotoImage(file = 'paper.png')
imgScissors = ImageTk.PhotoImage(file = 'scissors.png')
imgRock = ImageTk.PhotoImage(file = 'rock.png')
def ai_moves():
    index = randint(0, 2)
    ai_selection = psr[index]
    win_check()

def image_to_output_ai():
    global ai_image
    if ai_selection == 'p':
        ai_image = imgPaper
    elif ai_selection == 's':
        ai_image = imgScissors
    elif ai_selection == 'r':
        ai_image = imgRock
def image_to_output_player():
    global player_image
    if player_selection == 'p':
        player_image = imgPaper
    elif player_selection == 's':
        player_image = imgScissors
    elif player_selection == 'r':
        player_image = imgRock
def block_buttons():
    btnPaper.config(state = 'disabled')
    btnScissors.config(state = 'disabled')
    btnRock.config(state = 'disabled')
def win_check():
    global game_result, ai_selection
    global ai_image
    image_to_output_player()
    image_to_output_ai()
    player_label.config(image = player_image)
    ai_label.config(image = ai_image)
    if player_selection == ai_selection:
        game_result = 'ничья'
    elif (player_selection == 'p' and ai_selection == 'r') or (player_selection == 's' and ai_selection == 'p') or (player_selection == 'r' and ai_selection == 's'):
        game_result = 'победа'
    elif (ai_selection == 'p' and player_selection == 'r') or (ai_selection == 's' and player_selection == 'p') or (ai_selection == 'r' and player_selection == 's'):
        game_result = 'поражение'
    block_buttons()
    print(game_result)
    ai_selection = psr[index]

def player_selection_def(what):
    global player_selection
    player_selection = what
    ai_moves()

btnPaper = tk.Button(root, text = 'БУМАГА', font = 'Arial 18 bold', command = lambda: player_selection_def('p'))
btnPaper.place(width = 135, height = 54, x = 50, y = 400)
btnScissors = tk.Button(root, text = 'НОЖНИЦЫ', font = 'Arial 18 bold', command = lambda: player_selection_def('s'))
btnScissors.place(width = 135, height = 54, x = 280, y = 400)
btnRock = tk.Button(root, text = 'КАМЕНЬ', font = 'Arial 18 bold', command = lambda: player_selection_def('r'))
btnRock.place(width = 135, height = 54, x = 525, y = 400)
player_label = tk.Label(root)
player_label.place(x = 50, y = 50)
ai_label = tk.Label(root)
ai_label.place(x = 300, y = 50)

root.mainloop()

Ответы (1 шт):

Автор решения: S. Nick

Попробуйте так:

import tkinter as tk
from PIL import ImageTk
from random import randint, choice


root = tk.Tk()
root.geometry('700x512')
root.resizable(0, 0)

player_selection = ''
psr = ['p', 's', 'r']
ai_selection = ''
game_result = ''
index = 0
ai_image, player_image = '', ''

imgPaper = ImageTk.PhotoImage(file = 'paper.png')
imgScissors = ImageTk.PhotoImage(file = 'scissors.png')
imgRock = ImageTk.PhotoImage(file = 'rock.png')

def ai_moves():
    index = randint(0, 2)
    ai_selection = psr[index]
    win_check(ai_selection)                                    # + ai_selection

def image_to_output_ai(ai_selection):                          # + ai_selection
    global ai_image
    if ai_selection == 'p':
        ai_image = imgPaper
    elif ai_selection == 's':
        ai_image = imgScissors
    elif ai_selection == 'r':
        ai_image = imgRock
        
def image_to_output_player():
    global player_image
    if player_selection == 'p':
        player_image = imgPaper
    elif player_selection == 's':
        player_image = imgScissors
    elif player_selection == 'r':
        player_image = imgRock
        
def block_buttons():
    btnPaper.config(state = 'disabled')
    btnScissors.config(state = 'disabled')
    btnRock.config(state = 'disabled')
    
def win_check(ai_selection):                                   # + ai_selection
    global game_result 
    global ai_image

    image_to_output_player()
    image_to_output_ai(ai_selection)                           # + ai_selection
    
    player_label.config(image=player_image)
    ai_label.config(image = ai_image)
    
    if player_selection == ai_selection:
        game_result = 'ничья'
    elif (player_selection == 'p' and ai_selection == 'r') \
        or (player_selection == 's' and ai_selection == 'p') \
        or (player_selection == 'r' and ai_selection == 's'):
        game_result = 'победа'
    elif (ai_selection == 'p' and player_selection == 'r') \
        or (ai_selection == 's' and player_selection == 'p') \
        or (ai_selection == 'r' and player_selection == 's'):
        game_result = 'поражение'
        
    print(game_result)
    result_label.config(text=game_result)
    
# ?  block_buttons()
# ?  ai_selection = psr[index]

def player_selection_def(what):
    global player_selection
    player_selection = what
    ai_moves()

btnPaper = tk.Button(root, text='БУМАГА', font = 'Arial 18 bold', 
    command = lambda: player_selection_def('p'))
btnPaper.place(width = 135, height = 54, x = 50, y = 400)

btnScissors = tk.Button(root, text = 'НОЖНИЦЫ', font = 'Arial 18 bold', 
    command = lambda: player_selection_def('s'))
btnScissors.place(width = 135, height = 54, x = 280, y = 400)

btnRock = tk.Button(root, text = 'КАМЕНЬ', font = 'Arial 18 bold', 
    command = lambda: player_selection_def('r'))
btnRock.place(width = 135, height = 54, x = 525, y = 400)

player_label = tk.Label(root, text="Hello", bg="yellow")
player_label.place(x = 50, y = 50)

ai_label = tk.Label(root, text="World", bg="#FF0000")
ai_label.place(x = 400, y = 50)

result_label = tk.Label(root, fg="#FF0000", font = 'Arial 18 bold')     # +
result_label.place(x = 50, y = 220)                                     # +

root.mainloop()

введите сюда описание изображения

введите сюда описание изображения


paper.png

введите сюда описание изображения

scissors.png

введите сюда описание изображения

rock.png

введите сюда описание изображения

→ Ссылка