在面板中选择文件并在另一个面板中可视化 wxpython [英] selection file in a panel and visualize in another panel wxpython

查看:29
本文介绍了在面板中选择文件并在另一个面板中可视化 wxpython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有点复杂,但我会试试

我有一个面板或一个用于选择文件的按钮文件",并且该文件的打开是一个 .nc 文件 (netCDF4),因此我想将其可视化,但我想在其中查看问题另一个面板就像快速浏览一样,每次您必须选择 .nc 类型的文件或直接在另一个面板上可视化

这是我的 2 个面板代码的一部分:

class LeftPanelTop(wx.Panel): # 面板选择文件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, "打开文件", 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):通配符 = "netCDF4 文件 (*.nc)|*.nc"dialog = wx.FileDialog(self, "Open netCDF4 Files| HDF5 files", wildcard=wildcard,风格=wx.FD_OPEN |wx.FD_FILE_MUST_EXIST)如果 dialog.ShowModal() == wx.ID_CANCEL:返回路径 = dialog.GetPath()# 可视化面板类 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的代码:

将 numpy 导入为 np导入 netCDF4导入 matplotlib.pyplot 作为 plt#读取netcdffic='g_xvxvxvxv_20190108T120000Z.nc'路径='/home/globe/2019/01/08/'nc = netCDF4.Dataset(path+fic,'r')#读取netcfd文件中的一个变量cm=nc.variables['cm'][:]#可视化plt.pcolormesh(cm)plt.colorbar()plt.show()

我会在 panel2 中看到类似 quicklook 的内容:

[![在此处输入图片描述][1]][1]

我想要做的是使用我的代码来读取我的代码中的 .nc 并且我的用户可以只选择一个文件然后在另一个面板上自动显示2:

[![在此处输入图片描述][2]][2]

也许就像这个例子:

it's a little complicated but I will try

I have a panel or a button 'file' for the choice of a file and the opening of this file is a .nc file (netCDF4) so I would like to visualize it but the problem I would like to see it in one other panel which will be like a quicklook, and each time you have to choose a file of type .nc or directly to be visualized on the other panel

and here is part of my code for 2 panels:

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))

that it code for read and view netcdf4 in python3.6:

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()

that what i would see in panel2 like quicklook:

[![enter image description here][1]][1]

what i would like to do is use my code to read the .nc in my code and that my users can just choose a file and then display automatically on the other panel2 :

[![enter image description here][2]][2]

maybe is like this example : How to use matplotlib blitting to add matplot.patches to an matplotlib plot in wxPython?

thank you for the help

解决方案

Just open another frame and pass it the filename to be decoded, uncompressed, whatever, to be displayed.
The other option is to use webbrowser which will automatically pick the program required to display the file, based on the preferences that you have set on your pc.

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()

这篇关于在面板中选择文件并在另一个面板中可视化 wxpython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆