Search code examples
python-2.7wxpython

How to print the variable of a class in python


I am trying to print the variable self.result. The ultimate goal is to use that for further processing, but for now just want to access the variable, so I chose it to print, however I am getting this message:

"wx._controls.StaticText; proxy of Swig Object of type 'wxStaticText *' at 0x23fed48> "

My code is below, any help is appreciated.

import wx

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

    self.panel = wx.Panel(self)
    self.quote = wx.StaticText(self.panel, label="is Awesome")
    self.result = wx.StaticText(self.panel, label="")
    self.result.SetForegroundColour(wx.RED)
    self.button = wx.Button(self.panel, label="Save")
    self.lblname = wx.StaticText(self.panel, label="Your name:")
    self.editname = wx.TextCtrl(self.panel, size=(140, -1))

    # Set sizer for the frame, so we can change frame size to match widgets
    self.windowSizer = wx.BoxSizer()
    self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND)

    # Set sizer for the panel content
    self.sizer = wx.GridBagSizer(5, 5)
    self.sizer.Add(self.quote, (0, 1))
    self.sizer.Add(self.result, (0, 0))
    self.sizer.Add(self.lblname, (1, 0))
    self.sizer.Add(self.editname, (1, 1))
    self.sizer.Add(self.button, (2, 0), (1, 2), flag=wx.EXPAND)

    # Set simple sizer for a nice border
    self.border = wx.BoxSizer()
    self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 5)

    # Use the sizers
    self.panel.SetSizerAndFit(self.border)
    self.SetSizerAndFit(self.windowSizer)

    # Set event handlers
    self.button.Bind(wx.EVT_BUTTON, self.OnButton)

def OnButton(self, e):
    self.result.SetLabel(self.editname.GetValue())



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

Solution

  • Your question makes no sense: Why would you want to read out the label of a static text? Its label (!, StaticText has no value) is set by the OnButton event, reading the value of the TextCtrl named editname (I think it is that what you are searching). But to answer your question: How to read a label from a StaticText change:

    print frame.result
    

    to

    print frame.result.GetLabel() # or GetLabelText()
    

    This will result in an empty string because the label is not set yet directly after frame creation.

    See documentation parent object of StaticText.