Newbie has a statusbar problem
Larry Bates
larry.bates at websafe.com
Sun Nov 4 11:46:24 PST 2007
Regan Tackett wrote:
>>> Hi Regan,
>>>
>>> I've attached a little demo.
>>>
>>> My demo doesn't use so many class like your but I think you like it.
>>> I've use wx.RadioBox insted RadioButton but I think it's the same thing
>>>
>> Giuseppe
>> Python-it.org
>
> Thanks for the reply. I understand how your code works. However, your code
> doesn't use a separate class for the panel that you create, I my code
> (unfortunately) does. Therefore, your "self" refers to the frame instance, and
> the "self" in my panel class refers to the panel's "self".
>
> In the following snippet, I have cleaned up and shortened the code that I
> previously posted and created a reference to the frame's instance in my panel
> class. I then pass that to the "SetStatusBarText" class as the parent. This
> does change the status bar text like I want, but now it creates a separate,
> blank statusbar at the top of my panel!!! I don't understand why this is
> happening.
>
> Does anyone have any suggestions as to how to change the text on the statusbar
> from a class that inherits wx.Panel? There has to be an easy way, but I'm a
> newbie and just don't see it. Thanks.
>
>
>
>
> #!/usr/bin/env python
>
> import wx
>
> class GUI(wx.App):
>
> def OnInit(self):
> self.frame = Frame(None, title = "myGUI", size = (400, 300))
> self.frame.CenterOnScreen()
> self.frame.Show()
> self.SetTopWindow(self.frame)
> return True
>
>
> class Frame(wx.Frame):
>
> def __init__(self, *args, **kwargs):
> wx.Frame.__init__(self, *args, **kwargs)
> self.SetMinSize(kwargs["size"])
>
> self.statusBar = self.CreateStatusBar()
> self.SetStatusBar(self.statusBar)
>
> self.panel = MyPanel(self, wx.ID_ANY)
>
>
> class MyPanel(wx.Panel):
> def __init__(self, *args, **kwargs):
> wx.Panel.__init__(self, *args, **kwargs)
>
> self.frame = args[0] # I create a reference to the frame here
>
> # Creates a sizer holding the radiobuttons
> self.mainSizer = wx.BoxSizer(wx.VERTICAL)
> self.mainSizer.Add(self.createRadioButton1(), 0, wx.ALL, 5)
> self.mainSizer.Add(self.createRadioButton2(), 0, wx.ALL, 5)
> self.SetSizerAndFit(self.mainSizer)
> self.Layout()
>
> def createRadioButton1(self):
> # Creates radiobutton 1
> self.RadioButton1 = wx.RadioButton(self, wx.ID_ANY, "Button 1", \
> style = wx.RB_GROUP)
> self.RadioButton1.Bind(wx.EVT_RADIOBUTTON, self.onRadio)
> return self.RadioButton1
>
> def createRadioButton2(self):
> # Creates radiobutton 2
> self.RadioButton2 = wx.RadioButton(self, wx.ID_ANY, "Button 2")
> self.RadioButton2.Bind(wx.EVT_RADIOBUTTON, self.onRadio)
> return self.RadioButton2
>
> def onRadio(self, event):
> """
> Event handler for radiobutton 2
>
> If button 2 is clicked, I want a message to appear
> on the status bar.
> """
> radioSelected = event.GetEventObject()
> if radioSelected.GetLabel() == "Button 2":
> ###############################################
> self.statusBar = SetStatusBarText(self.frame, "Radio button 2 is activated")
>
> # I pass in the frame as the parent
>
> # For some reason, this creates a separate and blank
> # statusbar on the top of my panel.
>
> ###############################################
> else:
> pass
>
>
> class SetStatusBarText(wx.StatusBar):
> """
> I pass in the frame as the parent and the text that
> I want to be displayed.
> """
> def __init__(self, parent, text):
> wx.StatusBar.__init__(self, parent)
> parent.SetStatusText(text)
>
> # "parent" is the frame instance
>
>
>
> if __name__ == "__main__":
>
> app = GUI()
> app.MainLoop()
I think your problem is this:
self.statusBar = self.CreateStatusBar()
self.SetStatusBar(self.statusBar)
Unless you want to create a custom status bar, you should just
do:
self.CreateStatusBar()
I think doing it the way you did creates two status bars 2nd to the
panel.
Also you don't need this class:
class SetStatusBarText(wx.StatusBar):
You aren't creating a custom status bar so even if you wanted to use it it
shouldn't inherit from wx.StatusBar.
just do parent.SetStatusText(text) where you want to change
statusbar contents (or self.SetStatusText(text) if you are in
the frame object).
This creates your second statusBar on the panel:
self.statusBar = SetStatusBarText(self.frame, "Radio button 2 is activated")
Change it to the following (not tested but pretty sure):
parent.SetStatusText("Radio button 2 is activated", 0)
-Larry
More information about the wxpython-users
mailing list