【发布时间】:2021-07-15 09:10:30
【问题描述】:
我正在尝试在 Python Tkinter 中创建一个记忆游戏。我对 tkinter 还很陌生。我使用嵌套的 for 循环创建了 16 个显示卡片的按钮,并将每个按钮添加到列表中。我的问题是用公主图像替换卡片图像(这是迪士尼公主记忆游戏)。它说列表索引超出范围,尽管它不应该是。有人可以帮我解决吗?我尝试以多种方式解决它,但没有成功。到目前为止,这是我的代码。我还没有完成游戏,所以有些变量没有真正使用。
代码:
from tkinter import *
from random import choice
screen = Tk()
screen.title("Disney Princesses Memory Game")
width = screen.winfo_screenwidth()
height = screen.winfo_screenheight()
screen.geometry("%dx%d" % (width, height))
screen.configure(bg="#e0bce5")
title = Label(screen, text="Memory Game", font=("David", 50, "underline", "bold"), bg="#e0bce5")
title.place(x=400, y=20)
images_list = [
PhotoImage(file="images/aurora.png"),
PhotoImage(file="images/belle.png"),
PhotoImage(file="images/cinderella.png"),
PhotoImage(file="images/jasmine.png"),
PhotoImage(file="images/mulan.png"),
PhotoImage(file="images/rapunzel.png"),
PhotoImage(file="images/snow white.png"),
PhotoImage(file="images/tiana.png")
]
buttons_list = []
num_list = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
def replace_card(c, d):
chosen_image = choice(images_list)
check = num_list[images_list.index(chosen_image)]
check += 1
buttons_list[c][d].configure(image=chosen_image)
if check > 2:
num_list.remove(num_list[check])
card = PhotoImage(file="images/card.png")
x = 100
y = 250
for i in range(2):
for j in range(8):
a = Button(screen, image=card, command=replace_card(i, j))
x += 100
a.place(x=x, y=y)
buttons_list.append([a])
y += 100
x = 100
screen.mainloop()
这是错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\Meirom\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
return self.func(*args)
File "C:\Users\Meirom\PycharmProjects\memorygame\main.py", line 46, in <lambda>
a = Button(screen, image=card, command=lambda: replace_card(i, j))
File "C:\Users\Meirom\PycharmProjects\memorygame\main.py", line 35, in replace_card
buttons_list[c][d].configure(image=chosen_image)
IndexError: list index out of range
我期望的输出需要在点击卡片时显示卡片的按钮上显示生成的图像
非常感谢 Shoaib Ahmed 和其他所有人的帮助! 这是完成的程序(我需要添加更多功能,所以它没有完全完成,但原来的错误已经解决):
from tkinter import *
from random import choice
screen = Tk()
screen.title("Disney Princesses Memory Game")
width = screen.winfo_screenwidth()
height = screen.winfo_screenheight()
screen.geometry("%dx%d" % (width, height))
screen.configure(bg="#e0bce5")
title = Label(screen, text="Memory Game", font=("David", 50, "underline", "bold"), bg="#e0bce5")
title.place(x=400, y=20)
images_list = [
PhotoImage(file="images/aurora.png"),
PhotoImage(file="images/belle.png"),
PhotoImage(file="images/cinderella.png"),
PhotoImage(file="images/jasmine.png"),
PhotoImage(file="images/mulan.png"),
PhotoImage(file="images/rapunzel.png"),
PhotoImage(file="images/snow white.png"),
PhotoImage(file="images/tiana.png")
]
buttons_list = []
chosen_images = []
flipped = []
num_list = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
card = PhotoImage(file="images/card.png")
def choose_images():
count = 0
for num in range(16):
chosen_image = choice(images_list)
count += 1
if count > 2:
count = 0
reset()
else:
chosen_images.append(chosen_image)
def replace_card(c, d):
global flipped
buttons_list[c][d].configure(image=chosen_images[d])
flipped.append(buttons_list[c][d])
def reset():
global card
for element in flipped:
element.configure(image=card)
choose_images()
x = 100
y = 250
for i in range(2):
buttons_list.append([])
for j in range(8):
a = Button(screen, image=card, command=lambda i=i, j=j: replace_card(i, j))
x += 100
a.place(x=x, y=y)
buttons_list[i].append(a)
y += 100
x = 100
screen.mainloop()
【问题讨论】:
-
在
a = Button(screen, image=card, command=replace_card(i, j))你不能调用带参数的函数。您只需提供函数名称,例如a = Button(screen, image=card, command=replace_card)查看此链接以更好地了解命令绑定:pythontutorial.net/tkinter/tkinter-command -
嗨@Roni 欢迎来到stackoverflow。您能否将实际的错误消息添加到您的帖子中?这将使人们更容易提供帮助
-
游戏规则是什么?玩家点击卡片,列表和屏幕上会发生什么?
-
实现细节取决于程序应该做什么。但在我看来,没有必要将按钮添加到列表中并从列表中获取它们。您可以将某种事件绑定到它们。如果这些按钮是某些小部件的子级,您可以重置所有按钮图像。
-
@8349697,你能给我举个例子来说明你的意思吗?我真的不明白