473,394 Members | 2,063 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2098
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ringwraith | last post by:
Hello! How to detect in Python that some click action was performed in some module imported by my main module? Let's assume I have a tree.py module that implements all functionality of a tree...
19
by: Grant Edwards | last post by:
I've decided to learn wxPython, and I'm afraid I just don't grok the whole "id" thing where you have to pull unique integers out of your, er, the air and then use those to refer to objects: ...
2
by: John Salerno | last post by:
Ah, the object-oriented stuff is just so FUN! :) Here's my code, followed by the error. I thought I was referring to the 'text' attribute correctly, but it seems not. import wx class...
2
by: janama | last post by:
Hi all, Using wx When adding a second timer as i have the first, the second timer adding stops the first timer (updating or stops?) . In this example im updating a uptime and localtime label....
3
by: John Salerno | last post by:
I'd be curious to know if this works any differently on other computers/platforms or while other things are happening in the background. I can't tell if it's the Timer object that isn't keep...
0
by: gopython | last post by:
Hi, I just started coding with wxPython. The idea is to be able to change the gui dynamically. I am trying to change the static text inside the panel after the panel is created without user...
12
by: Matt Bitten | last post by:
I've got a wxPython program that needs to do some drawing on a DC on a regular basis, whether or not a paint event happens. I know how to make a ClientDC to do the drawing in, and I know what...
4
by: Jimmy | last post by:
Hi, wxPython is cool and easy to use, But I ran into a problem recently when I try to write a GUI. The thing is I want to periodically update the content of StatixText object, so after create...
3
by: Gandalf | last post by:
I try to reach a specific wx StaticText element's text and to change it by clicking on a button now let's say the this is my element: wx.StaticText(panel, 15, "Hello" ,(30, 70) ,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.