Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. import pandas
  2. import matplotlib.pyplot as plot
  3. from matplotlib.font_manager import FontProperties
  4. from tkinter import *
  5. from tkinter import filedialog
  6. import matplotlib
  7. from PIL import Image,ImageTk
  8.  
  9. root = Tk()
  10. root.title('第八週GUI')
  11. root.state("zoomed")
  12.  
  13. path = ''
  14. names = []
  15. cnt = []
  16. money = []
  17.  
  18. def main1():
  19. global names
  20. st = ''
  21. for name in names:
  22. st = st + name + '\n'
  23. var.set(st)
  24. def main2():
  25. global money
  26. st = ''
  27. for m in money:
  28. st = st + str(m) + '\n'
  29. var.set(st)
  30. def func2():
  31. global path
  32. path = filedialog.askopenfilename(initialdir = "/",title = "Select file")
  33. plot.rcParams['font.sans-serif'] = ['Microsoft JhengHei']
  34. plot.rcParams['axes.unicode_minus'] = False
  35.  
  36. data = pandas.read_csv(path)
  37.  
  38. global names
  39. global cnt
  40. global money
  41.  
  42. for index, row in data.iterrows():
  43. if row['產業別'] in names:
  44. name = names.index(row['產業別'])
  45. cnt[name] += 1
  46. money[name] += row['營業收入-當月營收']
  47. else:
  48. names.append(row['產業別'])
  49. cnt.append(1)
  50. money.append(row['營業收入-當月營收'])
  51. plot.figure(figsize = [10,10])
  52. plot.barh(range(len(names)), money)
  53. plot.yticks(range(len(names)), names)
  54. plot.savefig('./bar.png')
  55. plot.figure(figsize = [10,10])
  56. plot.pie(money, labels=names)
  57. plot.title("當月營收佔比")
  58. plot.axis('equal')
  59. plot.savefig('./pie.png')
  60.  
  61. var2.set(path)
  62.  
  63. def bar():
  64. global myImage
  65. myImage = ImageTk.PhotoImage(Image.open('./bar.png').resize((600, 600), Image.ANTIALIAS))
  66. myLabel = Label(image = myImage).place(x=300, y=50, anchor='nw')
  67. def pie():
  68. global myImage
  69. myImage = ImageTk.PhotoImage(Image.open('./pie.png').resize((600, 600), Image.ANTIALIAS))
  70. myLabel = Label(image = myImage).place(x=300, y=50, anchor='nw')
  71. btn = Button(root, text='主要欄位1',command=main1).place(x=20, y=20, anchor='nw')
  72.  
  73. btn1 = Button(root, text='主要欄位2',command=main2).place(x=20, y=70, anchor='nw')
  74.  
  75. var = StringVar()
  76. label = Label(root, textvariable=var,).place(x=20, y=120, anchor='nw')
  77.  
  78. var2 = StringVar()
  79. labe2 = Label(root, textvariable=var2,).place(x=120, y=800, anchor='nw')
  80.  
  81. btn2 = Button(root, text='選檔案',command=func2).place(x=20, y=800, anchor='nw')
  82. label4 = Label(root, text='第一次run程式 圖片會bug放大 試試看run第二次',).place(x=20, y=850, anchor='nw')
  83. btn3 = Button(root, text='長條圖',command=bar).place(x=300, y=850, anchor='nw')
  84.  
  85. btn4 = Button(root, text='圓餅圖',command=pie).place(x=500, y=850, anchor='nw')
  86.  
  87.  
  88.  
  89. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement