wxPython 在 Windows 和 Ubuntu 上给出不同的结果 [英] wxPython gives different results on Windows and Ubuntu

查看:25
本文介绍了wxPython 在 Windows 和 Ubuntu 上给出不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个小的 wxPython 程序,它在 Windows 和 Ubuntu 中提供完全不同的输出(我很高兴被告知我对它进行了错误的编程,我认为这是结果 - 只要我们可以让它工作)

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.

Windows 上的问题(请参阅在 Windows 和 Ubuntu 中执行相同操作后截取的屏幕截图):按钮无法正确呈现;当我点击十字、圆环或正方形时,它们会正确移动到屏幕左侧,但三角形的多个图像仍然存在;图片出现在左上角

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

这些问题都没有出现在 Ubuntu 中

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()

推荐答案

这里的问题是线条

self.Freeze()

self.Thaw() 

RefreshSizerCell()

在 Windows 中似乎没有任何用处 :(

Don't seem to be of any use in Windows :(

线

Self.Layout()

似乎也是多余的

这篇关于wxPython 在 Windows 和 Ubuntu 上给出不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆