user1442957
user1442957

Reputation: 7709

wxpython change text after function call

On click of my "browse" button, the text in my textbox "tc1" does not change. I get a console error of "global name 'tc1' is not defined". I just want the text of tc1 to change when I click button1

python:

def InitUI(self):

  panel = wx.Panel(self)

  button1 = wx.Button(panel, label="Browse...")
  button1.Bind(wx.EVT_BUTTON, self.OnBrowse)

  tc1 = wx.TextCtrl(panel, -1, "Text")

def OnBrowse(self, event):
  return tc1.SetValue("New Text")

Upvotes: 0

Views: 185

Answers (2)

Joran Beasley
Joran Beasley

Reputation: 113978

you must declare it global This should be an attribute of the class

self.tc1 = None
def InitUI(self):
  button1 = wx.Button(panel, label="Browse...")
  button1.Bind(wx.EVT_BUTTON, self.OnBrowse)
  self.tc1 = wx.TextCtrl(panel, -1, "Text")

def OnBrowse(self, event):
  return self.tc1.SetValue("New Text")

Upvotes: 1

unutbu
unutbu

Reputation: 879471

You are defining a class for the GUI and the TextCtrl is part of that GUI, so I think it makes sense to make tc1 an attribute of self:

def InitUI(self):
  ...    
  self.tc1 = wx.TextCtrl(panel, -1, "Text")

def OnBrowse(self, event):
  return self.tc1.SetValue("New Text")

Upvotes: 1

Related Questions