问题
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