我正在開發一個簡單的Python游戲,該游戲顯示一張付費牌,你必須猜測下一張牌是低還是高。
我遇到的問題是。
當卡的圖片被更新一次后,在下一次更新時,會出現以下錯誤:
Card updated
計數 '正確猜測' 增加了
例外 in Tkinter回呼
回溯(最近一次呼叫)。
檔案 "C:Usersusernameanaconda3lib kinter\__init__.py", line 1892, in __call__
return self.func(*args)
檔案 "C:UsersusernamePycharmProjectsCardsGamemain.py", 行 77, inpress_lower
get_new_card()
檔案 "C:UsersusernamePycharmProjectsCardsGamemain.py", line 59, in get_new_card
label_image.configure(image=new_card)
AttributeError: 'PhotoImage' object沒有屬性'configure'。
我不知道為什么函式'get_new_card()'一次成功,但從第二次呼叫開始就會出現錯誤。
我的代碼:
import tkinter as tk
from PIL import ImageTk, Image
import random
# 增加右邊的計數器。
def count_right()。
global right_num
right_num.set(right_num.get() 1)
print("'正確猜測'計數增加")
# 增加錯誤的計數器。
def count_wrong() 。
global wrong_num
wrong_num.set(wrong_num.get() 1)
print("Count 'wrong guesses' increased")
# GUI[/span]。
window = tk.Tk()
window.title('Guess the card')
window.geometry('300x480')
window.resizable(False, False)
# DEFINITIONS[/span]。
# 定義框架 # 定義框架
# define counter variables
right_num = tk.IntVar()
wrong_num = tk.IntVar()
right_num.set(0)
wrong_num.set(0)
# define labels # 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='Rong 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=''/span>, width=50, height=1)
# image# 程式打開時,打開的是同一張卡。
current_img = '26.jpg'/span>
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
# 保存當前卡片的狀態 # 保存當前卡片的狀態
previous_img = current_img
#生成新卡
current_img = str(random.randint(1, 52) '.jpg'/span>
new_card = ImageTk.PhotoImage(Image.open(current_img))
label_image.configure(image=new_card)
label_image = new_card
print('Card updated'/span>)
def press_higher()。
global current_img
global previous_img
# check if new card higher or lower than previous card[/span]。
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[/span]。
get_new_card()
if current_img < previous_img:
count_right()
else:
count_right()
# define buttons else: count_right()
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()
# 創建mainloop以繼續運行腳本。
window.mainloop()
我很高興得到任何幫助,因為我在網上找不到很多關于這個具體問題的資料,使我無法完成這個專案。
uj5u.com熱心網友回復:
acw1668的答案是:
我認為
label_image = new_card應該是label_image.image = new_card。 以便保存影像的參考。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/319469.html
標籤:
上一篇:在python中使用tkinter在加載時播放GIF影片
下一篇:擴大到2個吊艙并沒有改善TPS
