How to access a global object in another function?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jay manu
    New Member
    • Feb 2011
    • 7

    How to access a global object in another function?

    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?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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:
    Code:
    def foo():
        global foobar
        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.

    Comment

    • jay manu
      New Member
      • Feb 2011
      • 7

      #3
      Actually I need to create a button
      Code:
      class myApp(wx.Frame):
      def __init__(self,parent,id):
              wx.Frame.__init__(self,parent,id,'Export Form', size=(500,500))
              panel =wx.Panel(self)
      #Creating Buttons
              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
      Code:
       def chain(self,event):
              global button1.Disable()
      is this possible
      Last edited by bvdet; Feb 7 '11, 01:53 PM. Reason: Add code tags

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Make your button available to other methods in your application by defining it like this:
        Code:
        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

        Comment

        Working...