Answered step by step
Verified Expert Solution
Question
1 Approved Answer
import time from tkinter import * from random import randint class ButtonWrapper: def ( self , i d = , row = - 1 ,
import time
from tkinter import
from random import randint
class ButtonWrapper:
def self row col :
self.ID id
self. ROW row
self. COL col
self. COLOR
self.BUTTONOBJ None
Check if two buttons have been clicked and if they are a match""."
def checkmatch:
global buttons
clicked
for in buttons. values:
if bBUTTONOBrelief "sunken":
clicked.append b
if lenclicked :
if clicked BUTTONOBbg clickedBUTTONOBJ bg :
# It's a match
clicked BUTTONOBJ. configurefg'black', bg'black', relief'raised
clicked BUTTONOBJ. configurefg'black', bg'black', relief'raised
# black
else:
clicked BUTTONOBJ. configure fg'SystemButtonFace', bg'SystemButtonFace', relief'raised
clicked BUTTONOB configure fg'SystemButtonFace', bg'SystemButtonFace', relief raised
If a button is pushed, draw the correct colors".".
def buttonpushedpushedid:
global buttons
buttons pushedidBUTTONOBJ.configure bgbuttons pushedidCOLOR, relief"sunken"
# Because time.sleep blocks execution, the color change for the second button clicked does not take effect until
# the function returns. So we need to use the 'after method
# Parameters:
# parent: is the object of the widget or main window whichever is using this function.
# ms: is the time in milliseconds.
# function: which shall be called.
# args: other options.
buttons pushedidBUTTONOBJ.after checkmatch
def closeoptionstop:
top.destroy
# Create an Entry wiaget in the loplevel window
row Entrytop width textvariablerowval
row.pack
col Entrytop width textvariable colval
col. pack
Create a Button to print something in the Entry widget
save Buttontop text"Save", commandlambda: saveoptionsintrowval.get intcolval.get
# Create a Button Widget in the Toplevel Window
close Buttontop text"Close", commandlambda: closeoptionstop
save. pack pady sideTOP
close. packpady sideTOP
Resets the game. Will clear, reshuffle and redraw the board
Parameters: buttons the global list of buttons
def resetgame:
global buttons
# Delete all buttons if any
for in buttons.values:
bBUTTONOBJ.destroy
buttons # Clear the button dictionary
# Create all new buttons and add them to the button dictionary
for in rangerows:
for in rangecols:
id cols
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started