Answered step by step
Verified Expert Solution
Link Copied!

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,id=, row =-1, col =-1,c=) :
self.ID = id
self. ROW = row
self. COL = col
self. COLOR =C
self.BUTTON_OBJ = None
Check if two buttons have been clicked and if they are a match""."
def check_match():
global buttons
clicked =[]
for b in buttons. values():
if b.BUTTON_OB]['relief']== "sunken":
clicked.append (b)
if len(clicked)2 :
if clicked [0].BUTTON_OB]['bg']== clicked[1].BUTTON_OBJ ['bg '] :
# It's a match
clicked [0]. BUTTON_OBJ. configure(fg='black', bg='black', relief='raised ')
clicked [1].BUTTON_OBJ. configure(fg='black', bg='black', relief='raised ')
# black
else:
clicked [0]. BUTTON_OBJ. configure (fg='SystemButtonFace', bg='SystemButtonFace', relief='raised ')
clicked [1].BUTTON_OB]. configure (fg='SystemButtonFace', bg='SystemButtonFace', relief=' raised ')
If a button is pushed, draw the correct colors".".
def button_pushed(pushed_id):
global buttons
buttons [pushed_id].BUTTON_OBJ.configure (bg=buttons [pushed_id].COLOR, 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 [pushed_id].BUTTON_OBJ.after(1500, check_match)
def close_options(top):
top.destroy()
# Create an Entry wiaget in the loplevel window
row = Entry(top, width =25, textvariable=row_val)
row.pack()
col = Entry(top, width =25, textvariable = col_val)
col. pack()
Create a Button to print something in the Entry widget
save = Button(top, text="Save", command=lambda: save_options(int(row_val.get()), int(col_val.get())))
# Create a Button Widget in the Toplevel Window
close = Button(top, text="Close", command=lambda: close_options(top))
save. pack ( pady=5, side=TOP)
close. pack(pady=5, side=TOP)
Resets the game. Will clear, reshuffle and redraw the board
Parameters: buttons - the global list of buttons
def reset_game():
global buttons
# Delete all buttons if any
for b in buttons.values():
b.BUTTON_OBJ.destroy()
buttons ={} # Clear the button dictionary
# Create all new buttons and add them to the button dictionary
for i in range(rows):
for j in range(cols):
id cols
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions