2016-09-14 7 views
0

私はPythonの初心者であり、完全にバイリンガルではないので、あなたが私を理解してくれることを願っています。誰もがファイルを開くことができるコードを開発しようとしています。グラフのmatplotlibにその内容を表示するには、read_file()という関数を使ってデータを取得し、問題なくListboxを挿入します。私は機能を果たしましたが、ファイルに含まれている情報をshow_graph()という別の関数から呼び出す場合、この部分ではロードされたファイル(read_file()関数内)を使用する必要があります。これを実現する唯一の方法は、 :オープンファイルをseverals関数で再利用する方法

私はseverals機能でそれを再利用するために、開いているファイルを使用する方法を他の言葉で、私のための実用的なプロットすることはできませんが、これで
f = open(‘example1.las') 

log = LASReader(f, null_subs=np.nan) 

誰かが私にこの問題を解決するためのサポートを与えることができますか?

from Tkinter import * 
from las import LASReader 
from pprint import pprint 
import tkFileDialog 
import matplotlib, sys 
matplotlib.use('TkAgg') 
import numpy as np 
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg 
from matplotlib.figure import Figure 
import matplotlib.pyplot as plt 

root = Tk() 
root.geometry("900x700+10+10") 

def read_file(): 
    filename = tkFileDialog.askopenfilename() 
    f = open(filename) 
    log = LASReader(f, null_subs=np.nan) 

    for curve in log.curves.names: 
    parent.insert(END,curve) 

def add_name(): 
    it = parent.get(ACTIVE) 
    child.insert(END, it) 

def show_graph(): 
    child = Listbox(root, selectmode=MULTIPLE) 
    try: 
     s = child.selection_get() 
     if s == "GR": 
      print 'selected:', s    
      f = open('example1.las') 
      log = LASReader(f, null_subs=np.nan) 


      fig = plt.figure(figsize=(6, 7.5)) 

      plt.plot(log.data['GR'], log.data['DEPT']) 
      plt.ylabel(log.curves.DEPT.descr + " (%s)" % log.curves.DEPT.units) 
      plt.xlabel(log.curves.GR.descr + " (%s)" % log.curves.GR.units) 
      plt.ylim(log.stop, log.start) 
      plt.title(log.well.WELL.data + ', ' + log.well.DATE.data) 
      plt.grid() 
      dataPlot = FigureCanvasTkAgg(fig, master=root) 
      dataPlot.show() 
      dataPlot.get_tk_widget().grid(row=0, column=2, columnspan=2, rowspan=2, 
       sticky=W+E+N+S, padx=380, pady=52) 

     elif s == "NPHI": 
      print 'selected:', s 
      f = open('Shamar-1.las') 
      log = LASReader(f, null_subs=np.nan) 


      fig = plt.figure(figsize=(6, 7.5)) 

      plt.plot(log.data['NPHI'], log.data['DEPT']) 
      plt.ylabel(log.curves.DEPT.descr + " (%s)" % log.curves.DEPT.units) 
      plt.xlabel(log.curves.NPHI.descr + " (%s)" % log.curves.NPHI.units) 
      plt.ylim(log.stop, log.start) 
      plt.title(log.well.WELL.data + ', ' + log.well.DATE.data) 
      plt.grid() 
      dataPlot = FigureCanvasTkAgg(fig, master=root) 
      dataPlot.show() 
      dataPlot.get_tk_widget().grid(row=0, column=2, columnspan=2, rowspan=2, 
       sticky=W+E+N+S, padx=380, pady=52) 

     elif s == "DPHI": 
      print 'selected:', s 
      f = open('Shamar-1.las') 
      log = LASReader(f, null_subs=np.nan) 


      fig = plt.figure(figsize=(6, 7.5)) 

      plt.plot(log.data['DPHI'], log.data['DEPT']) 
      plt.ylabel(log.curves.DEPT.descr + " (%s)" % log.curves.DEPT.units) 
      plt.xlabel(log.curves.DPHI.descr + " (%s)" % log.curves.DPHI.units) 
      plt.ylim(log.stop, log.start) 
      plt.title(log.well.WELL.data + ', ' + log.well.DATE.data) 
      plt.grid() 
      dataPlot = FigureCanvasTkAgg(fig, master=root) 
      dataPlot.show() 
      dataPlot.get_tk_widget().grid(row=0, column=2, columnspan=2, rowspan=2, 
       sticky=W+E+N+S, padx=380, pady=52) 

    except: 
     print 'no selection' 

def remove_name(): 
    child.delete(ACTIVE) 

def btnClick(): 
     pass 

e = Entry(root) 
e.pack(padx=5) 
b = Button(root, text="OK", command=btnClick) 
b.pack(pady=5) 

# create the canvas, size in pixels 
canvas = Canvas(width = 490, height = 600, bg = 'grey') 
# pack the canvas into a frame/form 
canvas.place(x=340, y=50) 

etiqueta = Label(root, text='Nemonics:') 
etiqueta.place(x=10, y=30) 

parent = Listbox(root) 

root.title("Viewer") 

parent.place(x=5, y=50) 

selec_button = Button(root, text='Graph', 
        command=show_graph) 
selec_button.place(x=340, y=20) 

remove_button = Button(root, text='<<delete', 
        command=remove_name) 
remove_button.place(x=138, y=150) 

add_button = Button(root, text='Add>>', 
        command=add_name) 
add_button.place(x=138, y=75) 


child = Listbox(root) 
child.place(x=210, y=50) 

butt = Button(root, text="load file", command=read_file) 
butt.place(x=10, y=5) 



root.mainloop() 
+0

あなたのためのファイルを閉じると* "ではなく、私のための実用的な "* - なぜですか?開いているファイルのハンドルを回すことは、より混乱を招く可能性があります。 – jonrsharpe

+0

ファイルを一時ファイルにコピーして使用してから削除できますか? – Techidiot

+0

ファイルをロードして、実際のファイルハンドルとほとんど同じインターフェースを持つ 'io.StringIO'オブジェクトを渡すことができます。 –

答えて

0

あなたは、変数名の前にグローバルとして宣言、それを維持するために、グローバル変数を使用することができます(あなたのケースではF):

はここで完全なコードです。ただし、ファイルを変更する場合はお勧めしません。

0

これはあなたが使用しているレーザーリーダーですか? https://scipy.github.io/old-wiki/pages/Cookbook/LASReader.html

次に、ファイルハンドルを与える必要はありません。これは、トリックを行う必要があるファイル名を与えるだけです。リーダーは解析後にファイルを閉じます。

def read_file(): 
    filename = tkFileDialog.askopenfilename() 
    log = LASReader(filename, null_subs=np.nan) 

あなたは通常、ファイルの内容を読んだことがある場合は、後でそれを閉じることができます。

f = open(filename) 
log = LASReader(f, null_subs=np.nan) 
f.close() 

または使用自動的

with open(filename, 'r') as f: 
    log = LASReader(f, null_subs=np.nan) 
関連する問題