469,568 Members | 1,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,568 developers. It's quick & easy.

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!
Feb 7 '10 #1
3 4011
bvdet
2,851 Expert Mod 2GB
Is this what you mean?
Expand|Select|Wrap|Line Numbers
  1. >>> class MyLabel(object):
  2. ...     def __init__(self, label="Label 1"):
  3. ...         self.label = label
  4. ...         
  5. >>> def modify_label(obj, newlabel="This is a new label"):
  6. ...     obj.label = newlabel
  7. ...     
  8. >>> a = MyLabel()
  9. >>> a.label
  10. 'Label 1'
  11. >>> modify_label(a)
  12. >>> a.label
  13. 'This is a new label'
  14. >>> 
Feb 7 '10 #2
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

Expand|Select|Wrap|Line Numbers
  1. # communicate.py
  2.  
  3. import wx
  4. import urllib
  5.  
  6. global IP
  7. global myip
  8.  
  9.  
  10.  
  11. def wanip():
  12.     global IP
  13.     global myip
  14.     url=urllib.URLopener()
  15.     html=url.open('http://checkip.dyndns.com')
  16.     ans=html.read(160)
  17.     start=ans.find('IP Address')
  18.     end=ans.find('</body>')
  19.     IP = str(ans[start:end])
  20.     return IP
  21.  
  22.  
  23. class LeftPanel(wx.Panel):
  24.     def __init__(self, parent, id):
  25.         wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
  26.         global IP
  27.         self.text1 = parent.GetParent().rightPanel.text1
  28.         self.text2 = parent.GetParent().rightPanel.text2
  29.  
  30.         button1 = wx.Button(self, -1, '+', (10, 10))
  31.         button2 = wx.Button(self, -1, '-', (10, 35))
  32.         button3 = wx.Button(self, -1, 'Check WAN IP', (10,60))
  33.  
  34.         self.Bind(wx.EVT_BUTTON, self.OnPlus, id=button1.GetId())
  35.         self.Bind(wx.EVT_BUTTON, self.OnMinus, id=button2.GetId())
  36.         self.Bind(wx.EVT_BUTTON, self.CheckIP, id=button3.GetId())
  37.  
  38.     def OnPlus(self, event):
  39.         value = int(self.text1.GetLabel())
  40.         value = value + 1
  41.         self.text1.SetLabel(str(value))
  42.  
  43.     def OnMinus(self, event):
  44.         value = int(self.text1.GetLabel())
  45.         value = value - 1
  46.         self.text1.SetLabel(str(value))
  47.  
  48.     def CheckIP(self,event):
  49.         global IP
  50.         #value = str(self.text.GetLabel())
  51.         #here i call function to check IP and set label
  52.         self.text2.SetLabel(str(wanip())) #place IP instead of function
  53.  
  54. class RightPanel(wx.Panel):
  55.     def __init__(self, parent, id):
  56.         wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
  57.         self.text1 = wx.StaticText(self, -1, '0', (5, 10))
  58.         self.text2 = wx.StaticText(self, -1, 'checking IP', (5,25))
  59.  
  60. class Communicate(wx.Frame):
  61.     def __init__(self, parent, id, title):
  62.         wx.Frame.__init__(self, parent, id, title, size=(400, 200))
  63.  
  64.         panel = wx.Panel(self, -1)
  65.         self.rightPanel = RightPanel(panel, -1)
  66.  
  67.         leftPanel = LeftPanel(panel, -1)
  68.  
  69.         hbox = wx.BoxSizer()
  70.         hbox.Add(leftPanel, 1, wx.EXPAND | wx.ALL, 5)
  71.         hbox.Add(self.rightPanel, 1, wx.EXPAND | wx.ALL, 5)
  72.  
  73.         panel.SetSizer(hbox)
  74.         self.Centre()
  75.         self.Show(True)
  76.  
  77. app = wx.App()
  78. Communicate(None, -1, 'widgets communicate')
  79.  
  80. #here is (I think) reference to object in class LeftPanel, text2
  81.  
  82. #here i call my func
  83. wanip() #args
  84.  
  85. app.MainLoop()
  86.  
Feb 7 '10 #3
bvdet
2,851 Expert Mod 2GB
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.
Expand|Select|Wrap|Line Numbers
  1. # communicate.py
  2.  
  3. import wx
  4. import urllib
  5.  
  6. def wanip(obj):
  7.     url=urllib.URLopener()
  8.     html=url.open('http://checkip.dyndns.com')
  9.     ans=html.read(160)
  10.     start=ans.find('IP Address')
  11.     end=ans.find('</body>')
  12.     IP = str(ans[start:end])
  13.     obj.SetLabel(IP)
  14.     return IP
  15.  
  16. class LeftPanel(wx.Panel):
  17.     def __init__(self, parent, id):
  18.         wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
  19.         self.text1 = parent.GetParent().rightPanel.text1
  20.         self.text2 = parent.GetParent().rightPanel.text2
  21.  
  22.         button1 = wx.Button(self, -1, '+', (10, 10))
  23.         button2 = wx.Button(self, -1, '-', (10, 35))
  24.         button3 = wx.Button(self, -1, 'Check WAN IP', (10,60))
  25.         self.button3 = button3
  26.         self.Bind(wx.EVT_BUTTON, self.OnPlus, id=button1.GetId())
  27.         self.Bind(wx.EVT_BUTTON, self.OnMinus, id=button2.GetId())
  28.         self.Bind(wx.EVT_BUTTON, self.CheckIP, id=button3.GetId())
  29.  
  30.     def OnPlus(self, event):
  31.         value = int(self.text1.GetLabel())
  32.         value += 1
  33.         self.text1.SetLabel(str(value))
  34.  
  35.     def OnMinus(self, event):
  36.         value = int(self.text1.GetLabel())
  37.         value -= 1
  38.         self.text1.SetLabel(str(value))
  39.  
  40.     def CheckIP(self,event):
  41.         wanip(self.text2)
  42.  
  43. class RightPanel(wx.Panel):
  44.     def __init__(self, parent, id):
  45.         wx.Panel.__init__(self, parent, id, style=wx.BORDER_SUNKEN)
  46.         self.text1 = wx.StaticText(self, -1, '0', (5, 10))
  47.         self.text2 = wx.StaticText(self, -1, 'checking IP', (5,25))
  48.  
  49. class Communicate(wx.Frame):
  50.     def __init__(self, parent, id, title):
  51.         self.app = wx.App()
  52.         wx.Frame.__init__(self, parent, id, title, size=(400, 200))
  53.         panel = wx.Panel(self, -1)
  54.         self.rightPanel = RightPanel(panel, -1)
  55.  
  56.         leftPanel = LeftPanel(panel, -1)
  57.         self.leftPanel = leftPanel
  58.         hbox = wx.BoxSizer()
  59.         hbox.Add(leftPanel, 1, wx.EXPAND | wx.ALL, 5)
  60.         hbox.Add(self.rightPanel, 1, wx.EXPAND | wx.ALL, 5)
  61.  
  62.         panel.SetSizer(hbox)
  63.         self.Centre()
  64.         self.Show(True)
  65.  
  66. if __name__ == '__main__':
  67.     a = Communicate(None, -1, 'widgets communicate')
  68.     a.app.MainLoop()
  69.  
Feb 7 '10 #4

Post your reply

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

Similar topics

3 posts views Thread by awan | last post: by
60 posts views Thread by K. G. Suarez | last post: by
2 posts views Thread by Lance Geeck | last post: by
2 posts views Thread by Lau Lei Cheong | last post: by
6 posts views Thread by joosteto | last post: by
2 posts views Thread by babakandme | last post: by
2 posts views Thread by Peter Duniho | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.