Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas
- import matplotlib.pyplot as plt
- from matplotlib.font_manager import FontProperties
- from tkinter import *
- from tkinter import filedialog
- import matplotlib
- from PIL import Image,ImageTk
- win = Tk()
- win.title('week8_csv_GUI')
- win.state("zoomed")
- filename = ''
- st = ''
- industry_dic = {}
- money_dic = {}
- def func2():
- global filename
- global st
- global st1
- global st2
- filename = filedialog.askopenfilename(initialdir = "/",title = "Select file")
- plt.rcParams['font.sans-serif'] = ['Microsoft JhengHei']
- plt.rcParams['axes.unicode_minus'] = False
- global industry_dic
- global money_dic
- #read file
- with open(filename,"r",encoding="utf-8") as file:
- reader = csv.reader(file)
- header_col=next(reader)
- for col in reader:
- if col[4] in industry_dic.keys():
- industry_dic[col[4]] += 1
- else:
- industry_dic[col[4]] = 1
- if col[4] in money_dic.keys():
- money_dic[col[4]] += int(col[5])
- else:
- money_dic[col[4]] = 0
- #直條圖
- plt.figure(figsize=(10,10))
- label = list(industry_dic.keys())
- quantity = list(industry_dic.values())
- index = list(range(len(label)))
- plt.barh(label,quantity)
- plt.yticks(index,label)
- plt.xlabel('公司數')
- plt.title('各產業公司數')
- plt.savefig('./bar.png')
- #圓餅圖
- plt.figure(figsize=(10,10))
- label = list(money_dic.keys())
- revenue = list(money_dic.values())
- plt.pie(revenue, labels = label, autopct = "%1.1f%%",
- textprops = {"fontsize" : 7},labeldistance = 1.02)
- plt.title('各產業當月營收百分比',fontsize = 10)
- plt.axis("off")
- plt.savefig('./pie.png')
- st1 = list(industry_dic.keys())
- st2 = list(money_dic.values())
- def main1():
- st = ''
- for name in st1:
- st = st + name + '\n'
- var.set(st)
- def main2():
- st = ''
- for m in st2:
- st = st + str(m) + '\n'
- var.set(st)
- var2.set(filename)
- def bar():
- global myImage
- myImage = ImageTk.PhotoImage(Image.open('./bar.png'))
- myLabel = Label(image = myImage).place(x=500, y=50, anchor='nw')
- def pie():
- global myImage
- myImage = ImageTk.PhotoImage(Image.open('./pie.png'))
- myLabel = Label(image = myImage).place(x=500, y=50, anchor='nw')
- btn = Button(win, text='主要欄位1',command=main1).place(x=20, y=20, anchor='nw')
- btn1 = Button(win, text='主要欄位2',command=main2).place(x=20, y=70, anchor='nw')
- label = Label(win, textvariable=main1,
- font=('標楷體', 15)).place(x=20, y=20, anchor='nw')
- label1 = Label(win, textvariable=main2,
- font=('標楷體', 15)).place(x=20, y=80, anchor='nw')
- var = StringVar()
- label2 = Label(win, textvariable=var,
- font=('標楷體', 15)).place(x=20, y=120, anchor='nw')
- var2 = StringVar()
- label3 = Label(win, textvariable=var2,
- font=('標楷體', 15)).place(x=100, y=800, anchor='nw')
- btn2 = Button(win, text='選檔案',command=func2,
- font=('標楷體', 20), width=20, height=3).place(x=20, y=850, anchor='nw')
- btn3 = Button(win, text='長條圖',command=bar,
- font=('標楷體', 20), width=20, height=3).place(x=800, y=850, anchor='nw')
- btn4 = Button(win, text='圓餅圖',command=pie,
- font=('標楷體', 20), width=20, height=3).place(x=1200, y=850, anchor='nw')
- win.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement