How to change label from "any" function or outside from class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • saskoPython
    New Member
    • Feb 2010
    • 3

    How to change label from "any" function or outside from class

    Hello,

    I go thrugh number of tutorials and steel can't find one simple thing (or I can't understand how to do this). My question is this:

    if we make "any" function like this:

    def myfunc():
    #code here:
    return

    and have a class (gtk or wxWidget) in which we create a Label

    class MyLabel():
    #code here

    #end class
    mainloop()

    how to change Label inside class MyLabel from myfunc?
    I understand how buttons and events work, but i have no idea how to change label, or text from function outside class. ie time triggered timer or any result returned by function which periodicaly change its value?

    or is there any way to make label autoupdating? / refreshing?

    Tnx for your time and sorry for my english!
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Is this what you mean?
    Code:
    >>> class MyLabel(object):
    ... 	def __init__(self, label="Label 1"):
    ... 		self.label = label
    ... 		
    >>> def modify_label(obj, newlabel="This is a new label"):
    ... 	obj.label = newlabel
    ... 	
    >>> a = MyLabel()
    >>> a.label
    'Label 1'
    >>> modify_label(a)
    >>> a.label
    'This is a new label'
    >>>

    Comment

    • saskoPython
      New Member
      • Feb 2010
      • 3

      #3
      here is the source

      Here is source from tutorial, I made some small changes
      In short function wanip gets WAN IP and stores it in variable
      Button 3 on click calls it and change text2
      I wish to change text2 by simply call that function - wanip or any else
      how to do that?

      here is the source code
      tnx for help

      Code:
      # communicate.py
      
      import wx
      import urllib
      
      global IP
      global myip
      
      
      
      def wanip():
          global IP
          global myip
          url=urllib.URLopener()
          html=url.open('http://checkip.dyndns.com')
          ans=html.read(160)
          start=ans.find('IP Address')
          end=ans.find('</body>')
          IP = str(ans[start:end])
          return IP
      
      
      class LeftPanel(wx.Panel):
          def __init__(self, parent, id):
              wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
              global IP
              self.text1 = parent.GetParent().rightPanel.text1
              self.text2 = parent.GetParent().rightPanel.text2
      
              button1 = wx.Button(self, -1, '+', (10, 10))
              button2 = wx.Button(self, -1, '-', (10, 35))
              button3 = wx.Button(self, -1, 'Check WAN IP', (10,60))
      
              self.Bind(wx.EVT_BUTTON, self.OnPlus, id=button1.GetId())
              self.Bind(wx.EVT_BUTTON, self.OnMinus, id=button2.GetId())
              self.Bind(wx.EVT_BUTTON, self.CheckIP, id=button3.GetId())
      
          def OnPlus(self, event):
              value = int(self.text1.GetLabel())
              value = value + 1
              self.text1.SetLabel(str(value))
      
          def OnMinus(self, event):
              value = int(self.text1.GetLabel())
              value = value - 1
              self.text1.SetLabel(str(value))
      
          def CheckIP(self,event):
              global IP
              #value = str(self.text.GetLabel())
              #here i call function to check IP and set label
              self.text2.SetLabel(str(wanip())) #place IP instead of function
      
      class RightPanel(wx.Panel):
          def __init__(self, parent, id):
              wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
              self.text1 = wx.StaticText(self, -1, '0', (5, 10))
              self.text2 = wx.StaticText(self, -1, 'checking IP', (5,25))
      
      class Communicate(wx.Frame):
          def __init__(self, parent, id, title):
              wx.Frame.__init__(self, parent, id, title, size=(400, 200))
      
              panel = wx.Panel(self, -1)
              self.rightPanel = RightPanel(panel, -1)
      
              leftPanel = LeftPanel(panel, -1)
      
              hbox = wx.BoxSizer()
              hbox.Add(leftPanel, 1, wx.EXPAND | wx.ALL, 5)
              hbox.Add(self.rightPanel, 1, wx.EXPAND | wx.ALL, 5)
      
              panel.SetSizer(hbox)
              self.Centre()
              self.Show(True)
      
      app = wx.App()
      Communicate(None, -1, 'widgets communicate')
      
      #here is (I think) reference to object in class LeftPanel, text2
      
      #here i call my func
      wanip() #args
      
      app.MainLoop()

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Eliminate the global statements - you don't need them. It's not good practice to use global variables anyway. Pass the object you want to modify to your function, and use method SetLabel() in the function.
        Code:
        # communicate.py
         
        import wx
        import urllib
        
        def wanip(obj):
            url=urllib.URLopener()
            html=url.open('http://checkip.dyndns.com')
            ans=html.read(160)
            start=ans.find('IP Address')
            end=ans.find('</body>')
            IP = str(ans[start:end])
            obj.SetLabel(IP)
            return IP
        
        class LeftPanel(wx.Panel):
            def __init__(self, parent, id):
                wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
                self.text1 = parent.GetParent().rightPanel.text1
                self.text2 = parent.GetParent().rightPanel.text2
         
                button1 = wx.Button(self, -1, '+', (10, 10))
                button2 = wx.Button(self, -1, '-', (10, 35))
                button3 = wx.Button(self, -1, 'Check WAN IP', (10,60))
                self.button3 = button3
                self.Bind(wx.EVT_BUTTON, self.OnPlus, id=button1.GetId())
                self.Bind(wx.EVT_BUTTON, self.OnMinus, id=button2.GetId())
                self.Bind(wx.EVT_BUTTON, self.CheckIP, id=button3.GetId())
         
            def OnPlus(self, event):
                value = int(self.text1.GetLabel())
                value += 1
                self.text1.SetLabel(str(value))
         
            def OnMinus(self, event):
                value = int(self.text1.GetLabel())
                value -= 1
                self.text1.SetLabel(str(value))
         
            def CheckIP(self,event):
                wanip(self.text2)
         
        class RightPanel(wx.Panel):
            def __init__(self, parent, id):
                wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
                self.text1 = wx.StaticText(self, -1, '0', (5, 10))
                self.text2 = wx.StaticText(self, -1, 'checking IP', (5,25))
         
        class Communicate(wx.Frame):
            def __init__(self, parent, id, title):
                self.app = wx.App()
                wx.Frame.__init__(self, parent, id, title, size=(400, 200))
                panel = wx.Panel(self, -1)
                self.rightPanel = RightPanel(panel, -1)
         
                leftPanel = LeftPanel(panel, -1)
                self.leftPanel = leftPanel
                hbox = wx.BoxSizer()
                hbox.Add(leftPanel, 1, wx.EXPAND | wx.ALL, 5)
                hbox.Add(self.rightPanel, 1, wx.EXPAND | wx.ALL, 5)
         
                panel.SetSizer(hbox)
                self.Centre()
                self.Show(True)
        
        if __name__ == '__main__':
            a = Communicate(None, -1, 'widgets communicate')
            a.app.MainLoop()

        Comment

        Working...