Simple wxPython SetLabel question

Cédric Lucantis omer at no-log.org
Thu Jun 19 12:06:42 CEST 2008


Le Thursday 19 June 2008 11:48:54 dp_pearce, vous avez écrit :
> Hi All,
>
> Apologies if this should be seriously obvious. But I am quite new to
> Python and it is not quite so obvious yet.
>
> I have a GUI which will eventually load and display database
> information. I want the user to be able to browse for a database and
> then load it. My problem relates to how I set the value of a TextCtrl
> once the user has selected the database they wish to load.
>
> Here is a snip of my code:
>
> import wx
> import os
>
> class TestFrame(wx.Frame):
>     def __init__(self):
>         wx.Frame.__init__(self, None, -1, "Code Snip")
>         panel       = wx.Panel(self)
>
>         databaseLbl = wx.StaticText(panel, -1, "Database:")
>         database    = wx.TextCtrl(panel, -1, "")
>         databaseBtn = wx.Button(panel, -1, "Browse")
>         self.Bind(wx.EVT_BUTTON, self.OnBrowse, databaseBtn)
>         fetchSizer  = wx.BoxSizer(wx.HORIZONTAL)
>         fetchSizer.Add(databaseLbl)
>         fetchSizer.Add(database, -1, wx.LEFT |wx.RIGHT, 5)
>         fetchSizer.Add(databaseBtn)
>
>         panel.SetSizer(fetchSizer)
>
>     def OnBrowse(self, event):
>         wildcard = "Access Database (*.mdb) | *.mdb | Access Database
> (*.MDB) | *.MDB | All Files (*.*) | *.*"
>         dialog   = wx.FileDialog(None, "Choose an database",
> os.getcwd(), "", wildcard, wx.OPEN)
>
>         if dialog.ShowModal() == wx.ID_OK:
>             path = dialog.GetPath()
>             #########################################
>             # NOW SET TEXTCTRL "database" TO "path"
>             panel.database.SetLabel(path)
>             #########################################
>         dialog.Destroy()
>
> app = wx.PySimpleApp()
> TestFrame().Show()
> app.MainLoop()
>
> The current code returns that "global name 'panel' is not defined" 

'panel' is local to your __init__ function, so it's not available elsewhere. 
You should store it as an instance attribute instead :

# in __init__:
self.panel = wx.Panel(self)

# in OnBrowse
self.panel.database.SetLabel(patrh)

note that unlike some other languages, panel and self.panel are two distinct 
variables, so you should replace _all_ references to panel by self.panel.

-- 
Cédric Lucantis



More information about the Python-list mailing list