微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Matplotlib Canvas不适合wxpython面板

如何解决Matplotlib Canvas不适合wxpython面板

我正在尝试使用matplotlib在wxpython笔记本页面的面板内显示图像。使用wx.StaticBitmap可以完美工作,但是我需要使用matplotlib进行图像处理。代码给出了以下结果:

enter image description here

右侧面板是一个终端,尺寸正确。左侧面板是图像适合放置的位置。 wx.lib.inspection的结果显示wxfigureCanvas的最小大小为(640,480),但我不明白为什么,也找不到如何更改它。我怀疑是问题所在。代码(很抱歉,我能做到的很简短):

import cv2 as cv
import matplotlib.pyplot as plt
from matplotlib.figure import figure
from matplotlib.backends.backend_wxagg import figureCanvasWxAgg as figureCanvas
import wx
import wx.lib.agw.aui as aui

class TopRight(wx.Panel):
    def __init__(self,parent):
        super(TopRight,self).__init__(parent)
        self.term = wx.TextCtrl(self,size=(200,100),style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH
                                )
        self.term.SetDefaultStyle(wx.TextAttr(wx.GREEN))
        self.term.SetBackgroundColour(wx.BLACK)     
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.term,1,wx.EXPAND)
        self.SetSizer(self.sizer)
        self.term.WriteText("hello")
        self.term.Update

class TopLeft(wx.Panel):
    def __init__(self,parent):
        super(TopLeft,self).__init__(parent)

        img_pth = "av_cat.png"
        # Intitialise the matplotlib figure
        self.figure = figure()

        # Create an axes,turn off the labels and add them to the figure
        self.axes = plt.Axes(self.figure,[0,1])
        self.axes.set_axis_off()
        self.figure.add_axes(self.axes)

        # Add the figure to the wxfigureCanvas
        self.canvas = figureCanvas(self,-1,self.figure)

        # Sizer to contain the canvas
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.canvas,wx.EXPAND|wx.ALL)
        self.SetSizer(self.sizer)
        
        # If there is an initial image,display it on the figure
        if img_pth is not None:
            self.setimage(img_pth)

    
    def setimage(self,pathToImage):
        # Load the image into matplotlib
        image = cv.imread(pathToImage) 
        # Add the image to the figure and redraw the canvas.
        self.axes.imshow(image)
        self.canvas.draw()

class explorer_panel(wx.Panel):
    def __init__(self,parent):
        #Constructor
        wx.Panel.__init__(self,parent)
        topSplitter = wx.SplitterWindow(self)
        image_panel = TopLeft(topSplitter)
        terminal_panel = TopRight(topSplitter)
        topSplitter.SplitVertically(image_panel,terminal_panel)
        topSplitter.SetSashGravity(0.5)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(topSplitter,wx.EXPAND)
        self.SetSizer(sizer)

class Main(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(
            self,parent = None,title = "Borges Infinite Image",size = (600,300)
            )

        panel = wx.Panel(self)
        notebook = wx.Notebook(panel)
        explorer = explorer_panel(notebook)
        notebook.AddPage(explorer,'Explorer')
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook,wx.ALL|wx.EXPAND,5)
        panel.SetSizer(sizer)

if __name__ == "__main__":
    app = wx.App()
    frame = Main()
    frame.Show()
    import wx.lib.inspection as wxli
    wxli.inspectionTool().Show()
    app.MainLoop()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。