472,122 Members | 1,475 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 software developers and data experts.

Simple wxPython SetLabel question

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
Jun 27 '08 #1
3 2016
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
Jun 27 '08 #2
Thank you very much Cédric. I thought it would be something very
straight forward.
Jun 27 '08 #3
On Jun 19, 5:25*am, dp_pearce <dp_pea...@hotmail.comwrote:
Thank you very much Cédric. I thought it would be something very
straight forward.
Just an FYI. There's also a wxPython specific mailing list that I
highly recommend. You can learn a lot just reading other people issues
and how they get them fixed.

Here's the link: http://wxpython.org/maillist.php

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org
Python Extension Building Network: http://www.pythonlibrary.org
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Grant Edwards | last post: by
2 posts views Thread by John Salerno | last post: by
2 posts views Thread by janama | last post: by
3 posts views Thread by John Salerno | last post: by
reply views Thread by gopython | last post: by
12 posts views Thread by Matt Bitten | last post: by
4 posts views Thread by Jimmy | last post: by
3 posts views Thread by Gandalf | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.