def main(): # create the application window win = GraphWin("Dice Roller") win.setCoords(0, 0, 10, 10) win.setBackground("green2") # Draw the interface widgets die1 = DieView(win, Point(3, 7), 2) die2 = DieView(win, Point(7, 7), 2) rollButton = Button(win, Point(5, 4.5), 6, 1, "Roll Dice") rollButton.activate() quitButton = Button(win, Point(5, 1), 2, 1, "Quit") # Event loop pt = win.getMouse() while not quitButton.clicked(pt): if rollButton.clicked(pt): # pick a random color color = color_rgb(randrange(256), randrange(256), randrange(256)) # set dice to have new color die1.setColor(color) die2.setColor(color) value1 = randrange(1, 7) die1.setValue(value1) value2 = randrange(1, 7) die2.setValue(value2) quitButton.activate() pt = win.getMouse() # close up shop win.close()
def __createButtons(self): # create list of buttons # start with all the standard sized buttons # bSpecs gives center coords and label of buttons bSpecs = [(2, 1, '0'), (3, 1, '.'), (1, 2, '1'), (2, 2, '2'), (3, 2, '3'), (4, 2, '+'), (5, 2, '-'), (1, 3, '4'), (2, 3, '5'), (3, 3, '6'), (4, 3, '*'), (5, 3, '/'), (1, 4, '7'), (2, 4, '8'), (3, 4, '9'), (4, 4, '<-'), (5, 4, 'C')] self.buttons = [] for (cx, cy, label) in bSpecs: self.buttons.append( Button(self.win, Point(cx, cy), .75, .75, label)) # create larger "=" button self.buttons.append(Button(self.win, Point(4.5, 1), 1.75, .75, "=")) # activate all buttons for b in self.buttons: b.activate()
def main(): # infile = input("Enter the name of the data file: ") infile = 'students' # Open Graphics Window win = GraphWin("Arrange Student File", 800, 800) win.setCoords(0, 0, 5, 6) message = Text(Point(2.5, 3), "This program sorts student grade information by GPA, name, or credits.\n Select the sorting method you would like.") message.draw(win) # Create Buttons for GPA, Name, and Credits bSpecs = [(1, 2, "GPA"), (2, 2, "Name"), (3, 2, "Credits"), (4, 2, "Quit")] buttons = [] for (cx, cy, label) in bSpecs: buttons.append(Button(win, Point(cx, cy), .75, .75, label)) # ....activate for b in buttons: b.activate() while True: click = win.getMouse() # return clicked for each button for b in buttons: if b.clicked(click): label = b.getLabel() while label != "Quit": if label == "GPA": x = "GPA" m = "D" break elif label == "Name": x = "name" m = "A" break elif label == "Credits": x = "credits" m = "D" break elif label == "Quit": break else: message.setText("Please try again.") click = win.getMouse() # call sort function, which should return filename outfile, data = sort(infile, x, m) writeStudents(data, outfile) outmessage = "The file has been printed to " + outfile message.setText(outmessage) if label == "Quit": break
def main(): # Set up interface win = GraphWin("Extended Three Button Monte", 350, 350) win.setCoords(.5, -2, 3.5, 3) b1 = Button(win, Point(1, 2), .75, 1, "Door 1") b1.activate() b2 = Button(win, Point(2, 2), .75, 1, "Door 2") b2.activate() b3 = Button(win, Point(3, 2), .75, 1, "Door 3") b3.activate() again = Button(win, Point(1.25, 0), 1, .75, "Play Again") quit = Button(win, Point(2.75, 0), 1, .75, "Quit") mess = Text(Point(2, .75), "Guess a door") mess.setStyle("bold") mess.draw(win) scoreBox = Text(Point(2, -1), "") scoreBox.draw(win) playAgain = True hits = 0 misses = 0 while playAgain: mess.setText("Guess a door") pick = getDoorPick(win, b1, b2, b3) hits, misses = updateScore(pick, mess, scoreBox, hits, misses) playAgain = quitOrPlay(win, quit, again) win.close()
def main(): win = GraphWin("Three Button Monte", 350, 100) win.setCoords(.5, 0, 3.5, 3) b1 = Button(win, Point(1, 2), .75, 1, "Door 1") b1.activate() b2 = Button(win, Point(2, 2), .75, 1, "Door 2") b2.activate() b3 = Button(win, Point(3, 2), .75, 1, "Door 3") b3.activate() mess = Text(Point(2, .75), "Guess a door.") mess.setStyle("bold") mess.draw(win) secret = randrange(1, 4) choice = None while choice == None: pt = win.getMouse() for button in [b1, b2, b3]: if button.clicked(pt): choice = button choiceNum = int(choice.getLabel()[-1]) if choiceNum == secret: mess.setText("You win!") else: mess.setText("You lose. The answer was door {0}.".format(secret)) win.getMouse() win.close()