By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,655 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

How to access a global object in another function?

P: 7
i have two buttons, which are disabled on page load,
when i select one radio button i have to make that button enable.
But when i write button1.Enable() in the radibutton click event, its showing an error "global object 'button1' not defined.

How can i tackle this problem.

How can i access a global variable in another function?
Feb 7 '11 #1

✓ answered by bvdet

Make your button available to other methods in your application by defining it like this:
Expand|Select|Wrap|Line Numbers
  1. self.button1 = wx.Button(panel,label="Export",pos=(75,120),size=(50,30))
The button and its methods will be available in other methods if accessed using self.button1

Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
To make a variable available to the global scope, declare the variables with the global statement before the variables are used in a given block of code.

For example:
Expand|Select|Wrap|Line Numbers
  1. def foo():
  2.     global foobar
  3.     foobar = "This variable will be available in the global scope"
This is not good programming style however. Most GIU applications are encapsulated in class objects where an instance of the class has a namespace implemented as a dictionary, containing the attributes defined.
Feb 7 '11 #2

P: 7
Actually I need to create a button
Expand|Select|Wrap|Line Numbers
  1. class myApp(wx.Frame):
  2. def __init__(self,parent,id):
  3.         wx.Frame.__init__(self,parent,id,'Export Form', size=(500,500))
  4.         panel =wx.Panel(self)
  5. #Creating Buttons
  6.         global button1 = wx.Button(panel,label="Export",pos=(75,120),size=(50,30))
in another click event i wnt to make this button disable, i tried with your sujjestion, it didnt worked. is there any other way
Expand|Select|Wrap|Line Numbers
  1.  def chain(self,event):
  2.         global button1.Disable()
is this possible
Feb 7 '11 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Make your button available to other methods in your application by defining it like this:
Expand|Select|Wrap|Line Numbers
  1. self.button1 = wx.Button(panel,label="Export",pos=(75,120),size=(50,30))
The button and its methods will be available in other methods if accessed using self.button1
Feb 7 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.