wxPython添加wx.StaticBitmap到wx.GridBagSiz公司

2024-09-23 22:23:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个位图,我想放在一个特定的位置使用wx.GridBagSizer公司. 这个问题类似于this one。然而,目前还不清楚是否解决了这一问题。在

这是one of the examples I found。我相信我也在做同样的事情,但我的形象并没有被添加到尺码表中。在

这是我的密码。我创建了一个框架,面板和两个位图放在两个不同的单元格中wx.gridbagsizer公司. 但是,当我用实际的图形更新位图时,它们看起来是重叠的。在

    frame = wx.Frame.__init__(self, None, wx.ID_ANY, "", size = (1200,800))#, style= wx.SYSTEM_MENU | wx.CAPTION)
    self.panel = wx.Panel(self, wx.ID_ANY, style=wx.BORDER_THEME, size = (1200,800))

    bmp1 = wx.Bitmap.FromRGBA(100, 100, red=255, alpha=0)
    self.bitmap1 = wx.StaticBitmap(self.panel, bitmap=bmp1)
    self.bitmap2 = wx.StaticBitmap(self.panel, bitmap=bmp1)

    sizer = wx.GridBagSizer(hgap = 0, vgap = 0)#(13, 11)
    sizer.Add(self.bitmap1, pos=(0,0),  flag = wx.ALL)#, flag=wx.TOP|wx.RIGHT) FIXIT so the sidebar is closer to the graph
    sizer.Add(self.bitmap2, pos=(0,1),  flag = wx.ALL)#,flag=wx.TOP|wx.RIGHT)


    def buf2wx (buf):
        image = PIL.Image.open(buf)
        width, height = image.size
        return wx.Bitmap.FromBuffer(width, height, image.tobytes())

    import PIL
    import io
    from matplotlib import pyplot as plt
    import urllib, base64
    import cStringIO
    import io
    from numpy import random
    plt.figure()
    b = random.rand(100,)
    plt.subplot(311)
    plt.plot(b)
    b = random.rand(100,)
    plt.subplot(312)
    plt.plot(b)
    b = random.rand(100,)
    plt.subplot(313)
    plt.plot(b)
    plt.title("test")
    buf = io.BytesIO()
    plt.savefig(buf, format='jpg')
    buf.seek(0)

    self.bitmap1.SetBitmap(buf2wx(buf))
    self.bitmap2.SetBitmap(buf2wx(buf))


    buf.close()
    app.MainLoop()

我得到的是重叠图

enter image description here

我可以通过指定位置来移动它们

^{pr2}$

enter image description here


Tags: theimportselfsizepltrandomflagwx
1条回答
网友
1楼 · 发布于 2024-09-23 22:23:14

最终答案是微不足道的。为了使它工作,我需要设置尺寸。在前面的图表末尾添加下面的代码应用程序主循环(),尤其是“setizer()”。其他函数负责调整大小等。另外,确保位图被添加到面板对象中(自面板),而不是框架(自身)。在

    self.panel.SetSizer(sizer)
    self.Layout()
    self.panel.Layout()
    self.Fit()

相关问题 更多 >