Reputation: 105
This is a memory game where the user selects two buttons (cells). If the two words behind the cells match, the words stays visible else the cells goes hidden and u can't see the words behind them. What I want to do is to make a cell freeze if it shows the word behind it when it is pressed. I want to apply that function to both choice1 and choice2 and even to the pairs that the user have already found.
I have tried to change self.hidden = not self.hidden
on line 16 to
if self.hidden:
self.hidden = False
else:
self.hidden = False
but that didn't work out pefectly.
Here are the important part:
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
Upvotes: 1
Views: 385
Reputation: 4443
You could disable the button when it should not be clickable anymore:
cell.button.config(state = DISABLED)
Upvotes: 1