Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # this program gets stock symbols and stock quotes from the user
- # it stores them in a form of a dictionary and displays a bar graph
- # based on each stock's value
- # create the dictionary to store the data
- d = {}
- # set barCount to 0
- barCount = 0
- # get stock symbol from the user
- key = raw_input("Please enter a stock symbol (max of 10) or * to quit: ")
- # if user enter * for a key then quite the program
- if (key == "*"):
- quit()
- barCount = barCount + 1
- # get stock quote from the user
- value = raw_input("Please enter stock quote (max of 200 dollars): ")
- # store the key and value in the dictionary
- d[key] = value
- # while key is not * repeat the process
- while (key != "*"):
- key = raw_input("Please enter a stock symbol (max of 10) or * to quit: ")
- if (key == "*"):
- break
- barCount = barCount + 1
- value = raw_input("Please enter a stock quote (max of 200 dollars): ")
- d[key] = value
- # we will now create the bar graph based on the user input
- from Tkinter import *
- root = Tk()
- width = 600
- height = 500
- canvas = Canvas(root, bg='#d0d0d0', height = 500, width = 600)
- # set up bar chart variables
- upperX = 40
- upperY = 40
- deltaX = 10
- deltaY = 10
- barWidth = 20
- barHeight = 0
- maxBarHeight = 200
- lineWidth = 3
- lineColor = "#000000"
- rectColor = ""
- # main loop
- # render horizontal axis
- lId = canvas.create_line(upperX - deltaX,
- upperY + maxBarHeight + deltaY,
- upperX + 2 * deltaX + barCount * barWidth,
- upperY + maxBarHeight + deltaY,
- fill = lineColor, width = lineWidth)
- # render vertical axis
- lId = canvas.create_line(upperX - deltaX,
- upperY - deltaY,
- upperX - deltaX,
- upperY + maxBarHeight +deltaY,
- fill = lineColor, width = lineWidth)
- # render bar elements
- # set bar number to 0
- b = 0
- # set rectColor to the initial color
- rectColor = "#FF0000"
- for key in d:
- upperLX = upperX + b * barWidth
- upperLY = upperY
- barHeight = float(d[key])
- rId = canvas.create_rectangle(upperLX,
- upperLY + (maxBarHeight - barHeight),
- upperLX + barWidth,
- upperLY + maxBarHeight,
- fill = rectColor, width = 1)
- b = b + 1
- # change rectangle color to the other color to rotate colors
- if (rectColor == "#FF0000"):
- rectColor = "#FFFF00"
- else:
- rectColor = "#FF0000"
- print "stock symbol: ", key, "\tstock value: ", d[key]
- canvas.pack()
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment