我有以下代码,它使用Tkinter的askopenfilename使用户能够选择文件。然后将该文件的内容用于图形。我只是使用Tkinter来允许用户选择文件,而不是其他。因此,有一个Python脚本将在文件打开后结束Tkinter,我希望它位于标记为“##”的行中。因为Tkinter仍然在运行,而它不需要运行。我的代码所针对的代码程序是在绘制图形时停止的。下面是我的代码:
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发布于 2012-08-09 19:15:32
保留对Tk对象的引用,并在完成后调用其destroy方法:
tk = Tk()
tk.withdraw()
#do file dialog stuff
(...)
tk.destroy()发布于 2013-12-18 03:21:33
使用.destroy()销毁它
window.destroy()https://stackoverflow.com/questions/11881827
复制相似问题