我正在尝试使用我选择的图像构建一个记忆游戏。我正在使用 Tkinter 和 Python 我可以创建按钮,但我的功能有问题,不允许我在单击后看到图像按钮。如果我坚持使用数字而不是图像,我就可以让它工作,所以我知道这就是我的问题所在。
我创建了按钮列表并将其默认图像设置为白色。我创建了一个包含我想要显示的图像的列表。 onClick 我想浏览图像列表并将按钮图像更改为列表中的图像。
如有任何帮助,我们将不胜感激!
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
if b["image"] == base and count < 2:
b["image"] = matches[number]
# adding the number to answer list
answer_list.append(number)
# Adding number and button to answer dictionary
answer_dictionary[b] = matches[number]
# Increment count
count += 1
# Determine if they match
if len(answer_list) == 2:
if matches[answer_list[0]] == matches[answer_list[1]]:
match_label.config(text="Your 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 = []
# Reset buttons to be blank
for key in answer_dictionary:
key["image"] = base
answer_dictionary = {}
# Create Buttons for layout
b0 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b0, 0))
b1 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b1, 1))
b2 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b2, 2))
b3 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b3, 3))
b4 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b4, 4))
b5 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b5, 5))
b6 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b6, 6))
b7 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b7, 7))
b8 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b8, 8))
b9 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b9, 9))
b10 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b10, 10))
b11 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b11, 11))
b12 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b12, 12))
b13 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b13, 13))
b14 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b14, 14))
b15 = Button(easy_frame, image=base, height=125, width=125, command=lambda: button_click(b15, 15))
# 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()
请注意,
b["image"]
返回按钮中使用的图像的内部名称(字符串),但base
是ImageTk.PhotoImage()
的实例,因此b["image"] == base
将始终是False
。请使用 b["image"] == str(base)
来代替。
您还需要强制更新选定的按钮并短暂睡眠,让用户看到它们的实际图像。
以下是修改后的
button_click()
:
# Define function for clicking the button
def button_click(b, number):
global count, answer_list, answer_dictionary
# use str(base) instead of base
if b["image"] == str(base) and count < 2:
b["image"] = matches[number]
# adding the number to answer list
answer_list.append(number)
# Adding number and button to answer dictionary
answer_dictionary[b] = matches[number]
# Increment count
count += 1
# Determine if they match
if len(answer_list) == 2:
if matches[answer_list[0]] == matches[answer_list[1]]:
match_label.config(text="Your Found a Match")
for key in answer_dictionary:
key["state"] = "disable"
count = 0
answer_list = []
answer_dictionary = {}
else:
match_label.config(text="Try again")
game.update() # force update the button
game.after(500) # sleep a short period (half second)
count = 0
answer_list = []
# Reset buttons to be blank
for key in answer_dictionary:
key["image"] = base
answer_dictionary = {}