wxPython, Set value of StaticText()

RailsSon picture RailsSon · Nov 16, 2008 · Viewed 44k times · Source

I am making a little GUI frontend for a app at the moment using wxPython.

I am using wx.StaticText() to create a place to hold some text, code below:

content = wx.StaticText(panel, -1, "Text Here", style=wx.ALIGN_CENTRE)

I have a button when clicked retrieves data from MySQL, I am wanting to change the value of the StaticText() to the MySQL data or what else could I use the hold the data.

I have tried using the below method:

contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE | wx.HSCROLL)
content.SetValue("New Text")

This displays the data fine but after the data is loaded you can edit the data and I do not want this.

Hope you guys understand what I am trying to do, I am new to Python :)

Cheers

Answer

DrBloodmoney picture DrBloodmoney · Nov 16, 2008

If you are using a wx.StaticText() you can just:

def __init__(self, parent, *args, **kwargs): #frame constructor, etc.
    self.some_text = wx.StaticText(panel, wx.ID_ANY, label="Awaiting MySQL Data", style=wx.ALIGN_CENTER)

def someFunction(self):
    mysql_data = databasemodel.returnData() #query your database to return a string
    self.some_text.SetLabel(mysql_data)

As litb mentioned, the wxWidgets docs are often much easier to use than the wxPython docs. In order to see that the SetLabel() function can be applied to a wx.StaticText instance, you have to travel up the namespace hierarchy in the wxPython docs to the wxWindow superclass, from which wx.StaticText is subclassed. There are a few things different in wxPython from wxWidgets, and it can be challenging to find out what they are. Fortunately, a lot of the time, the differences are convenience functions that have been added to wxPython and are not found in wxWidgets.