如何使用tkinter用户输入执行python代码

时间:2016-10-10 01:07:37

标签: python python-2.7 tkinter pycharm cluster-analysis

当用户在此tkinter代码(tkintt.py)中输入群集并单击“提交”按钮时,我希望程序根据用户输入的群集执行k-medoids代码(example.py),但会出现一些错误。 你能救我吗?

tkintt.py

import Tkinter
from _tkinter import *
root = Tkinter.Tk()

label1 = Tkinter.Label(text = " enter cluster : ")
label1.pack()
clvar = Tkinter.IntVar()
cluster = Tkinter.Entry(bd = 5)
clvar = cluster.get()
cluster.pack()

def open1():
  print ("K-MEDOIDS CLUSTERING")
  execfile('example.py')

button_1 = Tkinter.Button(text = "SUBMIT", command = open1)
button_1.pack()
root.mainloop()

example.py

from k_medoids import KMedoids
import numpy as np
import matplotlib.pyplot as plt


def example_distance_func(data1, data2):
   '''example distance function'''
   return np.sqrt(np.sum((data1 - data2)**2))

if __name__ == '__main__':
   X = np.random.normal(0,3,(500,2))
   model = KMedoids(n_clusters= cluster, dist_func=example_distance_func)
   model.fit(X, plotit=True, verbose=True)
   plt.show()

错误:

    Traceback (most recent call last):
    File "C:\Users\user\Anaconda2\lib\lib-tk\Tkinter.py", line 1537, in __call__
      return self.func(*args)
    File "C:/Users/user/Anaconda2/K_Medoids-master/tkintt.py", line 15, in open1
      execfile('example.py')
    File "example.py", line 13, in <module>
      model.fit(X, plotit=True, verbose=True)
    File "C:\Users\user\Anaconda2\K_Medoids-master\k_medoids.py", line 114, in       fit
    X,self.n_clusters, self.dist_func, max_iter=self.max_iter, tol=self.tol,verbose=verbose)
    File "C:\Users\user\Anaconda2\K_Medoids-master\k_medoids.py", line 54, in _kmedoids_run members, costs, tot_cost, dist_mat = _get_cost(X, init_ids,dist_func)
    File "C:\Users\user\Anaconda2\K_Medoids-master\k_medoids.py", line 37, in _get_cost mask = np.argmin(dist_mat,axis=1)
    File "C:\Users\user\Anaconda2\lib\site-packages\numpy\core\fromnumeric.py", line 1034, in argmin return argmin(axis, out)
    ValueError: attempt to get argmin of an empty sequence

1 个答案:

答案 0 :(得分:0)

好像k-medoid没有正确处理角落的情况。

最有可能的是,你有一个空的群集。