Python Tkinter 'PhotoImage' 对象没有属性 'configure' 图片更新一次后

Python Tkinter 'PhotoImage' object has no attribute 'configure' after picture is updated once

我正在开发一个简单的 python 游戏,它显示一张支付卡,你必须猜测下一张卡是更低还是更高。

我 运行 关注的问题:
卡片图片更新一次,下次更新时出现如下错误:

Card updated
Count 'Right guesses' increased
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\username\anaconda3\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
  File "C:\Users\username\PycharmProjects\CardsGame\main.py", line 77, in press_lower
    get_new_card()
  File "C:\Users\username\PycharmProjects\CardsGame\main.py", line 59, in get_new_card
    label_image.configure(image=new_card)
AttributeError: 'PhotoImage' object has no attribute 'configure'

我不知道为什么函数 'get_new_card()' 工作一次,但从第二次调用开始就抛出错误。

我的代码:

import tkinter as tk
from PIL import ImageTk, Image
import random

# increase right counter
def count_right():
    global right_num
    right_num.set(right_num.get() + 1)
    print("Count 'Right guesses' increased")

# increase wrong counter
def count_wrong():
    global wrong_num
    wrong_num.set(wrong_num.get() + 1)
    print("Count 'Wrong guesses' increased")

# GUI
window = tk.Tk()
window.title('Guess the card')
window.geometry('300x480')
window.resizable(False, False)
# DEFINITIONS
# define frames
 
 # define counter variables
 right_num = tk.IntVar()
 wrong_num = tk.IntVar()
 right_num.set(0)
 wrong_num.set(0)
 
# define labels
label_top = tk.Label(text='Will the next card be higher or lower?', width=30, height=3)
label_right = tk.Label(text='Right guesses:', width=50, height=2)
label_right_num = tk.Label(textvariable=right_num, width=50, height=2)
label_wrong = tk.Label(text='Wrong guesses:', width=50, height=2)
label_wrong_num = tk.Label(textvariable=wrong_num, width=50, height=2)
label_space1 = tk.Label(text='', width=50, height=1)
label_space2 = tk.Label(text='', width=50, height=1)

# image
# program opens the same card when opening
current_img = '26.jpg'
previous_img = ''
img_start = ImageTk.PhotoImage(Image.open(current_img))

label_image = tk.Label(image = img_start) 

#function to change image
def get_new_card():
    global label_image
    global current_img
    global previous_img
    # save state of current card
    previous_img = current_img
    # gen new card
    current_img = str(random.randint(1, 52)) + '.jpg'
    new_card = ImageTk.PhotoImage(Image.open(current_img))
    label_image.configure(image=new_card)
    label_image = new_card
    print('Card updated')

def press_higher():
    global current_img
    global previous_img
    # check if new card higher or lower than previous card
    get_new_card()
    if current_img > previous_img:
        count_right()
    else:
        count_right()

def press_lower():
     global current_img
     global previous_img
     # check if new card higher or lower than previous card
    get_new_card()
    if current_img < previous_img:
    count_right()
else:
    count_right()

# define buttons
button_higher = tk.Button(text='Higher', width=15, height=2, command = press_higher)
button_lower = tk.Button(text='Lower', width=15, height=2, command = press_lower)

# PACKS:
label_top.pack()  
label_image.pack()
label_space2.pack()
button_higher.pack()
button_lower.pack()
label_space1.pack()
label_right.pack()
label_right_num.pack()
label_wrong.pack()
label_wrong_num.pack()

# create mainloop to keep running the script
window.mainloop()

我很高兴能得到任何帮助,因为我在网上找不到太多关于这个特定问题的信息,这使我无法完成这个项目。

acw1668的答案有效:

I think label_image = new_card should be label_image.image = new_card instead in order to save a reference of the image.