Simple wxPython SetLabel question

dp_pearce dp_pearce at hotmail.com
Thu Jun 19 11:48:54 CEST 2008


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" so
I must be referring to it in the wrong way. Can any body help? Any
directions towards any well recommended tutorials would also be
appreciated.

Thank you in advance for your time.

DevonDan



More information about the Python-list mailing list