0

I have the following code that uses Tkinter's askopenfilename to enable the user to select a file. The contents of this file is then used for a graph. I'm only using Tkinter to allow the user to select the file, nothing else. Therefore is there Python script that will end Tkinter after the file has been opened, which I want to be located in the line marked with '##'. Because Tkinter is still running, when it doesn't need to be. The code program that my code is for is to stop when the graph is plotted. Here is my code:

Exampe of the data
x,y,
1,4,
3,9,
6,7,
,,

#Code starts
import numpy as np
from Tkinter import Tk
from tkFileDialog import askopenfilename
import matplotlib.pyplot as plt

Tk().withdraw() # keep the root window from appearing (dont want full Gui)
filename = askopenfilename()# show an "Open" dialog box and return the path to the    selected file print(filename)
data = np.genfromtxt(filename, dtype=[('x',float),('y',float)],comments='"', delimiter=',',skip_header=1,missing_values=True)
##Location of tkinter stop code##
x=data['x']
x = x[np.logical_not(np.isnan(x))] #Remove Nan values
y=data['y']
y = y[np.logical_not(np.isnan(y))] # Remove Nan values
plt.plot(x, y, 'ko', ms=4)
plt.show()
#Code Ends

2 Answers 2

4

Keep a reference to your Tk object, and call its destroy method after you are done:

tk = Tk()
tk.withdraw() 

#do file dialog stuff
(...)

tk.destroy()
Sign up to request clarification or add additional context in comments.

Comments

0

destroy it using .destroy()

window.destroy()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.