473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 4345
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

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

Similar topics

3
by: awan | last post by:
Is there a function in dotnet which is equivalent as "app.Path" in VB 6? Thanks in advance!
0
by: Omkar Singh | last post by:
I am using XmlSerialization and XmlDeserialization for making soap Body part. Then I am making sopa-header and other part of soap envelope manually. At last joining all part to get complete soap...
2
by: Ganesh | last post by:
I have to change the tartget property of a windows start->Program->X's shortcut properties using c program. Do we have any function for that ? please reply me ASAP.. as it is urgent !
60
by: K. G. Suarez | last post by:
Hello everyone. I am new to programming and my uncle gave me a copy of "C For Dummies 2nd Edition". I am up to chapter 9 right now. He probably saw me struggling with "The C Programming...
2
by: Lance Geeck | last post by:
I have many items that I lifted off from Microsoft's website several years ago. These samples were in VB6. I now want to convert an application to VB.NET. I am getting an error that says "As Any...
2
by: Lau Lei Cheong | last post by:
Hello, Actually, I'm wondering if there's anything of the sort avaliable in the wild - a developer oriented W3C browser. It's kinda W3C's online validation service, just that it runs on locahost...
5
by: Brahm | last post by:
hi folks ! What is the equivalent parameter in vb 2005 for "any" from vb 6 ? Thanks, Daniel
6
by: joosteto | last post by:
Subject: pointer to any member function of any class. The "C++ FAQ Lite" explains how to hand a pointer to a member function to a signal handler etc, but only to a static function (33.2), to a...
2
by: babakandme | last post by:
Hi everybody:D I've a string that contains the name of a class. Some members told that I can use """Stringizing Operator (#)""", but the problem is here, that I have the string, & I want...
2
by: Peter Duniho | last post by:
On Fri, 18 Jul 2008 10:41:23 -0700, jmDesktop <needin4mation@gmail.com> wrote: Well, that's an interesting example, if for no other reason than that the DirectShow video capture stuff is a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.