473,382 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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

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
1 2467
dwblas
626 Expert 512MB
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

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

Similar topics

3
by: robert | last post by:
Hello, Anybody knows if it's possible to execute python code from an db. db=MySQLdb.connect(host="localhost",user="r",passwd="j",db="v") c=db.cursor() c.execute("""SELECT * FROM table...
2
by: jwaixs | last post by:
Hello, I've a question. Can I execute a part of a python code and put it's output in a string? Something like this: s = "" s = exec """print "Hello World" """ print s Greetz,
3
by: L.Peter | last post by:
Dear Group, i have a combobox and a button on a form. the combobox has three values: stop, pause,start when user press the button, the program will take apropriate action at the momment, here is...
17
by: seberino | last post by:
How can a proprietary software developer protect their Python code? People often ask me about obfuscating Python bytecode. They don't want people to easily decompile their proprietary Python app....
1
by: bob1660 | last post by:
I am new to PHP and MYSQL. I have a Database table with 4 field. FirstName, LastName, DateofBirth and MemberNumber. I would like to have a form where a user can fill in the FirstName, LastName and...
4
by: bob1660 | last post by:
I am new to PHP and MYSQL. I have a Database table with 4 field. FirstName, LastName, DateofBirth and MemberNumber. I would like to have a form where a user can fill in the FirstName, LastName and...
2
by: gsherp | last post by:
How does one execute code when the user closes the browers window by clicking on the top right corner . I need to log out user and do a database write when the user closes the brower. I do these to...
2
by: John c | last post by:
I have looping code in Access which generates Excel spreadsheets and prints them off. They can run to hundreds and at present, once the code starts to run there is no way it can stop cleanly part...
0
by: vysakhv90 | last post by:
Hi, This may be the easiest question you have ever heard..But I must ask this since I am new to Powershell.. I need powershell to ask for a user input and then based on the input it must read the...
0
by: jch053 | last post by:
I am unsure of how to actually store user-input data to a register. I would like to prompt the user for a signed base-10 integer, then store that integer to the bx register. What I have does not seem...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.