问题描述
我正在尝试使用matplotlib在wxpython笔记本页面的面板内显示图像。使用wx.StaticBitmap可以完美工作,但是我需要使用matplotlib进行图像处理。代码给出了以下结果:
右侧面板是一个终端,尺寸正确。左侧面板是图像适合放置的位置。 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 (将#修改为@)