Simple wxPython SetLabel question

D

dp_pearce

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
 
C

Cédric Lucantis

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.
 
D

dp_pearce

Thank you very much Cédric. I thought it would be something very
straight forward.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top