from graphics import *
from gamelibrary import Button
win = Graphwin('Testing Button', 400, 400)
myButton = Button(Point(200-hw, 200- hh), Point(200+hw , 200 +hh), 'Click ME!', )
colors = ['purple','indianred','gold','darkblue','pink']
i = 0
while True:
p = win.checkMouse()
key = win.checkKey()
if myButton.clicked(p):
#switch colors of the background
win.setBackground(colors[i % 5])
i += 1
if key == 'q':
break
win.close()
~~~~~~~~~~~~~~~~~Button that Changes colors~~~~~~~~~~~~~~~~~~~~~~~~~
#Objective: Create a button that when clicked randomly changes the color of the background
from graphics import *
from random import randint
from gamelibrary import clicked
#we take from a different saved py and use the function clicked from it
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Window~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
win = GraphWin('Bootin', 400, 400)
win.setCoords(-10, -10, 10, 10)
win.setBackground('black')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Button~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
hw, hh = 2, 1
ram = randint(-10, 10)
button = Circle(Point(0,ram),3)
button.setFill('pink')
button.draw(win)
for i in range(5):
p = win.getMouse()
if clicked(p, button):
r, g, b = randint(0, 255),randint(0, 255),randint(0, 255)
win.setBackground(color_rgb(r, g, b))
win.close()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Button Class~~~~~~~~~~~~~~~~~~#
class Button :
def _int_(self, point1, point2, label, win):
self.shape = Rectangle(point1, point2) #self.shape the self means it am saving this action for latter use
self.shape.setFill('gray')
self.shape.draw(win)
x1, x2 = point1.getX(),point2.getX()
y1, y2 = point1.getY(), point2.getY()
def clicked(p, button):
if p == None:
return False
else:
x, y = p.getX(), p.getY()
plo, phi = button.getP1(), button.getP2()
xlo, ylo = plo.getX(), plo.getY()
xhi, yhi = phi.getX(), phi.getY()
return (xlo < x ) and (x < xhi) and (ylo < y) and (y < yhi) and self.activate
self.width = x2 - x1
self.height = y2 - y1
xc, yc = (x1 + x2)/2, (y1 +y2)/2
self.name = label
self.label = Text
def activate(self):
self.active = True
self.shape.setFill('gray')
self.shape.setOutline('darkgrey')
self.label.setFill('darkgrey')
def deactivate(self):
return True
Comments
Displaying 0 of 0 comments ( View all | Add Comment )