问题
I have developed a small wxPython program that gives radically different output in Windows and Ubuntu (I am more than happy to be told I've programmed it incorrectly and I would regard that as a result- provided we can get it to work)
The program displays four shapes the right hand side. Double clicking on the right hand images moves them to the left hand side and vice versa.
Issues on Windows (see screen shots taken after the same actions in Windows and Ubuntu): The buttons don't render correctly; when I click on the cross, cicle or square they move correctly to the left of the screen, but multiple images of the triangle remain; an image appears in the top left corner
None of these issues appear in Ubuntu
import wx
class ImageSizer(wx.Frame):
def __init__(self, parent, title):
super(ImageSizer, self).__init__(parent, title=title,
size=(250, 200))
self.ShapeTypes=['available','selected']
self.MainSizer=wx.GridBagSizer()
self.SetSizer(self.MainSizer)
cmdNew=wx.Button(self, label='New')
cmdNew.Bind(wx.EVT_BUTTON, self.onNewClick)
cmdCancel=wx.Button(self, label='Cancel')
cmdCancel.Bind(wx.EVT_BUTTON, self.CancelClick)
self.MainSizer.Add((500,0), pos=(0,0), span=(1,2)) #dummy to position Available
self.MainSizer.Add((0,200), pos=(1,0), span=(1,1)) #dummy to position Buttons
self.MainSizer.Add(cmdNew, pos=(2,2), flag=wx.LEFT|wx.TOP, border=10)
self.MainSizer.Add(cmdCancel, pos=(2,3), flag=wx.RIGHT|wx.BOTTOM|wx.TOP|wx.ALIGN_RIGHT, border=10)
self.SetBackgroundColour((246, 244, 242))
self.Initialise()
self.Center()
self.Fit()
self.Show()
def DisplayImages(self):
availableSizer=ShapeSizer(self, self.AvailableShapes, self.ShapeTypes.index('available'))
self.RefreshSizerCell(self.MainSizer, availableSizer, (1,2), (1,2))
selectedSizer=ShapeSizer(self, self.SelectedShapes, self.ShapeTypes.index('selected'))
self.RefreshSizerCell(self.MainSizer, selectedSizer, (1,1), (1,1))
def Initialise(self):
self.AvailableShapes=['square','circle','triangle','cross']
self.SelectedShapes=[]
self.DisplayImages()
def RefreshSizerCell(self, sizer, item, pos, span, flag=wx.ALL, border=10):
self.Freeze()
oldItem=sizer.FindItemAtPosition(pos)
if (oldItem !=None) and oldItem.IsWindow():
oldItem.GetWindow().Destroy()
sizer.Add(item, pos=pos, span=span, flag=flag, border=border)
self.Layout()
self.Thaw()
def GetShapeName(self, event):
imgCtrl=event.GetEventObject()
return imgCtrl.GetName()
def onAvailableShapeDClick(self, event):
shape=self.GetShapeName(event)
self.AvailableShapes.remove(shape)
self.SelectedShapes.append(shape)
self.DisplayImages()
def onSelectedShapeDClick(self, event):
shape=self.GetShapeName(event)
self.SelectedShapes.remove(shape)
self.AvailableShapes.append(shape)
self.DisplayImages()
def onNewClick(self, event):
self.Initialise()
def CancelClick(self, event):
self.Destroy()
class ShapeSizer(wx.Panel):
def __init__(self, frame, shapes, shapeType):
wx.Panel.__init__(self, frame, id=wx.ID_ANY)
if shapeType==frame.ShapeTypes.index('available'):
size=40
action=frame.onAvailableShapeDClick
elif shapeType==frame.ShapeTypes.index('selected'):
size=80
action=frame.onSelectedShapeDClick
shapeSizer=wx.GridBagSizer()
shapes.sort()
for ii in range(0, len(shapes)):
bitmap=wx.Bitmap(shapes[ii]+'.png',wx.BITMAP_TYPE_PNG)
bitmap=self.ScaleBitmap(bitmap, size, size)
img=wx.StaticBitmap(self, wx.ID_ANY, bitmap, name=shapes[ii])
img.Bind(wx.EVT_LEFT_DCLICK, action)
shapeSizer.Add(img, pos=(0,ii), flag=wx.RIGHT, border=10)
self.SetSizer(shapeSizer)
def ScaleBitmap(self, bitmap, width, height):
image = wx.ImageFromBitmap(bitmap)
image = image.Scale(width, height, wx.IMAGE_QUALITY_HIGH)
result = wx.BitmapFromImage(image)
return result
if __name__ == '__main__':
app = wx.App()
ImageSizer(None, title='Image Sizer')
app.MainLoop()
回答1:
The problem here is the lines
self.Freeze()
and
self.Thaw()
in
RefreshSizerCell()
Don't seem to be of any use in Windows :(
The line
Self.Layout()
also seems to be redundant
来源:https://stackoverflow.com/questions/32060635/wxpython-gives-different-results-on-windows-and-ubuntu