Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 无法在所需位置显示面板_Python 2.7_Wxpython - Fatal编程技术网

Python 2.7 无法在所需位置显示面板

Python 2.7 无法在所需位置显示面板,python-2.7,wxpython,Python 2.7,Wxpython,我在WX框架中创建了一个面板,然后创建了一个FigureCanvas。我想将FigureCanvas完全安装到面板中,但不知何故,FigureCanvas没有进入面板2_2,而是正好位于相反的一侧 下面是我的代码 import wx from numpy import arange, sin, pi import matplotlib matplotlib.use('WXAgg') from matplotlib.backends.backend_wxagg import FigureCanv

我在WX框架中创建了一个面板,然后创建了一个FigureCanvas。我想将FigureCanvas完全安装到面板中,但不知何故,FigureCanvas没有进入面板2_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(即右侧)而不是左侧

我认为画布确实要放在panel2_2上。MWE中的问题是没有为panel2_2定义尺寸大小。因此,panel2_2从帧的左上角渲染。这将导致panel2_2加上显示在框架左侧的画布。您在画布右侧看到的不是panel2_2,而是框架的其余部分,因为框架的大小大于panel2_2的大小。如果添加一个蓝色面板1_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()