Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In python, how do I modify this so that when the user enters the number of rows and columns it must multiply to be an
In python, how do I modify this so that when the user enters the number of rows and columns it must multiply to be an even number of buttons? Possibly having a messagebox pop up and warn them to this requirement and resetting the fields. Having an odd number of buttons errors out the program.
import time
from tkinter import
from random import randint
class ButtonWrapper:
def initself id row col c:
self.ID id
self.ROW row
self.COL col
self.COLOR c
self.BUTTONOBJ None
Check if two buttons have been clicked and if they are a match"""
def checkmatch:
global buttons
clicked
for b in buttons.values:
if bBUTTONOBJrelief "sunken":
clicked.appendb
if lenclicked:
if clickedBUTTONOBJbg clickedBUTTONOBJbg:
# It's a match
clickedBUTTONOBJ.configurefg'black', bg'black', relief'raised'
clickedBUTTONOBJ.configurefg'black', bg'black', relief'raised'
else:
clickedBUTTONOBJ.configurefg'SystemButtonFace', bg'SystemButtonFace', relief'raised'
clickedBUTTONOBJ.configurefg'SystemButtonFace', bg'SystemButtonFace', relief'raised'
If a button is pushed, draw the color"""
def buttonpushedpushedid:
global buttons
buttonspushedidBUTTONOBJ.configurebgbuttonspushedidCOLOR, 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.
buttonspushedidBUTTONOBJ.after checkmatch
def closeoptionstop:
top.destroy
Save the new Row and Col values to the global game variables"""
def saveoptionsr c:
global rows, cols
rows r
cols c
resetgame
Draw and handle the options menu"""
def options:
# Create a Toplevel window
top Toplevelroot
top.geometryx
rowval StringVar
colval StringVar
# Create an Entry Widget in Toplevel window
row Entrytop width textvariablerowval
row.pack
col Entrytop width textvariablecolval
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.packpady sideTOP
close.packpady sideTOP
def resetgame:
global buttons
# Delete all buttons if any
for b in buttons.values:
bBUTTONOBJ.destroy
buttons # Clear the button dictionary
# Create all new buttons and add them to the button dictionary
for i in rangerows:
for j in rangecols:
id i cols j # Calculate the button id
b ButtonWrapperidstrid rowi colj # Create the button meta data object
# Create the button widget
bBUTTONOBJ Buttonroot text commandlambda bidbID: buttonpushedbid height width
buttonsbID b # Save the button meta data object in the dictionary
# Randomly assign colors to buttons, always matching pairs
ids listrangerows cols # Make a list of all button IDs
while lenids: # While there are still IDs in the list
a idsrandint lenids # Pick a random button ID
ids.removea # Remove it from the list
b idsrandint lenids # Pick a second random button ID
ids.removeb # Remove it from the list
color colorsrandint lencolors # Pick a random color
buttonsstraCOLOR color # Set both buttons to the same color
buttonsstrbCOLOR color
# Set all buttons to default state
for b in buttons.values:
bBUTTONOBJ.configurefg'SystemButtonFace', bg'SystemButtonFace', relief'raised'
bBUTTONOBJ.configurefg'SystemButtonFace', bg'SystemButtonFace', relief'raised'
# Add all buttons to the screen
for b in buttons.values:
bBUTTONOBJ.gridrowbROW, columnbCOL
rows
cols
buttons
root Tk
menubar Menuroot # Creating Menubar
colors red 'green', 'blue', 'cyan', 'yellow', 'magenta'
# Adding File Menu and commands
file Menumenubar tearoff
menubar.addcascadelabel'File', menufile
file.addcommandlabel'New Game', commandresetgame
file.addcommandlabel'Options', commandoptions
file.addseparator
file.addcommandlabel'Exit', commandroot.destroy
resetgame
root.gridrowconfigure minsize
root.configmenumenubar
root.mainloop
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