简体   繁体   中英

size of panel in panel using wxPython incorrect

I have a problem with wxPython. I have a frame and in that frame, I have a panel. In that panel, I want another panel of fixed size (100x100). My code is below:

import wx

class TestPanel(wx.Panel):
    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)
        self.SetBackgroundColour(wx.RED)
        self.SetSize((100, 100))


class ExampleFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.panel = wx.Panel(self)
        self.panel.SetMinSize((-1, -1))
        self.panel.SetBackgroundColour(wx.WHITE)

        self.filesize = TestPanel(self.panel, -1)

        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        hSizer.Add(self.filesize, 0, wx.CENTER)

        vSizer = wx.BoxSizer(wx.VERTICAL)
        vSizer.Add(hSizer, 1, wx.EXPAND)
        self.panel.SetSizer(vSizer)


app = wx.App(False)
frame = ExampleFrame(None)
frame.Show()
app.MainLoop()

When I run the program, my TestPanel widget is only 1x1 pixels. However, when I print the size using the GetSize method, it says that the size is 100x100. Why does it display as a 1x1 red block in this case?

I'm using wxPython 3.0.2.0 on OS X.

I'm not sure why SetSize() doesn't work, but you can just pass the size to the constructor. That worked for me on Windows 7 with wxPython 3.0.2 where calling SetSize did not.

import wx

class TestPanel(wx.Panel):
    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, size=(100, 100), *args)
        self.SetBackgroundColour('red')
        self.Layout()


class ExampleFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.panel = wx.Panel(self)
        self.panel.SetMinSize((-1, -1))
        self.panel.SetBackgroundColour(wx.WHITE)

        self.filesize = TestPanel(self.panel, -1)

        hSizer = wx.BoxSizer(wx.HORIZONTAL)
        hSizer.Add(self.filesize, 0, wx.CENTER)

        vSizer = wx.BoxSizer(wx.VERTICAL)
        vSizer.Add(hSizer, 1, wx.EXPAND)
        self.panel.SetSizer(vSizer)

import wx.lib.inspection
app = wx.App(False)
frame = ExampleFrame(None)
frame.Show()
wx.lib.inspection.InspectionTool().Show()
app.MainLoop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM