1
이것은 사용자가 두 개의 버튼 (셀)을 선택하는 메모리 게임입니다. 세포 뒤에있는 두 단어가 일치하면 단어가 보이지 않는 채로 세포가 숨겨지고 그 뒤에있는 단어를 볼 수 없습니다. 내가하고 싶은 것은 눌렀을 때 배후에 단어가 표시되면 세포를 얼려 버리는 것입니다. 이 함수를 choice1과 choice2 모두에 적용하고 싶습니다. 사용자가 이미 찾은 쌍에도 적용하고 싶습니다.Python : 버튼을 눌렀을 때 버튼을 멈추게하려면 어떻게해야합니까? - Tkinter
나는
if self.hidden:
self.hidden = False
else:
self.hidden = False
에 라인 (16)에 self.hidden = not self.hidden
을 변경 시도했지만 그 pefectly 작동하지 않았다. 그것은 더 이상 클릭 할 수없는시기
from tkinter import *
import random
class Cell:
def __init__(self, word, hidden = True):
self.word = word
self.hidden = hidden
def show_word(self):
""" Shows the word behind the cell """
if self.hidden:
self.hidden = False
else:
self.hidden = False
self.button["text"] = str(self)
if mem.choice1 is None:
mem.choice1 = self
elif mem.choice2 is None:
mem.choice2 = self
mem.update_tries()
else:
choice1, choice2 = mem.choice1, mem.choice2
mem.choice1, mem.choice2 = self, None
self.check(choice1, choice2)
def check(self, choice1, choice2):
""" Checks if the chosen words are a pair """
if choice1.word != choice2.word:
for cell in (choice1, choice2):
cell.hidden = True
cell.button['text'] = str(cell)
def __str__(self):
""" Displays or hides the word """
if self.hidden:
return "---"
else:
return self.word
class Memory(Frame):
""" GUI application that creates a Memory game """
def __init__(self, master):
super(Memory, self).__init__(master)
self.grid()
self.create_widgets()
self.tries = 0
self.choice1 = None
self.choice2 = None
다음
는 중요한 부분입니다? self.hidden의 값을 토글하는 방법에 대해 묻고 있습니까? 'self.hidden = not self.hidden'은 잘 동작하며, 아무 것도 대체 할 필요가 없습니다. –셀을 클릭하면 셀 뒤의 단어가 "---"대신 표시됩니다. 문제는 사용자가 어리 석고 동일한 셀을 다시 누르는 것입니다. 셀이 숨겨지고 "---"이 표시됩니다. 나는 그것이 "---"로 돌아 가기를 원하지 않는다. 나는 그것을 "동결"하고 여전히 그 말을 보여주고 싶습니다. – Amazon