how to execute python code with tkinter user input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kikiegoguma
    New Member
    • Oct 2016
    • 2

    how to execute python code with tkinter user input

    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

    Code:
    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

    Code:
    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()


    error :

    Traceback (most recent call last):
    File "C:\Users\user\ Anaconda2\lib\l ib-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('examp le.py')
    File "example.py ", line 13, in <module>
    model.fit(X, plotit=True, verbose=True)
    File "C:\Users\user\ Anaconda2\K_Med oids-master\k_medoid s.py", line 114, in fit
    X,self.n_cluste rs, self.dist_func, max_iter=self.m ax_iter, tol=self.tol,ve rbose=verbose)
    File "C:\Users\user\ Anaconda2\K_Med oids-master\k_medoid s.py", line 54, in _kmedoids_run members, costs, tot_cost, dist_mat = _get_cost(X, init_ids,dist_f unc)
    File "C:\Users\user\ Anaconda2\K_Med oids-master\k_medoid s.py", line 37, in _get_cost mask = np.argmin(dist_ mat,axis=1)
    File "C:\Users\user\ Anaconda2\lib\s ite-packages\numpy\ core\fromnumeri c.py", line 1034, in argmin return argmin(axis, out)
    ValueError: attempt to get argmin of an empty sequence
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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_distanc e_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_distanc e_func use subprocess.chec k_output() https://pymotw.com/2/subprocess/inde...ule-subprocess

    Comment

    Working...