2

In my wxpython program, my panel is behaving differently depending on whether I make it a derived class or a straight panel instance:

import wx
class PanelWithText(wx.Panel):
 def __init__(self, parent):
 super(PanelWithText, self).__init__(parent)
 hbox1 = wx.BoxSizer(wx.HORIZONTAL)
 panel1 = wx.Panel(parent)
 st1 = wx.StaticText(panel1, label='Some Text')
 hbox1.Add(st1)
class Example(wx.Frame):
 def __init__(self, parent, title):
 super(Example, self).__init__(parent, title=title,
 size=(390, 350))
 panel = wx.Panel(self)
 vbox = wx.BoxSizer(wx.VERTICAL)
 hbox1 = wx.BoxSizer(wx.HORIZONTAL) # comment out from here
 panel1 = wx.Panel(panel) #
 st1 = wx.StaticText(panel1, label='Some Text') #
 hbox1.Add(st1) # to here
 # panel1 = PanelWithText(panel)
 vbox.Add(panel1)
 panel.SetSizer(vbox)
 self.Centre()
 self.Show()
if __name__ == '__main__':
 app = wx.App()
 Example(None, title='Example')
 app.MainLoop()

If I run it as is, it looks fine. If I run it commenting out the four lines that create panel1 and uncommenting the line that creates panel1 using the derived class, the "Some Text" gets clipped and only shows "Sor". Worse things start happening when I make a non-trivial program.

These two seem identical to me. What is the difference?

I'm using: Python 2.7.6 wxpython 3.0.0.0 Mac Yosemite 10.10.2

asked Mar 4, 2015 at 21:30

1 Answer 1

1

The problem is in the parenting. The problem is that in the first example you have the StaticText widget's parent set correctly to panel1. In your PanelWithText class, you set its parent to the top level panel instead of the panel class, which is incorrect. Here is a fixed example:

import wx
class PanelWithText(wx.Panel):
 def __init__(self, parent):
 super(PanelWithText, self).__init__(parent)
 hbox1 = wx.BoxSizer(wx.HORIZONTAL)
 st1 = wx.StaticText(self, label='Some Text')
 hbox1.Add(st1)
class Example(wx.Frame):
 def __init__(self, parent, title):
 super(Example, self).__init__(parent, title=title,
 size=(390, 350))
 panel = wx.Panel(self)
 vbox = wx.BoxSizer(wx.VERTICAL)
 #hbox1 = wx.BoxSizer(wx.HORIZONTAL) # comment out from here
 #panel1 = wx.Panel(panel) #
 #st1 = wx.StaticText(panel1, label='Some Text') #
 #hbox1.Add(st1) # to here
 panel1 = PanelWithText(panel)
 vbox.Add(panel1)
 panel.SetSizer(vbox)
 self.Centre()
 self.Show()
if __name__ == '__main__':
 import wx.lib.mixins.inspection
 app = wx.App()
 Example(None, title='Example')
 wx.lib.inspection.InspectionTool().Show()
 app.MainLoop()
answered Mar 4, 2015 at 22:40
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that certainly works! To elaborate on the explanation for any future readers (which took me a few minutes to figure out), my version of the class was creating a third panel (one in the main frame, one as the class, and one inside the class) and the third one was messing up the second one. Eliminating the third panel and referencing the text to the second one solved the problem.
Yes, you had a lot of nested panels. I was able to figure it out using the Widget Inspection Tool, which showed me that there were a couple of panels at the top level when there should have only been one. You might want to check out that handy utility: wiki.wxpython.org/Widget%20Inspection%20Tool

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.