我试图在wx帧中对齐复选框和matplotlib画布。
任何人都可以建议在代码中进行更改吗?
如果我想在同一帧中再添加一个画布,我该如何添加它?
import wx
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
class canvasFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,"MyCanvas",size =(1200,400))
self.fig = plt.Figure(figsize=(15,5))
self.ax1 = self.fig.add_subplot(1,1,1)
self.canvas = FigureCanvas(self, -1, self.fig)
b1 = wx.CheckBox(self,-1,"Apples",(10,20),(160,-1))
b2 = wx.CheckBox(self,-1,"Mango",(10,40),(160,-1))
b1sizer = wx.BoxSizer(wx.VERTICAL)
b2sizer = wx.BoxSizer(wx.VERTICAL)
canvSizer = wx.BoxSizer(wx.HORIZONTAL)
b1sizer.Add(b1,0,wx.ALL,5)
b2sizer.Add(b2,0,wx.ALL,5)
canvSizer.Add(self.canvas,wx.TOP | wx.EXPAND,5)
if __name__ == "__main__":
app = wx.App()
frame = canvasFrame(parent=None,id = -1)
frame.Show()
app.MainLoop()
答案 0 :(得分:2)
你遗漏了sizer如何工作的很多部分......我真的建议你重新访问文档和google搜索教程
class canvasFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,"MyCanvas",size =(1200,400))
self.fig = plt.Figure(figsize=(15,5))
self.ax1 = self.fig.add_subplot(1,1,1)
self.canvas = FigureCanvas(self, -1, self.fig)
b1 = wx.CheckBox(self,-1,"Apples",(10,20),(160,-1))
b2 = wx.CheckBox(self,-1,"Mango",(10,40),(160,-1))
# first create your sizer for your checkboxes
checkbox_sizer = wx.BoxSizer(wx.VERTICAL)
checkbox_sizer.AddMany([b1,b2]) # add both buttons to the sizer
#next create the main sizer
main_sizer = wx.BoxSizer(wx.HORIZONTAL)
#add the checkbox sizer to the main_sizer
main_sizer.Add(checkbox_sizer)
# add the canvas to the main sizer
main_sizer.Add(self.canvas)
#Set the sizer as the sizer for the frame
self.SetSizer(main_sizer)
#call layout for the sizers to layout their elements
self.Layout()
#call fit to make the frame fit its contents
self.Fit()
答案 1 :(得分:2)
您错误地使用了sizer,请尝试以下操作:
import wx
import matplotlib.pyplot as plt
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
class canvasFrame(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,"MyCanvas",size =(1200,400))
self.fig = plt.Figure(figsize=(15,5))
self.ax1 = self.fig.add_subplot(1,1,1)
self.canvas = FigureCanvas(self, -1, self.fig)
b1 = wx.CheckBox(self,-1,"Apples", size=(160,-1))
b2 = wx.CheckBox(self,-1,"Mango", size=(160,-1))
mainsizer = wx.BoxSizer(wx.HORIZONTAL) # Main sizer
cbsizer = wx.BoxSizer(wx.VERTICAL) # Checkbox sizer
canvassizer = wx.BoxSizer(wx.VERTICAL) # Canvas sizer
# Add checkbox
cbsizer.Add(b1, 0, wx.ALIGN_LEFT|wx.ALL, 5)
cbsizer.Add(b2, 0, wx.ALIGN_LEFT|wx.ALL, 5)
# Add canvas
canvassizer.Add(self.canvas, 1, wx.EXPAND|wx.ALL, 1)
# Add "secondary" sizers to main sizer
mainsizer.Add(cbsizer, 1, wx.EXPAND|wx.ALL, 2)
mainsizer.Add(canvassizer, 3, wx.EXPAND|wx.ALL, 2)
self.SetSizer(mainsizer)
if __name__ == "__main__":
app = wx.App()
frame = canvasFrame(parent=None,id = -1)
frame.Show()
app.MainLoop()
现在,关于最后一个问题,我认为你会添加一个子图(轴)而不是其他画布,即
self.fig = plt.Figure(figsize=(15,5))
self.ax1 = self.fig.add_subplot(1,2,1)
self.ax2 = self.fig.add_subplot(1,2,2)
self.canvas = FigureCanvas(self, -1, self.fig)