brewersfan1976

ch7-ex1.py

Aug 18th, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. # this program gets stock symbols and stock quotes from the user
  2. # it stores them in a form of a dictionary and displays a bar graph
  3. # based on each stock's value
  4.  
  5. # create the dictionary to store the data
  6.  
  7. d = {}
  8.  
  9. # set barCount to 0
  10.  
  11. barCount = 0
  12.  
  13. # get stock symbol from the user
  14.  
  15. key = raw_input("Please enter a stock symbol (max of 10) or * to quit: ")
  16.  
  17. # if user enter * for a key then quite the program
  18.  
  19. if (key == "*"):
  20.    quit()
  21.  
  22. barCount = barCount + 1
  23.  
  24. # get stock quote from the user
  25.  
  26. value = raw_input("Please enter stock quote (max of 200 dollars): ")
  27.  
  28. # store the key and value in the dictionary
  29.  
  30. d[key] = value
  31.  
  32. # while key is not * repeat the process
  33.  
  34. while (key != "*"):
  35.       key = raw_input("Please enter a stock symbol (max of 10) or * to quit: ")
  36.    
  37.       if (key == "*"):
  38.          break
  39.  
  40.       barCount = barCount + 1
  41.  
  42.       value = raw_input("Please enter a stock quote (max of 200 dollars): ")
  43.       d[key] = value
  44.  
  45.  
  46. # we will now create the bar graph based on the user input
  47.  
  48. from Tkinter import *
  49.  
  50. root = Tk()
  51. width = 600
  52. height = 500
  53.  
  54. canvas = Canvas(root, bg='#d0d0d0', height = 500, width = 600)
  55.  
  56. # set up bar chart variables
  57.  
  58. upperX       = 40
  59. upperY       = 40
  60. deltaX       = 10
  61. deltaY       = 10
  62. barWidth     = 20
  63. barHeight    = 0
  64. maxBarHeight = 200
  65. lineWidth    = 3
  66. lineColor    = "#000000"
  67. rectColor    = ""
  68.  
  69. # main loop
  70. # render horizontal axis
  71.  
  72. lId = canvas.create_line(upperX - deltaX,
  73.                          upperY + maxBarHeight + deltaY,
  74.                          upperX + 2 * deltaX + barCount * barWidth,
  75.                          upperY + maxBarHeight + deltaY,
  76.                          fill = lineColor, width = lineWidth)
  77.  
  78. # render vertical axis
  79.  
  80. lId = canvas.create_line(upperX - deltaX,
  81.                          upperY - deltaY,
  82.                          upperX - deltaX,
  83.                          upperY + maxBarHeight +deltaY,
  84.                          fill = lineColor, width = lineWidth)
  85.  
  86. # render bar elements
  87.  
  88. # set bar number to 0
  89.  
  90. b = 0
  91.  
  92. # set rectColor to the initial color
  93.  
  94. rectColor = "#FF0000"
  95.  
  96. for key in d:
  97.     upperLX = upperX + b * barWidth
  98.     upperLY = upperY
  99.     barHeight = float(d[key])
  100.  
  101.     rId = canvas.create_rectangle(upperLX,
  102.                                   upperLY + (maxBarHeight - barHeight),
  103.                                   upperLX + barWidth,
  104.                                   upperLY + maxBarHeight,
  105.                                   fill = rectColor, width = 1)
  106.  
  107.     b = b + 1
  108.  
  109.     # change rectangle color to the other color to rotate colors
  110.    
  111.     if (rectColor == "#FF0000"):
  112.        rectColor = "#FFFF00"
  113.     else:
  114.        rectColor = "#FF0000"
  115.  
  116.    
  117.     print "stock symbol: ", key, "\tstock value: ", d[key]  
  118.                                
  119. canvas.pack()
  120. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment