内存游戏中的第二个文本不显示(Tkinter)



这是我第一次发布问题,所以请告诉我还需要什么我一直在尝试使用Tkinter创建内存游戏,请在下面找到我的代码


from tkinter import Tk , Button , DISABLED, NORMAL
import time
import random
global texts
texts = ['A','B','C','D','E','F','G','H','A','B','C','D','E','F','G','H']
random.shuffle(texts)
print(texts)

global symbols
first = True
win = Tk()
win.title("Flipping Tiles")
win.resizable(0,0)

def action(x,y):
print(x,y)
global first
global first_text
global firsti
global firstj

if first:
print("first")
print(symbols[x][y])
first = False
firsti = x
firstj = y
button = Button (win,text = symbols[x][y] , height = 5, width = 7 )
first_text = symbols[x][y]
button.grid(row=x,column=y)

elif (firsti,firstj) == (x,y):
print("same")
pass

else:
print("second")
print(symbols[x][y])
first = True
button = Button (win,text = symbols[x][y] , height = 5, width = 7 )
button.grid(row=x,column=y)          

if first_text == symbols[x][y]:
print("disable part")
button = Button (win,text = symbols[firsti][firstj] , height = 5, width = 7, state = DISABLED )
button.grid(row=firsti,column=firstj)
button = Button (win,text = symbols[x][y] , height = 5, width = 7, state = DISABLED )
button.grid(row=x,column=y)    

else:
time.sleep(3)   
print("plain part")

button = Button (win,text = " " , height = 5, width = 7)
button.grid(row=firsti,column=firstj)
button = Button (win,text = " ", height = 5, width = 7)
button.grid(row=x,column=y)



k = 0
symbols = []
for x in range(4):
symbols.append([])
for y in range(4):
button = Button (win, command = lambda x=x , y=y: action(x,y), height = 5, width = 7 )
button.grid(row=x,column=y)
symbols[x].append(texts[k])
k+=1
win.mainloop()

根据我的观察,除了显示第二个符号(文本(外,其他一切都很好你能帮我知道出问题的原因吗

谢谢!

time.sleep()将阻止tkintermainloop()更新,因此在mainloop()收回控制之前,不会显示time.sleep()之前的所有更改。

此外,您应该更新正在单击的按钮的文本,而不是创建新按钮。

以下是基于您的修改和简化代码:

from tkinter import Tk , Button , DISABLED
import time
import random
texts = ['A','B','C','D','E','F','G','H','A','B','C','D','E','F','G','H']
random.shuffle(texts)
print(texts)
first_button = None
win = Tk()
win.title("Flipping Tiles")
win.resizable(0,0)

def action(btn):
global first_button

btn.config(text=btn.symbol) # show the symbol for the clicked button
btn.update_idletasks() # force update before time.sleep()
if first_button is None:
print("first", btn.symbol)
first_button = btn   # save the first button clicked
elif btn is first_button:
print("same")  # ignore same button click
else:
print("second", btn.symbol)
if first_button.symbol == btn.symbol:
print("disable part")
# disable matched buttons
first_button.config(state=DISABLED, bg="#eee")
btn.config(state=DISABLED, bg="#eee")
else:
time.sleep(1.5)   # use shorter delay
print("plain part")
# hide the symbols of clicked buttons
first_button.config(text="")
btn.config(text="")
first_button = None # reset for next round

# create the buttons
buttons = []
for i, symbol in enumerate(texts):
button = Button(win, height=5, width=7, disabledforeground="black")
button.config(command=lambda btn=button: action(btn))
button.grid(row=i//4, column=i%4)
button.symbol = symbol
buttons.append(button)
win.mainloop()

因此,这是一个非常直接的解决方案,它将使文本出现,然后等待大约3秒,然后消失,更大的问题(在问题中没有问(是,如果瓦片不匹配,它们以后将无法点击:

def action(x,y):
print(x,y)
global first
global first_text
global firsti
global firstj

if first:
print("first")
print(symbols[x][y])
first = False
firsti = x
firstj = y
button = Button (win,text = symbols[x][y] , height = 5, width = 7 )
first_text = symbols[x][y]
button.grid(row=x,column=y)

elif (firsti,firstj) == (x,y):
print("same")
pass

else:
print("second")
print(symbols[x][y])
first = True
button = Button (win,text = symbols[x][y] , height = 5, width = 7 )
button.grid(row=x,column=y)
for widget in win.winfo_children():
widget.config(state=DISABLED)
def func():
if first_text == symbols[x][y]:
print("disable part")
button = Button (win,text = symbols[firsti][firstj] , height = 5, width = 7, state = DISABLED )
button.grid(row=firsti,column=firstj)
button = Button (win,text = symbols[x][y] , height = 5, width = 7, state = DISABLED )
button.grid(row=x,column=y)
else:
print("plain part")
button = Button (win,text = " " , height = 5, width = 7)
button.grid(row=firsti,column=firstj)
button = Button (win,text = " ", height = 5, width = 7)
button.grid(row=x,column=y)
for widget in win.winfo_children():
widget.config(state=NORMAL)
win.after(3000, func)

正如你所看到的,我在else中定义了整个if/elif语句,然后用after调用它,这样它在3秒钟后执行,但不会干扰tkinter的主循环

正如@acw1668所提到的,使用after将使按钮可点击,所以我添加了几行来解决这个问题:当点击第二个按钮时,它会禁用所有按钮(所有小部件,但在这种情况下它们都是按钮(,然后当最后执行func()时,使所有按钮再次可点击。

最新更新