Python 3.x 在一个面板中选择文件,并在另一个面板中可视化wxpython
这有点复杂,但我会试试看 我有一个用于选择文件的面板或按钮“文件”,该文件的打开是一个.nc文件(netCDF4),因此我希望将其可视化,但问题是我希望在另一个面板中看到它,该面板类似于quicklook,每次您都必须选择.nc类型的文件或直接在另一个面板上可视化 这是我为2个面板编写的部分代码:Python 3.x 在一个面板中选择文件,并在另一个面板中可视化wxpython,python-3.x,wxpython,matplotlib-widget,Python 3.x,Wxpython,Matplotlib Widget,这有点复杂,但我会试试看 我有一个用于选择文件的面板或按钮“文件”,该文件的打开是一个.nc文件(netCDF4),因此我希望将其可视化,但问题是我希望在另一个面板中看到它,该面板类似于quicklook,每次您都必须选择.nc类型的文件或直接在另一个面板上可视化 这是我为2个面板编写的部分代码: class LeftPanelTop(wx.Panel): # panel choose file def __init__(self, parent): super()._
class LeftPanelTop(wx.Panel): # panel choose file
def __init__(self, parent):
super().__init__(parent,style = wx.SUNKEN_BORDER)
self.SetBackgroundColour('snow2')
List_choices = ["1Km","3Km"]
List2 = ["3X3","5X5","7X7"]
self.dateLbl = wx.StaticBox(self, -1, 'Outils ', size=(310, 320))
self.dategraphSizer = wx.StaticBoxSizer(self.dateLbl, wx.VERTICAL)
combobox1 = wx.ComboBox(self,choices = List_choices, size =(80,20),pos =(180,50))
combobox2 = wx.ComboBox(self,choices = List2, size =(80,20),pos =(180,90))
wx.StaticText(self, label='Referen:', pos=(70, 50))
wx.StaticText(self, label='pixel:', pos=(70, 90))
QuickLook = wx.Button(self ,-1, "Open file" , size =(80, 25),pos =(180,130))
wx.StaticText(self, label='QuickLook:', pos=(70, 130))
QuickLook.Bind(wx.EVT_BUTTON, self.onOpen)
def onOpen(self, event):
wildcard = "netCDF4 files (*.nc)|*.nc"
dialog = wx.FileDialog(self, "Open netCDF4 Files| HDF5 files", wildcard=wildcard,
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if dialog.ShowModal() == wx.ID_CANCEL:
return
path = dialog.GetPath()
# panel for visualization
class LeftPanelBottom(wx.Panel):
def __init__(self, parent):
super().__init__(parent,style = wx.SUNKEN_BORDER)
self.SetBackgroundColour('whitesmoke')
self.dateLbl = wx.StaticBox(self, -1, 'QuickLook', size=(310, 600))
它在python3.6中为读取和查看netcdf4编码:
import numpy as np
import netCDF4
import matplotlib.pyplot as plt
#read the netcdf
fic='g_xvxvxvxv_20190108T120000Z.nc'
path='/home/globe/2019/01/08/'
nc = netCDF4.Dataset(path+fic,'r')
#read one variable in netcfd file
cm=nc.variables['cm'][:]
#visualization
plt.pcolormesh(cm)
plt.colorbar()
plt.show()
这就是我在panel2中看到的quicklook:
[![在此处输入图像描述][1][1]
我想做的是使用我的代码读取代码中的.nc,我的用户只需选择一个文件,然后自动显示在另一个面板上2:
[![在此处输入图像描述][2][2]
可能是这样的例子:
感谢您的帮助只需打开另一个帧,并将要解码、解压缩的文件名传递给它即可。
另一个选项是使用
webbrowser
,它将根据您在电脑上设置的首选项自动选择显示文件所需的程序
import wx
import webbrowser
import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
class MyFrame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
self.select_button = wx.Button(panel, label="Select file")
sizer.Add(self.select_button, 0, 0, 0)
self.select_button.Bind(wx.EVT_BUTTON, self.pick_file)
self.load_options = "netCDF4 files (nc)|*.nc| Text files (txt) |*.txt| All files |*.*"
panel.SetSizer(sizer)
def pick_file(self, event):
with wx.FileDialog(self, "Pick files", wildcard=self.load_options,
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE) as fileDialog:
if fileDialog.ShowModal() != wx.ID_CANCEL:
chosen_file = fileDialog.GetPath()
if chosen_file.endswith('.txt'):
#Method 1 using another frame
QuickLook(parent=self, text=chosen_file)
elif chosen_file.endswith('.nc'):
QuickLook_plot(parent=self, text=chosen_file)
else:
#Method 2 (the smart method) using webbrowser which chooses the application
# to use to display the file, based on preferences on your machine
webbrowser.open_new(chosen_file)
class QuickLook(wx.Frame):
def __init__(self,parent,text=None):
wx.Frame.__init__(self, parent, wx.ID_ANY, "Quick Look", size=(610,510))
panel = wx.Panel(self, wx.ID_ANY, size=(600,500))
log = wx.TextCtrl(panel, wx.ID_ANY,size=(600,480),
style = wx.TE_MULTILINE|wx.TE_READONLY|wx.VSCROLL)
Quit_button = wx.Button(panel, wx.ID_ANY, "&Quit")
Quit_button.Bind(wx.EVT_BUTTON, self.OnQuit)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(log, 1, wx.ALL|wx.EXPAND, 0)
sizer.Add(Quit_button,0,wx.ALIGN_RIGHT)
panel.SetSizerAndFit(sizer)
# use whatever method is appropriate for the file type
# to read, decode, uncompress, etc at this point
# I am assuming a text file below.
try:
with open(text,'rt') as f:
TextInfo = f.read()
log.write(TextInfo)
log.SetInsertionPoint(0)
self.Show()
except:
self.OnQuit(None)
def OnQuit(self,event):
self.Close()
self.Destroy()
class QuickLook_plot(wx.Frame):
def __init__(self, parent,text=None):
wx.Frame.__init__(self, parent, wx.ID_ANY, "Quick Plot", size=(610,510))
panel = wx.Panel(self, wx.ID_ANY, size=(600,500))
self.figure = Figure()
self.axes = self.figure.add_subplot(111)
self.canvas = FigureCanvas(panel, -1, self.figure)
Quit_button = wx.Button(panel, wx.ID_ANY, "&Quit")
Quit_button.Bind(wx.EVT_BUTTON, self.OnQuit)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.LEFT | wx.TOP | wx.GROW)
self.sizer.Add(Quit_button, 0,wx.ALIGN_RIGHT)
panel.SetSizerAndFit(self.sizer)
#plot figure
t = np.arange(0.0, 30.0, 0.01)
s = np.sin(2 * np.pi * t)
self.axes.plot(t, s)
self.Show()
def OnQuit(self,event):
self.Close()
self.Destroy()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'A test dialog')
frame.Show()
return True
if __name__ == "__main__":
app = MyApp()
app.MainLoop()
我将编辑我的问题,因为我有一个代码可以读取和可视化我的netcdf4。我不会为特定的文件类型编写代码,这是由您来完成的。我编辑我的问题是为了解释我需要做什么,以便使用use matplotlib查看netcdf4对不起,谢谢您的帮助。我可以使用netcfd文件的相同示例在quicklook中绘制它吗?谢谢,我需要一些帮助,例如matplotlib很复杂