Python记忆匹配游戏开发遇TclError:未知选项-window_height
解决Tkinter记忆匹配游戏中的“TclError: unknown option '-window_height'”错误
直接报错原因及修复
当前报错的核心是tkinter.Button组件不存在window_height和window_width参数,你写错了参数名,同时参数之间缺少逗号分隔。
修复代码中创建按钮的行:
# 错误写法 button = Button(command=lambda x=x, y=y: show_symbol(x, y), window_height=3 window_width=3) # 正确写法 button = Button(command=lambda x=x, y=y: show_symbol(x, y), height=3, width=3)
其他潜在问题修复(让游戏正常运行)
除了当前报错,代码还有几处逻辑和用法错误,需要一并修复:
PhotoImage对象创建错误
用PIL打开图片后,需要转换为ImageTk.PhotoImage才能被tkinter识别,修改所有图片加载代码:royaljello = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\StarEgg.gif")) treat = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\treat.gif")) # 其余图片同理修改按钮字典赋值错误
代码中错误地给Button对象本身赋值,应该将按钮存入buttons字典:# 错误写法 button[x, y] = button # 正确写法 buttons[x, y] = button按钮禁用逻辑错误
禁用按钮应该修改state属性,而非command:# 错误写法 buttons[previousX, previousY]["command"] = DISABLED buttons[x, y]["command"] = DISABLED # 正确写法 buttons[previousX, previousY]["state"] = DISABLED buttons[x, y]["state"] = DISABLED图片显示逻辑错误
按钮显示图片需要设置image属性,而非text:# 错误写法 buttons[x, y]["text"] = button_symbols[x, y] # 正确写法 buttons[x, y]["image"] = button_symbols[x, y]避免界面阻塞
time.sleep()会卡住tkinter界面,改用after()方法实现延迟:# 替换show_symbol函数中的time.sleep部分 if buttons[previousX, previousY]["image"] != buttons[x, y]["image"]: # 定义延迟执行的函数 def hide(): buttons[previousX, previousY]["image"] = "" buttons[x, y]["image"] = "" root.after(500, hide)移除无用导入
删除未使用的turtle模块导入:# 移除这一行 from turtle import *
修复后的完整代码示例
import random from tkinter import * from PIL import Image, ImageTk def show_symbol(x, y): global first global previousX, previousY buttons[x, y]["image"] = button_symbols[x, y] buttons[x, y].update_idletasks() if first: previousX = x previousY = y first = False elif previousX != x or previousY != y: if buttons[previousX, previousY]["image"] != buttons[x, y]["image"]: def hide_symbols(): buttons[previousX, previousY]["image"] = "" buttons[x, y]["image"] = "" root.after(500, hide_symbols) else: buttons[previousX, previousY]["state"] = DISABLED buttons[x, y]["state"] = DISABLED first = True root = Tk() root.title('Memory Match') root.resizable(width=False, height=False) buttons = {} first = True previousX = 0 previousY = 0 # 加载图片(使用ImageTk.PhotoImage转换) royaljello = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\StarEgg.gif")) treat = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\treat.gif")) star_treat = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\star_treat.gif")) strawberry = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\Strawberry.gif")) blueberry = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\Blueberry.gif")) pineapple = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\Pineapple.gif")) seed = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\seed.gif")) star_jelly = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\StarJelly.gif")) silveregg = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\Silver_Egg.gif")) goldegg = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\Gold_Egg.gif")) deemondegg = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\Diamond_Egg.gif")) mythegg = ImageTk.PhotoImage(Image.open(r"D:\Documents\Projects\Memorysnap\Mythic_Egg.gif")) button_symbols = {} symbols = [royaljello, royaljello, treat, treat, star_treat, star_treat, strawberry, strawberry, blueberry, blueberry, pineapple, pineapple, seed, seed, star_jelly, star_jelly, silveregg, silveregg, goldegg, goldegg, deemondegg, deemondegg, mythegg, mythegg ] random.shuffle(symbols) for x in range(6) : for y in range(4) : button = Button(command=lambda x=x, y=y: show_symbol(x, y), height=3, width=3) button.grid(column=x, row=y) buttons[x, y] = button button_symbols[x, y] = symbols.pop() root.mainloop() # Credit to Onett for images
内容的提问来源于stack exchange,提问作者Imagineer42
相关产品推荐
相关产品推荐

