graphics py how to make a button

Solutions on MaxInterview for graphics py how to make a button by the best coders in the world

showing results for - "graphics py how to make a button"
Gabriel
15 Jun 2019
1from graphics import *
2
3WINDOW_WIDTH, WINDOW_HEIGHT = 200, 150
4
5win = GraphWin("Simple Breakout", WINDOW_WIDTH, WINDOW_HEIGHT)
6
7def buttons():
8    left = Rectangle(Point(25, 55), Point(55, 85))  # points are ordered ll, ur
9    right = Rectangle(Point(145, 55), Point(175, 85))
10    quit = Rectangle(Point(85, 116), Point(115, 146))
11
12    left.setFill("red")
13    right.setFill("green")
14    text = Text(Point(100, 133), "Exit")
15    text.draw(win)
16
17    left.draw(win)
18    right.draw(win)
19    quit.draw(win)
20
21    return left, right, quit
22
23def inside(point, rectangle):
24    """ Is point inside rectangle? """
25
26    ll = rectangle.getP1()  # assume p1 is ll (lower left)
27    ur = rectangle.getP2()  # assume p2 is ur (upper right)
28
29    return ll.getX() < point.getX() < ur.getX() and ll.getY() < point.getY() < ur.getY()
30
31left, right, quit = buttons()
32
33centerPoint = Point(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2)
34text = Text(centerPoint, "")
35text.draw(win)
36
37while True:
38    clickPoint = win.getMouse()
39
40    if clickPoint is None:  # so we can substitute checkMouse() for getMouse()
41        text.setText("")
42    elif inside(clickPoint, left):
43        text.setText("left")
44    elif inside(clickPoint, right):
45        text.setText("right")
46    elif inside(clickPoint, quit):
47        break
48    else:
49        text.setText("")
50
51win.close()
52
similar questions
queries leading to this page
graphics py how to make a button