By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,964 Members | 1,290 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,964 IT Pros & Developers. It's quick & easy.

how to execute python code with tkinter user input

P: 2
when user input in this tkinter code (tkintt.py) and click submit button, i want the program execute k-medoids code (example.py) based on how many cluster that user input but it gets some errors. could you help me?

tkintt.py

Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2. from _tkinter import *
  3. root = Tkinter.Tk()
  4.  
  5. label1 = Tkinter.Label(text = " enter cluster : ")
  6. label1.pack()
  7. clvar = Tkinter.IntVar()
  8. cluster = Tkinter.Entry(bd = 5)
  9. clvar = cluster.get()
  10. cluster.pack()
  11.  
  12. def open1():
  13.   print ("K-MEDOIDS CLUSTERING")
  14.   execfile('example.py')
  15.  
  16. button_1 = Tkinter.Button(text = "SUBMIT", command = open1)
  17. button_1.pack()
  18. root.mainloop()
example.py

Expand|Select|Wrap|Line Numbers
  1. from k_medoids import KMedoids
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5.  
  6. def example_distance_func(data1, data2):
  7.    '''example distance function'''
  8.    return np.sqrt(np.sum((data1 - data2)**2))
  9.  
  10. if __name__ == '__main__':
  11.    X = np.random.normal(0,3,(500,2))
  12.    model = KMedoids(n_clusters= cluster, dist_func=example_distance_func)
  13.    model.fit(X, plotit=True, verbose=True)
  14.    plt.show()


error :

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
Oct 10 '16 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 613
Nothing in example.py will execute when called from another program. If __name__ == "__main__" will only execute when the program is run from the command line. You would have to do something like call example_distance_func from the calling program. See ghostdog74's reply here https://bytes.com/topic/python/answe...-python-script To receive the data returned by example_distance_func use subprocess.check_output() https://pymotw.com/2/subprocess/inde...ule-subprocess
Oct 10 '16 #2

Post your reply

Sign in to post your reply or Sign up for a free account.