Matching pairs game in Python tkinter
<p>I am trying to create a matching card game in Python 3 tkinter. The player is presented with a 4x4 grid of buttons, each button with an image. There are eight pairs of images. The player clicks on a button, and the button shows the image; the player clicks on another button to show its image. If the two buttons show the same image, the images stay until the game ends; else, when I click another button, both these images cease to be displayed. </p>

<p>I can create the grid, however, I'm having trouble getting the actual game to work. Below is the code I've created so far.</p>

<pre><code>#Import all necessary modules.

from tkinter import *
from tkinter import ttk
import random
#Imnporting shuffle allows us to shuffle a list.
from random import shuffle
import time

#Create canvas and assign it to variable root.
root = Tk()

#Making photo accessible
def stimage(file):
    return PhotoImage(width=100,height=100,file=file)

clicks=[]

clickCount=0
faceUp=[]
score=0

back = PhotoImage(width=100, height=100, file="blank space.gif")
images = [stimage("homer.gif"), stimage("ducky.gif"), stimage("hulk.gif"), stimage("torus.gif"), stimage("ronaldo-moth.gif"), stimage("beyonce.gif"), stimage("monkey.gif"), stimage("kim.gif")]

#Function returns a button, equipped with a command that changes its background colour. It takes the argument image.
def button(n):
    #Create a button with parent root, and a red colour for image. This will be the back of each card.
    button = Button(root,height=100,width=100, image=back)

    #Command for button. Flips over when clicked.
    def flip():
        global clickCount
        global faceUp
        clickCount+=1
        #Make the card display an image.
        button.config(image=images[n])
        #If image of button is displayed, append to list faceUp
        if button.cget("image")!=back:
            #Append the button object and image index n to list faceUp
            faceUp.append([button,n])
        if clickCount==2:
            clickCount=0
            if faceUp[0][1]==faceUp[1][1]:
                print("Hello")
            else:
                button.config(image=back)
            faceUp=[]

    button.config(command=flip)
    return button

#Create a list of coordinates that the buttons will occupy.
coord = [[a,b] for a in range(1,5) for b in range(1,5)]
#Randomise coordinates so buttons appear in random places.
random.shuffle(coord)
buttons=[]

for i in range(8):
    buttons.append(button(i))
    buttons[2*i].grid(row=coord[i][0], column=coord[i][1])
    buttons.append(button(i))
    buttons[2*i+1].grid(row=coord[i+8][0], column=coord[i+8][1])
</code></pre>

<p>It doesn't work because if you click a non-matching pair, the button just fails to show its image. </p>

<p>Could anyone help?</p>

<p>you need to create an ifelse statement that checks for the legality of the move, if it is legal, continue with script, if else give the user a message but loop it back to the user guess.  </p>

python python-3.x tkinter