Hello, im starting a new project, about the game who wants to be a millionaire, so In the beggining I have a Start Button, but I want to know how to set the parameters of the mouse click to be only within the range of the START button.

heres what I got so far:

from graphics import *

win = GraphWin("Who wants to be a millionaire", 600, 400)
win.setBackground('Black')

# First Greeting
centertxt = Point(300,100)
title1 = Text(centertxt, "Who wants to be a Millionaire GAME!")
title1.setSize(24)
title1.setStyle('bold')
title1.setTextColor('White')
title1.draw(win)

# Start Button

StartRect = Rectangle(Point(220,200),Point(400,250))
StartRect.draw(win)
StartRect.setOutline('White')
centerstart = Point(310,225)
starttxt = Text(centerstart, "START")
starttxt.setSize(18)
starttxt.setStyle('bold')
starttxt.setTextColor('White')
starttxt.draw(win)

win.getMouse(centerstart)

Recommended Answers

All 5 Replies

You can use this button class to create a button with all the methods needed without hard coding every button.

# button.py
#    A simple Button widget.

from graphics import *

class Button:

    """A button is a labeled rectangle in a window.
    It is activated or deactivated with the activate()
    and deactivate() methods. The clicked(p) method
    returns true if the button is active and p is inside it."""

    def __init__(self, win, center, width, height, label):
        """ Creates a rectangular button, eg:
        qb = Button(myWin, Point(30,25), 20, 10, 'Quit') """ 

        w,h = width/2.0, height/2.0
        x,y = center.getX(), center.getY()
        self.xmax, self.xmin = x+w, x-w
        self.ymax, self.ymin = y+h, y-h
        p1 = Point(self.xmin, self.ymin)
        p2 = Point(self.xmax, self.ymax)
        self.rect = Rectangle(p1,p2)
        self.rect.setFill('lightgray')
        self.rect.draw(win)
        self.label = Text(center, label)
        self.label.draw(win)
        self.deactivate()

    def clicked(self, p):
        "Returns true if button active and p is inside"
        return self.active and \
               self.xmin <= p.getX() <= self.xmax and \
               self.ymin <= p.getY() <= self.ymax

    def getLabel(self):
        "Returns the label string of this button."
        return self.label.getText()

    def activate(self):
        "Sets this button to 'active'."
        self.label.setFill('black')
        self.rect.setWidth(2)
        self.active = True

    def deactivate(self):
        "Sets this button to 'inactive'."
        self.label.setFill('darkgrey')
        self.rect.setWidth(1)
        self.active = False

thanks for the help, but im sorry for being so ignorant in the topic, but that class, self and def __init__ commands are kind of new to me, so I dont understand very much what you just did

Think of this class as the template to build a button, you just copy and paste the above code into a file and call it something like "button.py". The sample code below is an example of how to create a button from Button class, and using the different methods.

from graphics import *
from button import Button

win = GraphWin("Button Test", 200, 100)

testButton = Button(win,Point(100,50),100,20,'TEXT GOES HERE')
testButton.label.setSize(8)
testButton.activate()

pt = win.getMouse()
if testButton.clicked(pt):
    #If clicked deactivate the button
    testButton.deactivate()
    print ('button clicked')
    
win.close()

ok that worked just fine, but im trying to make a "who wants to be a millionaire" sort of game, so how can I use this example and turn it in to a window that has the question with 4 buttons, 3 of them will be wrong answer, and the other one the correct answer?

ok that worked just fine, but im trying to make a "who wants to be a millionaire" sort of game, so how can I use this example and turn it in to a window that has the question with 4 buttons, 3 of them will be wrong answer, and the other one the correct answer?

Try to create several buttons, with different parameters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.