Detect if you click inside a box in Python Zelle graphics

2024/10/6 3:55:13

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()
Answer

I'd suggest using a gui framework like tkinter

import tkinter root = tkinter.tk()
def on_click():print('clicked')btn = tkinter.Button(root, command=on_click)
root.mainloop()
https://en.xdnf.cn/q/120192.html

Related Q&A

Nested Triangle in Python

My assingmentAt each level the complete triangle for the previous level is placed into an extra outer triangle. The user should be asked to input the two characters to be used and the width of the inne…

How to fix cannot open file Test.py: [Errno2] No such file or directory?

I tried to run a python script which does not exist in current folder, for exampleC:\>python Test.pypython:cant open file Test.py:[Errno2] No such file or directoryI have to specify the absolute pat…

Highlight cells in a column in google spreadsheet when the value above a threshold with python

Here is a simplified example of my codes and the screenshot of the results I want to get in google spreadsheet. I hope to either save the dataframe style to google spreadsheet as applying table style t…

cannot concatenate str and file objects : Python error

I have Following piece of code: for src_filename, src_code in src_dict.iteritems(): try: set.dependencies = subprocess.check_output(unifdef -s /home/c/maindir/folder/ +src_filename, shell=True) except…

Run python script from html button submit

i have a code input data to txt file :<form action="proses.php" method="post">Nomor Polisi : <br><input type="text" name="nopol"><br><…

Reading log files in python

I have a log file (it is named data.log) containing data that I would like to read and manipulate. The file is structured as follows:#Comment line 1 #Comment line 2 1.00000000,3.02502604,343260.6865…

Return more than one value in python function [duplicate]

This question already has answers here:How can I use `return` to get back multiple values from a loop? Can I put them in a list?(2 answers)Closed 1 year ago.I was trying to use *args with a for loop …

Python: Making a class to use complex numbers

I am currently trying to write a program that deals with complex numbers. I have to use classes and methods. I am trying to be able to add, subtract, multiply etc., complex numbers, as well as compare …

Return does not return anything in Spyder. It works fine with other IDE

I just moved to spyder for Python and the return function doesnt seem to work: def test():return 2 test()The IPython console is empty. If I use print instead of return it works fine. Any idea? I use p…

Error in goto module [Python]

Ok, let me start by saying I know that it is bad that I am using the goto module and I shouldnt be and blah blah blah. However, for this specific purpose I need it. Let me also say that I am new to Pyt…