【问题标题】:Unable to display panel at the desired position无法在所需位置显示面板
【发布时间】:2019-04-18 15:31:08
【问题描述】:

我在 wxFrame 中创建了一个 Panel,然后创建了一个 FigureCanvas。我想将 FigureCanvas 完全放入面板中,但不知何故 FigureCanvas 并没有进入 panel2_2,而是正好在对面。

以下是我的代码。

import wx
from numpy import arange, sin, pi
import matplotlib
matplotlib.use('WXAgg')

from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
from matplotlib.figure import Figure


class Frame1(wx.Frame):

    def __init__(self, prnt):

        wx.Frame.__init__(self, parent=prnt,
                          pos=wx.Point(0, 0), size=wx.Size(1340, 720),
                          style=wx.DEFAULT_FRAME_STYLE)

        self.panel2_2 = wx.Panel(parent=self,
                                 pos=wx.Point(940, 30), size=wx.Size(400, 690),
                                 style=wx.TAB_TRAVERSAL)

        self.figure = Figure()
        self.axes = self.figure.add_subplot(111)
        self.canvas = FigureCanvas(self.panel2_2, -1, self.figure)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(self.canvas, 0, wx.EXPAND)
        self.panel2_2.SetSizer(sizer)
        self.panel2_2.Fit()

        t = arange(0.0, 3.0, 0.01)
        s = sin(2 * pi * t)
        self.axes.plot(t, s)


#Every wxWidgets application must have a class derived from wxApp
class MyApp(wx.App):
# wxWidgets calls this method to initialize the application
    def OnInit(self):

        # Create an instance of our customized Frame class
        self.frame = Frame1(None)
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True


if __name__ == '__main__':
    application = MyApp(0)
    application.MainLoop()

结果

我希望将图像放在 panel2_2(即右侧)而不是左侧

【问题讨论】:

    标签: python-2.7 wxpython


    【解决方案1】:

    我认为画布确实要到 panel2_2。 MWE 中的问题是您没有为 panel2_2 定义 sizer。因此,panel2_2 从框架的左上角开始渲染。这导致 panel2_2 加上画布显示在框架的左侧。您在画布右侧看到的不是 panel2_2,而是框架的其余部分,因为框架的大小大于 panel2_2 的大小。如果您添加一个蓝色 panel1_1 并将另一个 wx.BoxSizer 分配给框架,您会看到画布显示在右侧。

    import wx
    from numpy import arange, sin, pi
    import matplotlib
    matplotlib.use('WXAgg')
    
    from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
    from matplotlib.figure import Figure
    
    
    class Frame1(wx.Frame):
    
        def __init__(self, prnt):
    
            wx.Frame.__init__(self, parent=prnt,
                              pos=wx.Point(0, 0), size=wx.Size(1340, 720),
                              style=wx.DEFAULT_FRAME_STYLE)
    
            self.panel1_1 = wx.Panel(parent=self, size=(400, 690))
            self.panel1_1.SetBackgroundColour('blue')
    
            self.panel2_2 = wx.Panel(parent=self,
                                     pos=wx.Point(940, 30), size=wx.Size(400, 690),
                                     style=wx.TAB_TRAVERSAL)
    
            self.figure = Figure()
            self.axes = self.figure.add_subplot(111)
            self.canvas = FigureCanvas(self.panel2_2, -1, self.figure)
    
            sizer = wx.BoxSizer(wx.HORIZONTAL)
            sizer.Add(self.canvas, 0, wx.EXPAND)
            self.panel2_2.SetSizer(sizer)
            self.panel2_2.Fit()
    
            sizerPanels = wx.BoxSizer(wx.HORIZONTAL)
            sizerPanels.Add(self.panel1_1)
            sizerPanels.Add(self.panel2_2)
            sizerPanels.Fit(self)
            self.SetSizer(sizerPanels)
    
            t = arange(0.0, 3.0, 0.01)
            s = sin(2 * pi * t)
            self.axes.plot(t, s)
    
            self.Center()
    
    
    #Every wxWidgets application must have a class derived from wxApp
    class MyApp(wx.App):
    # wxWidgets calls this method to initialize the application
        def OnInit(self):
    
            # Create an instance of our customized Frame class
            self.frame = Frame1(None)
            self.SetTopWindow(self.frame)
            self.frame.Show()
            return True
    
    
    if __name__ == '__main__':
        application = MyApp(0)
        application.MainLoop()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 2012-06-06
      相关资源
      最近更新 更多