#!/usr/bin/env python """ program to draw horizontal bars in class on Oct 2 - Jim M """ from graphics import * xMax = 300 # size of window in pixels yMax = 300 window = GraphWin("bars", xMax, yMax) # Read in the data filename = "bars_input.txt" infile = open(filename, 'r') n_students = eval(infile.readline()) # print "the number of students is", n_students # print "that variable is a ", type(n_students) text_offset = 50 # pixels from the left top_border = 50 # start this far down from the top spacing = 20 # pixels between bars bar_offset = 100 # left side of bar bar_max = 180 # maximum length of bar in pixels score_max = 100 # highest possible score pix_per_score = 1.0 * bar_max / score_max bar_width = 10 # in pixels for i in range(n_students): line = infile.readline() who, number = line.split() y_position = top_border + i * spacing where_text = Point(text_offset, y_position) text = Text(where_text, who) text.draw(window) bottom_left = Point( bar_offset , y_position ) number = eval(number) # convert from string to number # print "pix_per_score = ", pix_per_score # print "number = ", number # print "type(number) = ", type(number) top_right = Point( bar_offset + number * pix_per_score , \ y_position + bar_width ) bar = Rectangle(bottom_left, top_right) bar.setFill('black') bar.draw(window) y = window.getMouse() # wait for the user to click