当我需要绘制缩放时选择面板时,我在wxpython中有此应用程序wxpython无法在正确的面板中绘制,而在同一面板中向上绘制
我不知道为什么无法在正确的面板中绘图(中部面板黄色) 那是我的代码以及我拥有的:
import wx
import numpy as np
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.widgets import RectangleSelector
import matplotlib
import matplotlib.pyplot as plt
import os
import netCDF4
from netCDF4 import Dataset
class MainFrame(wx.Frame):
def __init__(self, parent ):
super().__init__(parent,title= "quick",size = (2000,1000))
left = LeftPanel(self)
middle = MiddlePanel(self)
right = RightPanel(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(left, 3, wx.EXPAND)
sizer.Add(middle, 5, wx.EXPAND)
sizer.Add(right, 5, wx.EXPAND)
self.SetSizer(sizer)
# ------------ LEFT ------------
class LeftPanelTop(wx.Panel):
def __init__(self, parent,size = (610,350)):
super().__init__(parent)
self.figure = Figure(figsize =(5,3))
self.canvas = FigureCanvas(self, -1, self.figure)
self.Size = self.canvas.Size
self.parent = parent
def load_from_file(self, file_name):
"""
Méthode effectuant l'intermédiaire pour charger le fichier selon
son type
"""
self.axes = self.figure.add_subplot(111)
if file_name.endswith(".nc"):
self._load_nc(file_name)
else:
self._load_txt(file_name)
self.canvas.draw()
def _load_nc(self, file_name):
""" Simule le chargement et affichage à partir d'un fichier nc """
path='D:/stage2019/air.departure.sig995.2012.nc'
nc = Dataset('D:/stage2019/air.departure.sig995.2012.nc')
lons = nc.variables['lon'][:]
lats = nc.variables['lat'][:]
air_dep = nc.variables['air_dep'][:,:,:]
air_dep = air_dep[0,:,:]
self.axes.pcolormesh(air_dep)
self.RS = RectangleSelector(self.axes,self.line_select_callback,
drawtype='box', useblit=False,
button=[1, 3],minspanx=5, minspany=5,
spancoords='pixels',
interactive=True, rectprops = dict(facecolor='None',edgecolor='red',alpha=5,fill=False))
def line_select_callback(self, eclick, erelease):
'eclick and erelease are the press and release events'
x1, y1 = eclick.xdata, eclick.ydata
x2, y2 = erelease.xdata, erelease.ydata
self.zoom_axes=[x1,x2,y1,y2]
self.parent.zoom_panel.Update(self)
class LeftPanelBottom(wx.Panel):
def __init__(self, parent):
super().__init__(parent,style = wx.SUNKEN_BORDER,size = (510,450) )
self.SetBackgroundColour('snow2')
panel_buttons = wx.Panel(self)
panel_buttons_sizer = wx.GridSizer(1, 2, 0, 0)
canvas_panel = LeftPanelTop(self)
self.canvas_panel = LeftPanelTop(self)
self.zoom_panel = MiddlePanelTop(parent=self)
select_button = PickButton(
panel_buttons,
"netCDF4 files (nc)|*.nc",
canvas_panel.load_from_file,
label="Open file",)
panel_buttons_sizer.Add(select_button)
panel_buttons.SetSizer(panel_buttons_sizer)
canvas_sizer = wx.BoxSizer(wx.HORIZONTAL)
canvas_sizer.Add(self.canvas_panel,1,wx.EXPAND)
canvas_sizer.Add(self.zoom_panel,1,wx.EXPAND)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(panel_buttons)
sizer.Add(canvas_panel)
self.SetSizerAndFit(sizer)
self.Show()
class PickButton(wx.Button):
""" Bouton permettant de choisir un fichier """
def __init__(self, parent, wildcard, func, **kwargs):
# func est la méthode à laquelle devra être foruni le fichier sélectionné
super().__init__(parent, **kwargs)
self.wildcard = wildcard
self.func = func
self.Bind(wx.EVT_BUTTON, self.pick_file)
def pick_file(self, evt):
style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE
with wx.FileDialog(
self, "Pick files", wildcard=self.wildcard, style=style
) as fileDialog:
if fileDialog.ShowModal() != wx.ID_CANCEL:
chosen_file = fileDialog.GetPath()
self.func(chosen_file)
class LeftPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent,style = wx.SUNKEN_BORDER)
top = LeftPanelTop(self)
bottom = LeftPanelBottom(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(top, 3, wx.EXPAND)
sizer.Add(bottom, 4, wx.EXPAND)
self.SetSizer(sizer)
# ------------ MIDDLE ------------
class MiddlePanelTop(wx.Panel):
def __init__(self, parent):
super().__init__(parent,style = wx.SUNKEN_BORDER,size = (300,200))
self.SetBackgroundColour('yellow')
self.Show()
def Update(self,parent):
#Load axis values of the selected rectangle
zoom_axes=parent.zoom_axes
#duplicate the plot from the main panel
self.figure = Figure(figsize =(5,3))
self.canvas = FigureCanvas(self, -1, self.figure)
self.axes = self.figure.add_subplot(111)
#Apply axis of drawn rectangle to the plot
self.axes.axis(zoom_axes)
path='D:/stage2019/air.departure.sig995.2012.nc'
nc = Dataset('D:/stage2019/air.departure.sig995.2012.nc')
lons = nc.variables['lon'][:]
lats = nc.variables['lat'][:]
air_dep = nc.variables['air_dep'][:,:,:]
air_dep = air_dep[0,:,:]
self.axes.pcolormesh(air_dep)
self.canvas.draw()
self.Refresh()
class MiddlePanelBottom(wx.Panel):
def __init__(self, parent):
super().__init__(parent,style = wx.SUNKEN_BORDER)
self.SetBackgroundColour('black')
canal=wx.Button(self,-1,"Variable",size=(140,30),pos=(100,0))
dynamique=wx.Button(self,-1,"Dynamique",size=(140,30),pos=(240,0))
file = wx.Button(self,-1,"File", size = (110,30),pos=(0,0))
dynamique.SetBackgroundColour('white')
canal.SetBackgroundColour('white')
file.SetBackgroundColour('white')
dynamique.Bind(wx.EVT_BUTTON, self.OnClick)
file.Bind(wx.EVT_BUTTON, self.onOpen)
self.load_options = "netCDF4 files (nc)|*.nc| Text files (txt) |*.txt| All files |*.*"
def onOpen(self, event):
wildcard = "netCDF4 files (*.nc)|*.nc"
dialog = wx.FileDialog(self, "Open netCDF4 Files", wildcard=wildcard,
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
if dialog.ShowModal() == wx.ID_CANCEL:
return
path = dialog.GetPath()
if os.path.exists(path):
with open(path) as fobj:
for line in fobj:
self.my_text.WriteText(line)
def OnClick(self,event):
dlg = wx.TextEntryDialog(self, 'Enter Dynamique of image','Dynamique de image')
if dlg.ShowModal() == wx.ID_OK:
self.text.SetValue("Dynamique:"+dlg.GetValue())
dlg.Destroy()
class MiddlePanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
top = MiddlePanelTop(self)
bottom = MiddlePanelBottom(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(top, 2, wx.EXPAND)
sizer.Add(bottom, 2, wx.EXPAND)
self.SetSizer(sizer)
# ------------ RIGHT ------------
class RightPanelTop(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.SetBackgroundColour('black')
canal=wx.Button(self,-1,"Variable",size=(140,30),pos=(100,0))
dynamique=wx.Button(self,-1,"Dynamique",size=(140,30),pos=(240,0))
file = wx.Button(self,-1,"File", size = (110,30),pos=(0,0))
dynamique.SetBackgroundColour('white')
canal.SetBackgroundColour('white')
file.SetBackgroundColour('white')
dynamique.Bind(wx.EVT_BUTTON, self.OnClick)
file.Bind(wx.EVT_BUTTON, self.onOpen)
def onOpen(self, event):
wildcard = "netCDF4 files (*.nc)|*.nc| HDF5 files (*.h5) |*.h5"
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()
if os.path.exists(path):
with open(path) as fobj:
for line in fobj:
self.my_text.WriteText(line)
def OnClick(self,event):
dlg = wx.TextEntryDialog(self, 'Enter Dynamique of image','Dynamique de image')
if dlg.ShowModal() == wx.ID_OK:
self.text.SetValue("Dynamique:"+dlg.GetValue())
dlg.Destroy()
class RightPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent,style = wx.SUNKEN_BORDER)
top = RightPanelTop(self)
bottom = RightPanelBottom(self)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(top, 2, wx.EXPAND)
sizer.Add(bottom, 2, wx.EXPAND)
self.SetSizer(sizer)
class PanelBottom(wx.Panel):
def __init__(self,parent):
super().__init__(parent)
self.SetBackgroundColour('grey77')
class PanelTop(wx.Panel):
def __init__(self,parent):
super().__init__(parent)
left = SubPanelLeft(self)
right = SubPanelRight(self)
midlle = SubPanelMiddle(self)
sizer1 = wx.BoxSizer(wx.HORIZONTAL)
sizer1.Add(left, 1, wx.EXPAND)
sizer1.Add(midlle, 1, wx.EXPAND)
sizer1.Add(right, 1, wx.EXPAND)
self.SetSizer(sizer1)
class RightPanelBottom(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.SetBackgroundColour('snow2')
top = PanelTop(self)
bottom = PanelBottom(self)
sizer1 = wx.BoxSizer(wx.VERTICAL)
sizer1.Add(top, 2, wx.EXPAND)
sizer1.Add(bottom, 4, wx.EXPAND)
self.SetSizer(sizer1)
class SubPanelLeft(wx.Panel):
def __init__(self, parent):
super().__init__(parent,style = wx.SUNKEN_BORDER)
self.SetBackgroundColour('black')
class SubPanelMiddle(wx.Panel):
def __init__(self, parent):
super().__init__(parent,style = wx.SUNKEN_BORDER)
self.SetBackgroundColour('black')
class SubPanelRight(wx.Panel):
def __init__(self, parent):
super().__init__(parent,style = wx.SUNKEN_BORDER)
self.SetBackgroundColour('black')
app = wx.App()
frame = MainFrame(None).Show()
app.MainLoop()
选择文件后,当我单击矩形时,在同一面板内选择缩放比例来缩放缩放比例:
答案 0 :(得分:1)
从哪里开始?
在LeftPanelBottom
中,您已定义
canvas_panel = LeftPanelTop(self)
self.canvas_panel = LeftPanelTop(self)
self.zoom_panel = MiddlePanelTop(parent=self)
这就是2个画布面板,接着是sizer
的混乱,以及您将MiddlePanelTop
拍击声和LeftPanelBottom
放在LeftPanelTop
里面的事实进入LeftPanel
。
但是主要问题是关于inheritance
。因为几乎所有事物本身都被定义为class
,所以即使是按钮,试图找到通向MiddlePanelTop
的路径也无济于事。当我迷路时,也许有人可以在这里鸣叫。
一种简单且显然非Python的方法是只使用一个global
变量。
下面,我使用一个名为global
的{{1}}。
N.B.我还根据需要将Zoom
更改为文件
编辑:
我已经在类中对您的类进行了解码,并添加了适当的path
条目,这使我们可以访问它们。
结果,self
面板现在也可以按照我想的那样工作。
我注意到Left
和Right
面板仍然存在不一致,缺少项目和固定位置的情况。
MiddlePanelBottom