调整wxPython中面板大小时的图形问题

时间:2014-04-14 18:55:56

标签: python image python-2.7 wxpython

我在Windows 8操作系统上使用python v2.7和wxPython v3.0。 (很抱歉标题混乱。我不知道标题应该是什么。我会尽力详细解释我的问题。)

在我的应用程序中,我有一个带有背景图像的滚动面板。此面板在代码段中命名为mainPanel。此mainPanel包含名为myPanelAmyPanelB且具有透明背景的其他面板。这些面板myPanelAmyPanelB包含包含一些按钮的sizer。 PS:这只是我真实世界应用的一个示例。在我的真实世界应用程序中,我有许多不同的面板和按钮。在这种情况下,补丁更大,更烦人。 :(

问题:当我横向调整窗口大小时,有时候(在我的真实应用程序中,与此示例应用程序相比,它非常频繁。)我看到空白补丁在我的应用程序窗口中,如下面的示例图像所示。我怎么能避免这个? 如果有人可以测试这个并报告他们的计算机上是否存在相同的问题,那将是很好的(只是为了确保这不是Windows操作系统问题。我在Windows 7上测试过,它也存在同样的问题。)< / em>的

更新:只需调整窗口大小并垂直滚动,您就会经常看到这些补丁。

示例图片:修补程序由箭头指向。 example1 example2 example3

代码:以下是我的代码示例。可以从此处下载背景图像。 greensquares.jpg

import wx
import wx.lib.scrolledpanel

class gui(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, None, id, title, style=wx.DEFAULT_FRAME_STYLE)
        mainPanel = wx.lib.scrolledpanel.ScrolledPanel(self, -1)
        mainPanel.SetupScrolling()
        myImage = wx.Image('greensquares.jpg', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        myBitmap = wx.StaticBitmap(mainPanel, -1, myImage, (0, 0))
        myPanelA = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,0), size=(150,150))
        wx.StaticText(myPanelA, -1, '     MyPanelA    ')
        myButton1A = wx.Button(myPanelA, -1, size=(20,20))
        myButton2A = wx.Button(myPanelA, -1, size=(20,20))
        mySizerA = wx.BoxSizer(wx.HORIZONTAL)
        mySizerA.Add(myButton1A, 0, wx.ALL, 20)
        mySizerA.Add(myButton2A, 0, wx.ALL, 20)
        myPanelA.SetSizer(mySizerA)
        myPanelA.Layout()

        myPanelB = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,130), size=(250,200))
        wx.StaticText(myPanelB, -1, '      MyPanelB    ')
        myButton1B = wx.Button(myPanelB, -1, size=(20,20))
        myButton2B = wx.Button(myPanelB, -1, size=(20,20))
        myButton3B = wx.Button(myPanelB, -1, size=(20,20))
        myButton4B = wx.Button(myPanelB, -1, size=(20,20))
        mySizerB1 = wx.BoxSizer(wx.HORIZONTAL)
        mySizerB1.Add(myButton1B, 0, wx.ALL, 20)
        mySizerB1.Add(myButton2B, 0, wx.ALL, 20)
        mySizerB1.Add(myButton3B, 0, wx.ALL, 20)
        mySizerB1.Add(myButton4B, 0, wx.ALL, 20)
        myButton5B = wx.Button(myPanelB, -1, size=(20,20))
        myButton6B = wx.Button(myPanelB, -1, size=(20,20))
        myButton7B = wx.Button(myPanelB, -1, size=(20,20))
        myButton8B = wx.Button(myPanelB, -1, size=(20,20))
        mySizerB2 = wx.BoxSizer(wx.HORIZONTAL)
        mySizerB2.Add(myButton5B, 0, wx.ALL, 20)
        mySizerB2.Add(myButton6B, 0, wx.ALL, 20)
        mySizerB2.Add(myButton7B, 0, wx.ALL, 20)
        mySizerB2.Add(myButton8B, 0, wx.ALL, 20)
        mySizerC = wx.BoxSizer(wx.VERTICAL)
        mySizerC.Add(mySizerB1)
        mySizerC.Add(mySizerB2)
        myPanelB.SetSizer(mySizerC)
        myPanelB.Layout()

if __name__ == '__main__':
    app = wx.App()
    frame = gui(parent=None, id=-1, title="My-App")
    frame.Show()
    app.MainLoop()

感谢您的时间。

2 个答案:

答案 0 :(得分:1)

我在你的面板上添加了一个Update方法,这似乎解决了我的问题。

import wx
import wx.lib.scrolledpanel

class gui(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, None, id, title, style=wx.DEFAULT_FRAME_STYLE)
        mainPanel = wx.lib.scrolledpanel.ScrolledPanel(self, -1)
        mainPanel.SetupScrolling()
        myImage = wx.Image('greensquares.jpg', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        myBitmap = wx.StaticBitmap(mainPanel, -1, myImage, (0, 0))
        self.myPanelA = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,0), size=(150,150))
        wx.StaticText(self.myPanelA, -1, '     self.myPanelA    ')
        myButton1A = wx.Button(self.myPanelA, -1, size=(20,20))
        myButton2A = wx.Button(self.myPanelA, -1, size=(20,20))
        mySizerA = wx.BoxSizer(wx.HORIZONTAL)
        mySizerA.Add(myButton1A, 0, wx.ALL, 20)
        mySizerA.Add(myButton2A, 0, wx.ALL, 20)
        self.myPanelA.SetSizer(mySizerA)
        self.myPanelA.Layout()

        self.myPanelB = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,130), size=(250,200))
        wx.StaticText(self.myPanelB, -1, '      self.myPanelB    ')
        myButton1B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton2B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton3B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton4B = wx.Button(self.myPanelB, -1, size=(20,20))
        mySizerB1 = wx.BoxSizer(wx.HORIZONTAL)
        mySizerB1.Add(myButton1B, 0, wx.ALL, 20)
        mySizerB1.Add(myButton2B, 0, wx.ALL, 20)
        mySizerB1.Add(myButton3B, 0, wx.ALL, 20)
        mySizerB1.Add(myButton4B, 0, wx.ALL, 20)
        myButton5B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton6B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton7B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton8B = wx.Button(self.myPanelB, -1, size=(20,20))
        mySizerB2 = wx.BoxSizer(wx.HORIZONTAL)
        mySizerB2.Add(myButton5B, 0, wx.ALL, 20)
        mySizerB2.Add(myButton6B, 0, wx.ALL, 20)
        mySizerB2.Add(myButton7B, 0, wx.ALL, 20)
        mySizerB2.Add(myButton8B, 0, wx.ALL, 20)
        mySizerC = wx.BoxSizer(wx.VERTICAL)
        mySizerC.Add(mySizerB1)
        mySizerC.Add(mySizerB2)
        self.myPanelB.SetSizer(mySizerC)
        self.myPanelB.Layout()

        self.Bind(wx.EVT_SIZE, self.OnResize)

    def OnResize(self, e):
        self.myPanelA.Update()
        self.myPanelB.Update()
        e.Skip()

if __name__ == '__main__':
    app = wx.App()
    frame = gui(parent=None, id=-1, title="My-App")
    frame.Show()
    app.MainLoop()

答案 1 :(得分:0)

我终于找到了解决方案。除了multiphrenic建议的内容之外,我还将滚动事件绑定到OnResize()。因此,当任何滚动事件EVT_SCROLL发生时,面板也将更新。它适用于Windows7,8 OS,python v2.7和wxPython v3.0。

工作代码:

import wx
import wx.lib.scrolledpanel

class gui(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, None, id, title, style=wx.DEFAULT_FRAME_STYLE)
        mainPanel = wx.lib.scrolledpanel.ScrolledPanel(self, -1)
        mainPanel.SetupScrolling()
        myImage = wx.Image('greensquares.jpg', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        myBitmap = wx.StaticBitmap(mainPanel, -1, myImage, (0, 0))
        self.myPanelA = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,0), size=(150,150))
        wx.StaticText(self.myPanelA, -1, 'self.myPanelA')
        myButton1A = wx.Button(self.myPanelA, -1, size=(20,20))
        myButton2A = wx.Button(self.myPanelA, -1, size=(20,20))
        mySizerA = wx.BoxSizer(wx.HORIZONTAL)
        mySizerA.Add(myButton1A, 0, wx.ALL, 20)
        mySizerA.Add(myButton2A, 0, wx.ALL, 20)
        self.myPanelA.SetSizer(mySizerA)
        self.myPanelA.Layout()

        self.myPanelB = wx.Panel(myBitmap, -1, style=wx.TRANSPARENT_WINDOW, pos=(100,130), size=(250,200))
        wx.StaticText(self.myPanelB, -1, 'self.myPanelB')
        myButton1B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton2B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton3B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton4B = wx.Button(self.myPanelB, -1, size=(20,20))
        mySizerB1 = wx.BoxSizer(wx.HORIZONTAL)
        mySizerB1.Add(myButton1B, 0, wx.ALL, 20)
        mySizerB1.Add(myButton2B, 0, wx.ALL, 20)
        mySizerB1.Add(myButton3B, 0, wx.ALL, 20)
        mySizerB1.Add(myButton4B, 0, wx.ALL, 20)
        myButton5B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton6B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton7B = wx.Button(self.myPanelB, -1, size=(20,20))
        myButton8B = wx.Button(self.myPanelB, -1, size=(20,20))
        mySizerB2 = wx.BoxSizer(wx.HORIZONTAL)
        mySizerB2.Add(myButton5B, 0, wx.ALL, 20)
        mySizerB2.Add(myButton6B, 0, wx.ALL, 20)
        mySizerB2.Add(myButton7B, 0, wx.ALL, 20)
        mySizerB2.Add(myButton8B, 0, wx.ALL, 20)
        mySizerC = wx.BoxSizer(wx.VERTICAL)
        mySizerC.Add(mySizerB1)
        mySizerC.Add(mySizerB2)
        self.myPanelB.SetSizer(mySizerC)
        self.myPanelB.Layout()

        self.Bind(wx.EVT_SIZE, self.OnResize)
        self.Bind(wx.EVT_SCROLL, self.OnResize)

    def OnResize(self, e):
        self.myPanelA.Update()
        self.myPanelB.Update()
        e.Skip()

if __name__ == '__main__':
    app = wx.App()
    frame = gui(parent=None, id=-1, title="My-App")
    frame.Show()
    app.MainLoop()