I have created a dice betting game. When my code is run, there is a "CLICK TO ROLL" button. Currently, if you click anywhere on the screen, the dice will roll. How can I make it so the program closes if you click anywhere outside the box, and only if you click inside the box the dice rolls?
import graphics
from graphics import *
from random import randrange
max_x = 500
max_y = 300
win = GraphWin("Dice Rolls", max_x,max_y)
#drives the program
def main():class dots():#Drawn dotsdef __init__(self,p_x=0,p_y=0,p_s=50):self.m_x = p_xself.m_y = p_yself.m_dice_size = p_sself.m_items = []dot_size = 4x = self.m_xy = self.m_ys = self.m_dice_sized = s/4self.m_items.append(Circle(Point(x+2*d, y+2*d), dot_size))self.m_items.append(Circle(Point(x+1*d, y+1*d), dot_size))self.m_items.append(Circle(Point(x+1*d, y+2*d), dot_size))self.m_items.append(Circle(Point(x+1*d, y+3*d), dot_size))self.m_items.append(Circle(Point(x+3*d, y+1*d), dot_size))self.m_items.append(Circle(Point(x+3*d, y+2*d), dot_size))self.m_items.append(Circle(Point(x+3*d, y+3*d), dot_size))for dot in self.m_items:dot.setFill('black')#Displays three dice images based on the random valuedef display_dice(self):return (self.m_x,self.m_y,self.m_s)def undraw(self):for dot in self.m_items:dot.undraw()#Drawn Dicedef draw(self, p_win, p_num):for dot in self.m_items:dot.undraw()if (p_num==1):self.m_items[0].draw(p_win)elif (p_num==2):self.m_items[3].draw(p_win)self.m_items[4].draw(p_win)elif (p_num==3):self.m_items[0].draw(p_win)self.m_items[3].draw(p_win)self.m_items[4].draw(p_win)elif (p_num==4):self.m_items[1].draw(p_win)self.m_items[3].draw(p_win)self.m_items[4].draw(p_win)self.m_items[6].draw(p_win)elif (p_num==5):self.m_items[0].draw(p_win)self.m_items[1].draw(p_win)self.m_items[3].draw(p_win)self.m_items[4].draw(p_win)self.m_items[6].draw(p_win)elif (p_num==6):self.m_items[1].draw(p_win)self.m_items[2].draw(p_win)self.m_items[3].draw(p_win)self.m_items[4].draw(p_win)self.m_items[5].draw(p_win)self.m_items[6].draw(p_win)#Prepares for next rollclass dice_t:def __init__(self,x=0,y=0):self.m_x = xself.m_y = yself.m_s = 50self.m_item = Rectangle(Point(self.m_x,self.m_y),Point(self.m_x+self.m_s,self.m_y+self.m_s))self.m_item.setFill('white')self.m_dots = dots(self.m_x,self.m_y,self.m_s)def display_dice(self):return (self.m_x,self.m_y,self.m_s)def draw_die(self, p_win):self.m_item.undraw()self.m_item.draw(p_win)def draw(self, p_win, p_num):self.draw_die(p_win)self.m_dots.draw(p_win,p_num)def undraw(self):self.m_item.undraw()self.m_dots.undraw()#Winnings and losing calculationdef check_winner(p_rolls=[]):last = Nonetotal = 0triple = Truefor r in p_rolls:if (last!=None) and (last!=r):triple = Falselast = rtotal += rif (total==3) or (total==18):return 10elif (total==4) or (total==17):return 5elif (triple ==True):return 2return -1#Text and instructions/rulesdef get_bet(p_win,p_balance,p_def_bet):inst = []inst.append(Text(Point(max_x/2,20), "MAKE YOUR BET: "))inst.append(Text(Point(max_x/2,40), "BALANCE:"+str(p_balance)))inst.append(Text(Point(max_x/2,70), "Rules: "))inst.append(Text(Point(max_x/2,90), "If you roll a 3 or 18 in total your bet winnings will be 10x your bet."))inst.append(Text(Point(max_x/2,110), "If you roll a 4 or 17 in total your bet winnings will be 5x your bet."))inst.append(Text(Point(max_x/2,130), "If you roll triples besides a 3 and 18 your bet winnings will be 2x your bet."))inst.append(Text(Point(max_x/2,150), 'If you roll anything else, you lose your bet.'))inst.append(Rectangle(Point(max_x/2-59,190), Point(max_x/2+59, 210)))inst.append(Text(Point(max_x/2, 200), 'CLICK TO ROLL'))for item in inst:item.draw(p_win)bet_text = str(p_def_bet)bet_input = Entry(Point (max_x/2+100, 20),5)bet_input.setText(bet_text)bet_input.draw(p_win)p_win.getMouse()bet_text = bet_input.getText()bet = int(bet_text)bet_input.undraw()for item in inst:item.undraw()return bet#Shows winnings, checks for winner, updates total, and returns the updated totaldef show_winnings(p_win, p_winnings):inst = []inst.append(Text(Point(max_x/2,90), "Your WINNINGS:"+str(winnings)))inst.append(Rectangle(Point(max_x/2-50,190), Point(max_x/2+50, 210)))inst.append(Text(Point(max_x/2, 200), 'PLAY AGAIN'))for item in inst:item.draw(p_win)p_win.getMouse()for item in inst:item.undraw()#Shows betdef show_bet_invalid(p_win):inst = []inst.append(Text(Point(max_x/2,90), "YOUR BET WAS INVALID"))inst.append(Rectangle(Point(max_x/2-50,190), Point(max_x/2+50, 210)))inst.append(Text(Point(max_x/2, 200), 'TRY AGAIN'))for item in inst:item.draw(p_win)p_win.getMouse()for item in inst:item.undraw()#Shows game overdef show_game_over(p_win):inst = []inst.append(Text(Point(max_x/2,90), "YOU ARE OUT OF MONEY"))inst.append(Rectangle(Point(max_x/2-50,190), Point(max_x/2+50, 210)))inst.append(Text(Point(max_x/2, 200), 'QUIT'))for item in inst:item.draw(p_win)p_win.getMouse()for item in inst:item.undraw()# M A I N and balances along with random outputs#################################################dice = []for d in range(0,3):dice.append(dice_t(max_x/2-90+d*60,5))balance = 100def_bet = 10while ( balance > 0 ):bet_invalid = Truewhile (bet_invalid):bet = get_bet(win,balance,def_bet)if (bet>=1) and (bet<=balance):bet_invalid = Falseelse:show_bet_invalid(win)def_bet = betrolls = []for r in range(0,3):roll = randrange(1, 7)dice[r].draw(win,roll)rolls.append(roll)winnings = check_winner(rolls) * betbalance += winningsshow_winnings(win, winnings)for r in range(0,3):dice[r].undraw()show_game_over(win)main()