作者:金良([email protected]) csdn博客: http://blog.csdn.net/u012176591
最近用Tkinter编写了一个GUI界面。
源代码:
#coding=utf8
import Tkinter
import tkFileDialog
import numpy as np
from Tkinter import *
import excelreadlib
import libbody
import matplotlib.pyplot as plt
mylist = ['NULL','NULL'] #全局变量,函数内可改变,在函数外也可用
normdata = 0
leveldata = 0
error = 0
def detectionaction():
global normdata
global leveldata
global error
mylist[0] = float(feet.get())
text.insert(END, "\n\n\n\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n")
text.insert(END, "\n$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n\n")
text.insert(END, "\n\t**点击“计算”按钮**\n\n")
text.insert(END, ">> 异常数据占比(%):\t\t\t "+str(mylist[0])+"\n\n")
text.insert(END, ">> 数据文件路径:\t\t\t "+mylist[1].encode('utf-8')+"\n\n")
if (mylist[1].endswith('.xlsx'))==0:
text.insert(END, ">> 错误:\t\t\t数据文件的后缀必须为“.xlsx”\n\n")
return
metadata = excelreadlib.excelread(mylist[1])
text.insert(END,">> 数据为"+str(metadata.shape[0])+"个专家对"+str(metadata.shape[1])
+"个实验室的评审情况,包含了"+str(metadata.shape[0]*metadata.shape[1])+"个评审数据\n\n")
K = int(1.0*mylist[0]*metadata.shape[0]*metadata.shape[1]/100)
text.insert(END, ">> 异常数据个数:\t\t\t "+str(K)+"\n\n")
text.insert(END, "\n\t进入分析步骤....\n\n")
# 现在掌握的数据包括数据文件路径mylist[1],要给出的异常数据的个数,和原始数据矩阵 metadata,这是个ndarray类型的二维数组。
#函数getnormvsextent(metadata),输入原始数据,返回归一化的数据normdata和异常程度数据leveldata,都是二维数组
#函数getK(leveldata,k),返回K个异常数据的位置和异常程度值Klocvalue
#函数modify(leveldata,Klocvalue),返回K个修正值
#函数greyred(leveldata,error)作灰度图
normdata,leveldata = libbody.getnormvsextent(metadata)
text.insert(END, ">> 归一化操作....\n\n")
text.insert(END, ">> 异常程度计算....\n\n")
error = libbody.getK(leveldata,K)
text.insert(END, ">> TopK计算....\n\n")
text.insert(END, ">> 打印TopK异常值的位置和异常程度值,如下:\n\n")
text.insert(END, "-------------------------------------------\n")
for i in error:
text.insert(END, " ||"+str(i[0])+"\t"+str(i[1])+"\t"+str(i[2])+"||\n")
text.insert(END, "-------------------------------------------\n")
def modifyaction():
global normdata
global error
text.insert(END, ">> 基于CF算法的异常值修正,结果如下:\n\n")
text.insert(END, " 修正前对应位置的归一化数据:\n\n")
for i in error:
text.insert(END, " ||"+str(i[0])+"\t"+str(i[1])+"\t"+str(normdata[i[0]][i[1]])+"||\n")
text.insert(END, "-------------------------------------------\n")
text.insert(END, " 修正后对应位置的归一化数据:\n\n")
#########
err = libbody.modify(normdata,error)
#########
text.insert(END, "-------------------------------------------\n")
for i in err:
text.insert(END, " ||"+str(i[0])+"\t"+str(i[1])+"\t"+str(i[2])+"||\n")
text.insert(END, "-------------------------------------------\n")
def pictureaction():
global leveldata
libbody.greyred(leveldata,error)
text.insert(END, ">> 作图完毕,请到工作目录下查看\n")
def choosefilefunc():
try:
value = float(feet.get())
fileName = tkFileDialog.askopenfilename()
mylist[0] = value
mylist[1] = fileName
bt.config(text=fileName[-25:]) #将路径按钮上显示的字替换成文件路径
except ValueError:
text.insert(END, ">> 参数有误\n\n")
root = Tkinter.Tk()
root.title("异常数据检测工具")
############3
def myexit():
root.destroy()
root.protocol('WM_DELETE_WINDOW', myexit)
###########3
feet = Tkinter.StringVar()
# create and pack containers
top = Tkinter.Frame(root)
bottom = Tkinter.Frame(root)
top.pack(fill="x")
#bottom.pack(expand="yes", fill="both")
bottom.pack(expand='yes')
# just use pack on buttons on top
label = Tkinter.Label(top, text='请选择数据文件:',width = 22)
label.grid(row=2,column=1,sticky=(W, E))
bt = Tkinter.Button(top, text='路径',command = choosefilefunc,width = 22)
bt.grid(row=2,column=2,sticky=(W, E))
Tkinter.Label(top, text='异常数据占比(%):').grid(row=1,column=1,sticky=(W, E))
feet_entry = Tkinter.Entry(top,width=7,textvariable=feet)
feet_entry.grid(row=1,column=2, sticky=(W, E))
feet_entry.focus()
bt2 = Tkinter.Button(top, text ="检测分析....",command = detectionaction)
bt2.grid(row=3,column=2,sticky=(W, E))
bt3 = Tkinter.Button(top, text ="修正异常值....",command = modifyaction)
bt3.grid(row=4,column=2,sticky=(W, E))
bt4 = Tkinter.Button(top, text ="生成图像....",command = pictureaction)
bt4.grid(row=5,column=2,sticky=(W, E))
for child in top.winfo_children():
child.grid_configure(padx=5, pady=5)
# use grid on the bottom
scrollbar = Scrollbar(bottom)
scrollbar.pack(side=RIGHT, fill=Y)
text = Tkinter.Text(bottom, wrap=WORD, yscrollcommand=scrollbar.set,width = 44)
text.pack()
text.insert(END, "============================================\n\t\t 操作日志:\n============================================\n")
scrollbar.config(command=text.yview)
root.mainloop()