Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

graphics.py how to make a button

from graphics import *

WINDOW_WIDTH, WINDOW_HEIGHT = 200, 150

win = GraphWin("Simple Breakout", WINDOW_WIDTH, WINDOW_HEIGHT)

def buttons():
    left = Rectangle(Point(25, 55), Point(55, 85))  # points are ordered ll, ur
    right = Rectangle(Point(145, 55), Point(175, 85))
    quit = Rectangle(Point(85, 116), Point(115, 146))

    left.setFill("red")
    right.setFill("green")
    text = Text(Point(100, 133), "Exit")
    text.draw(win)

    left.draw(win)
    right.draw(win)
    quit.draw(win)

    return left, right, quit

def inside(point, rectangle):
    """ Is point inside rectangle? """

    ll = rectangle.getP1()  # assume p1 is ll (lower left)
    ur = rectangle.getP2()  # assume p2 is ur (upper right)

    return ll.getX() < point.getX() < ur.getX() and ll.getY() < point.getY() < ur.getY()

left, right, quit = buttons()

centerPoint = Point(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2)
text = Text(centerPoint, "")
text.draw(win)

while True:
    clickPoint = win.getMouse()

    if clickPoint is None:  # so we can substitute checkMouse() for getMouse()
        text.setText("")
    elif inside(clickPoint, left):
        text.setText("left")
    elif inside(clickPoint, right):
        text.setText("right")
    elif inside(clickPoint, quit):
        break
    else:
        text.setText("")

win.close()
Comment

PREVIOUS NEXT
Code Example
Python :: Fibonacci series up to n python 
Python :: .replace pandas in for loop 
Python :: how to sort subset of rows in pandas df 
Python :: python word encode asci 
Python :: are there learning activities for django-debug-toolbar 
Python :: figure in matplotlib 
Python :: how to uninstall python 
Python :: bst deleting in python 
Python :: calculate the R^2 for X and Y python 
Python :: Django Redirect Depending On Request Method 
Python :: Flask / Python. Get mimetype from uploaded file 
Python :: decision tree algorithm 
Python :: django give access to media folder 
Python :: searching for best k values in knn 
Python :: python serial COM3 
Python :: pascal triangle 
Python :: python random number between 0 and 1 
Python :: numpy filter based on value 
Python :: python mongodb docker 
Python :: create feature dataset arcpy 
Python :: Using Python Permutations to Find the order in lexicographical sorted order 
Python :: how to pass csrf token in post request django 
Python :: 2d array python 
Python :: python json nan 
Python :: get Fiscal year 
Python :: iterrows pd 
Python :: repeat a condition n times one by one python 
Python :: how to implement heap in python 
Python :: maximun row and columns in python 
Python :: sleep your computer python 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =