私はPythonの初心者であり、完全にバイリンガルではないので、あなたが私を理解してくれることを願っています。誰もがファイルを開くことができるコードを開発しようとしています。グラフのmatplotlibにその内容を表示するには、read_file()
という関数を使ってデータを取得し、問題なくListbox
を挿入します。私は機能を果たしましたが、ファイルに含まれている情報をshow_graph()
という別の関数から呼び出す場合、この部分ではロードされたファイル(read_file()
関数内)を使用する必要があります。これを実現する唯一の方法は、 :オープンファイルを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()
あなたのためのファイルを閉じると* "ではなく、私のための実用的な "* - なぜですか?開いているファイルのハンドルを回すことは、より混乱を招く可能性があります。 – jonrsharpe
ファイルを一時ファイルにコピーして使用してから削除できますか? – Techidiot
ファイルをロードして、実際のファイルハンドルとほとんど同じインターフェースを持つ 'io.StringIO'オブジェクトを渡すことができます。 –