Newbie has a statusbar problem

Regan Tackett regantackett at comcast.net
Sat Nov 3 17:58:43 PDT 2007


In the code below, I have created a panel with 2 radio buttons on it.  When I
click and activate "Button 2", I would like to have a certain message appear on
the status bar.  However, when the code is run, the message appears at the top
of the panel.  I think my problem is that I'm setting the panel that I created
as the parent of the statusbar.  However, I don't know (or have I ever saw an
example) how to pass the frame instance to the panel.  Could some of you more
experienced programmers tell me where I've went wrong?  Below is the code.  I
tried to make it as simple as possible.

#!/usr/bin/env python

import wx
import time

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 = CustomStatusBar(self)
        self.SetStatusBar(self.statusBar)
	
	self.panel = Panel(self, wx.ID_ANY)


class Panel(wx.Panel):
    def __init__(self, *args, **kwargs):
        wx.Panel.__init__(self, *args, **kwargs)
	
	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):
        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):
        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):
        radioSelected = event.GetEventObject()
	if radioSelected.GetLabel() == "Button 2":
	    ###############################################
	    self.statusBar = SetStatusBarText(self, "Radio button 2", 0)
	    
	    # This is where I think I have a problem
	    
	    ###############################################
	else:
	    pass

class SetStatusBarText(wx.StatusBar):
    def __init__(self, parent, text, position):
        wx.StatusBar.__init__(self, parent)
	
	self.SetStatusText(text, position)


class CustomStatusBar(wx.StatusBar):
    """
    This class just creates a clock in the status bar.
    I copied this from the demo.
    """
    def __init__(self, *args, **kwargs):
        wx.StatusBar.__init__(self, *args, **kwargs)
	self.SetFieldsCount(2)
	self.SetStatusWidths([-16, -5])
	self.timer = wx.PyTimer(self.createStatusBarClock)
        self.timer.Start(10)

    def createStatusBarClock(self):

        info = time.localtime(time.time())
        dateAndTime = time.strftime("%d-%b-%Y   %I:%M:%S", info)
        self.SetStatusText(dateAndTime, 1)


if __name__ == "__main__":

    app = GUI()
    app.MainLoop()





More information about the wxpython-users mailing list