Tkinter图像记忆游戏点击按钮不显示图像问题求助
问题
使用Python的Tkinter库开发自定义图像记忆游戏时,按钮默认白色背景图正常显示,但点击按钮后无法切换到目标图像。用数字替代图像时功能正常,确定问题出在图像相关逻辑上。
问题分析
核心问题有两个:
- 图像对象比较逻辑错误:
b["image"] == base的比较方式不成立,因为Tkinter中Button["image"]返回的是图像对象的内部字符串标识,而非直接的ImageTk.PhotoImage对象,无法与base对象直接匹配。 - 代码缩进错误:匹配成功分支中的变量重置语句缩进错误,导致逻辑无法正常执行。
修复方案
- 给每个按钮添加自定义属性
current_image,用于跟踪当前显示的图像对象,替代直接比较b["image"]的方式。 - 修正匹配成功分支的缩进问题,确保变量重置逻辑正确执行。
修复后的完整代码
from tkinter import * import random from PIL import ImageTk, Image game = Tk() game.title('Memory Game Challenge') game.geometry("800x1100") # Images base = ImageTk.PhotoImage(Image.open("/Users/a/PycharmProjects/Images/white.jpeg").resize((125, 125))) img1 = ImageTk.PhotoImage(Image.open("/Users/a/PycharmProjects/Images/red_1.png").resize((125, 125))) img2 = ImageTk.PhotoImage(Image.open("/Users/a/PycharmProjects/Images/red_2.png").resize((125, 125))) img3 = ImageTk.PhotoImage(Image.open("/Users/a/PycharmProjects/Images/red_3.png").resize((125, 125))) img4 = ImageTk.PhotoImage(Image.open("/Users/a/PycharmProjects/Images/red_4.png").resize((125, 125))) img5 = ImageTk.PhotoImage(Image.open("/Users/a/PycharmProjects/Images/brown_1.png").resize((125, 125))) img6 = ImageTk.PhotoImage(Image.open("/Users/a/PycharmProjects/Images/brown_2.png").resize((125, 125))) img7 = ImageTk.PhotoImage(Image.open("/Users/a/PycharmProjects/Images/brown_3.png").resize((125, 125))) img8 = ImageTk.PhotoImage(Image.open("/Users/a/PycharmProjects/Images/brown_4.png").resize((125, 125))) # Creating list for matches matches = [img1, img1, img2, img2, img3, img3, img4, img4, img5, img5, img6, img6, img7, img7, img8, img8] # Shuffle matches random.shuffle(matches) # Create Easy Frame 4 * 4 easy_frame = Frame(game) easy_frame.pack(pady=10) # Define Variables count = 0 answer_list = [] answer_dictionary = {} # Define function for clicking the button def button_click(b, number): global count, answer_list, answer_dictionary # 改用自定义属性跟踪当前图像,避免直接比较b["image"] if b.current_image == base and count < 2: b["image"] = matches[number] b.current_image = matches[number] # 添加序号到答案列表 answer_list.append(number) # 记录按钮与对应图像的映射 answer_dictionary[b] = matches[number] # 计数加1 count += 1 # 判断是否选中两张卡片 if len(answer_list) == 2: if matches[answer_list[0]] == matches[answer_list[1]]: match_label.config(text="You Found a Match") for key in answer_dictionary: key["state"] = "disable" # 重置变量 count = 0 answer_list = [] answer_dictionary = {} else: match_label.config(text="Try again") count = 0 answer_list = [] # 重置按钮为默认图像 for key in answer_dictionary: key["image"] = base key.current_image = base answer_dictionary = {} # Create Buttons for layout b0 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b0, 0)) b0.current_image = base # 添加自定义属性记录当前图像 b1 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b1, 1)) b1.current_image = base b2 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b2, 2)) b2.current_image = base b3 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b3, 3)) b3.current_image = base b4 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b4, 4)) b4.current_image = base b5 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b5, 5)) b5.current_image = base b6 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b6, 6)) b6.current_image = base b7 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b7, 7)) b7.current_image = base b8 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b8, 8)) b8.current_image = base b9 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b9, 9)) b9.current_image = base b10 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b10, 10)) b10.current_image = base b11 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b11, 11)) b11.current_image = base b12 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b12, 12)) b12.current_image = base b13 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b13, 13)) b13.current_image = base b14 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b14, 14)) b14.current_image = base b15 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b15, 15)) b15.current_image = base # Assign buttons to Grid b0.grid(row=0, column=0) b1.grid(row=0, column=1) b2.grid(row=0, column=2) b3.grid(row=0, column=3) b4.grid(row=1, column=0) b5.grid(row=1, column=1) b6.grid(row=1, column=2) b7.grid(row=1, column=3) b8.grid(row=2, column=0) b9.grid(row=2, column=1) b10.grid(row=2, column=2) b11.grid(row=2, column=3) b12.grid(row=3, column=0) b13.grid(row=3, column=1) b14.grid(row=3, column=2) b15.grid(row=3, column=3) match_label = Label(game, text="") match_label.pack(pady=20) game.mainloop()
内容的提问来源于stack exchange,提问作者learningToCode
相关产品推荐
相关产品推荐

