Advertisement
jjjj88521

Untitled

Nov 20th, 2019
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. import pandas
  2. import matplotlib.pyplot as plt
  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.  
  10. win = Tk()
  11. win.title('week8_csv_GUI')
  12. win.state("zoomed")
  13.  
  14. filename = ''
  15. st = ''
  16. industry_dic = {}
  17. money_dic = {}
  18.  
  19. def func2():
  20. global filename
  21. global st
  22. global st1
  23. global st2
  24.  
  25. filename = filedialog.askopenfilename(initialdir = "/",title = "Select file")
  26.  
  27. plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei']
  28. plt.rcParams['axes.unicode_minus'] = False
  29.  
  30.  
  31. global industry_dic
  32. global money_dic
  33. #read file
  34. with open(filename,"r",encoding="utf-8") as file:
  35. reader = csv.reader(file)
  36. header_col=next(reader)
  37. for col in reader:
  38. if col[4] in industry_dic.keys():
  39. industry_dic[col[4]] += 1
  40. else:
  41. industry_dic[col[4]] = 1
  42.  
  43. if col[4] in money_dic.keys():
  44. money_dic[col[4]] += int(col[5])
  45. else:
  46. money_dic[col[4]] = 0
  47.  
  48.  
  49. #直條圖
  50. plt.figure(figsize=(10,10))
  51. label = list(industry_dic.keys())
  52. quantity = list(industry_dic.values())
  53. index = list(range(len(label)))
  54. plt.barh(label,quantity)
  55. plt.yticks(index,label)
  56. plt.xlabel('公司數')
  57. plt.title('各產業公司數')
  58. plt.savefig('./bar.png')
  59.  
  60. #圓餅圖
  61. plt.figure(figsize=(10,10))
  62. label = list(money_dic.keys())
  63. revenue = list(money_dic.values())
  64. plt.pie(revenue, labels = label, autopct = "%1.1f%%",
  65. textprops = {"fontsize" : 7},labeldistance = 1.02)
  66. plt.title('各產業當月營收百分比',fontsize = 10)
  67. plt.axis("off")
  68. plt.savefig('./pie.png')
  69.  
  70.  
  71. st1 = list(industry_dic.keys())
  72. st2 = list(money_dic.values())
  73.  
  74. def main1():
  75. st = ''
  76. for name in st1:
  77. st = st + name + '\n'
  78. var.set(st)
  79.  
  80. def main2():
  81. st = ''
  82. for m in st2:
  83. st = st + str(m) + '\n'
  84.  
  85. var.set(st)
  86.  
  87.  
  88. var2.set(filename)
  89.  
  90. def bar():
  91. global myImage
  92. myImage = ImageTk.PhotoImage(Image.open('./bar.png'))
  93. myLabel = Label(image = myImage).place(x=500, y=50, anchor='nw')
  94. def pie():
  95. global myImage
  96. myImage = ImageTk.PhotoImage(Image.open('./pie.png'))
  97. myLabel = Label(image = myImage).place(x=500, y=50, anchor='nw')
  98.  
  99.  
  100.  
  101. btn = Button(win, text='主要欄位1',command=main1).place(x=20, y=20, anchor='nw')
  102.  
  103. btn1 = Button(win, text='主要欄位2',command=main2).place(x=20, y=70, anchor='nw')
  104.  
  105. label = Label(win, textvariable=main1,
  106. font=('標楷體', 15)).place(x=20, y=20, anchor='nw')
  107. label1 = Label(win, textvariable=main2,
  108. font=('標楷體', 15)).place(x=20, y=80, anchor='nw')
  109.  
  110. var = StringVar()
  111. label2 = Label(win, textvariable=var,
  112. font=('標楷體', 15)).place(x=20, y=120, anchor='nw')
  113.  
  114. var2 = StringVar()
  115. label3 = Label(win, textvariable=var2,
  116. font=('標楷體', 15)).place(x=100, y=800, anchor='nw')
  117.  
  118. btn2 = Button(win, text='選檔案',command=func2,
  119. font=('標楷體', 20), width=20, height=3).place(x=20, y=850, anchor='nw')
  120.  
  121. btn3 = Button(win, text='長條圖',command=bar,
  122. font=('標楷體', 20), width=20, height=3).place(x=800, y=850, anchor='nw')
  123.  
  124. btn4 = Button(win, text='圓餅圖',command=pie,
  125. font=('標楷體', 20), width=20, height=3).place(x=1200, y=850, anchor='nw')
  126.  
  127.  
  128. win.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement