Display image in wx.GridBagSizer

拈花ヽ惹草 提交于 2019-12-14 03:59:26

问题


I am trying to display an image on a wx.GridBagSizer

The image is being read and I can see it if I comment out the sizerMain.Add lines, but it will not show in the sizer. Interestingly the space is reserved for it in the sizer.

Can anyone please help?

    import wx

class MainWindow(wx.Frame):

    def __init__(self,parent,id,title):
      wx.Frame.__init__(self,parent, wx.ID_ANY, title, size = (1200,600), style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)

      sizerMain = wx.GridBagSizer(3, 2)
      self.sizerMain = sizerMain      

      pnl = wx.Panel(self)

      cmd1 = wx.Button(pnl, label='aaaaa')
      cmd2 = wx.Button(pnl, label='bbbbbb')
      cmd3 = wx.Button(pnl, label='ccccc')
      cmd4 = wx.Button(pnl, label='dddd')

      imgSizer = wx.BoxSizer(wx.HORIZONTAL) 
      image = wx.Bitmap('test.png',wx.BITMAP_TYPE_PNG)
      img = wx.StaticBitmap(self, -1, image)
      imgSizer.Add(img, flag=wx.LEFT, border=10)

      sizerMain.Add(imgSizer, pos=(0,0), span=(1, 3), flag=wx.TOP|wx.LEFT|wx.RIGHT, border=10)
      sizerMain.Add(cmd1, pos=(2,2), flag=wx.TOP|wx.LEFT|wx.RIGHT, border=10)
      sizerMain.Add(cmd2, pos=(1, 0), flag=wx.TOP|wx.LEFT|wx.RIGHT, border=10)
      sizerMain.Add(cmd3, pos=(1, 1), flag=wx.TOP|wx.LEFT|wx.RIGHT, border=10)
      sizerMain.Add(cmd4, pos=(2, 1), flag=wx.TOP|wx.LEFT|wx.RIGHT, border=10)
      pnl.SetSizer(sizerMain) 
      pnl.Layout()
      pnl.Fit()

      self.Show(True)

    def CloseWindow(self, event):
        self.Close()

class MyApp(wx.App):
    def OnInit(self):
        frame = MainWindow(None, -1, "Test GridBag")
        frame.Centre()
        self.SetTopWindow(frame)
        return True

# Declare the Application and start the Main Loop
app = MyApp(0)
app.MainLoop()

回答1:


The parent of the wx.StaticBitmap "img" needs to be the panel "pnl". Not "self".



来源:https://stackoverflow.com/questions/29319847/display-image-in-wx-gridbagsizer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!