473,943 Members | 19,529 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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 4371
bvdet
2,851 Recognized Expert Moderator Specialist
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
saskoPython
3 New Member
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 Recognized Expert Moderator Specialist
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
2287
by: awan | last post by:
Is there a function in dotnet which is equivalent as "app.Path" in VB 6? Thanks in advance!
0
1541
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 packet. Can anyone tell me how can I handle the case of "Any" during serialization and how to de-serialize the soap-packet containg the field the "Any". As for de-serization we have to give the class name which have the field of fix Type.
2
2139
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
4024
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 Language" by Ritchie and Kernigahn and felt bad. Does anyone have experience with this book? I feel that it is helping me along pretty well. But how much will this book teach me? What would be the next book to read?
2
12571
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 is not supported in a declare statement" during the automated Conversion process. Some examples of this are: Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory"(ByRef
2
1966
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 as an application. It reads a config file to see what is allowed to pass, and what is depreciated, and whether fails the deperciated one. Basically, it'll only render what W3C recommandations and nothing more. It'll not render any...
5
3044
by: Brahm | last post by:
hi folks ! What is the equivalent parameter in vb 2005 for "any" from vb 6 ? Thanks, Daniel
6
1944
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 fixed class (33.2), any member of a fixed class (33.6), to a fixed function of any object (functiods, 33.10). What I wanted was to be able to hand any member function of any created object to a signal hander etc.
2
2026
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 something vice- versa. As we know with """Stringizing Operator (#)""", we can get the stirng name of a class or ... str <--- #ClassA
2
1392
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 reasonably straight-forward and natural thing to access from .NET using the p/invoke stuff. You may be referring to the DirectShow.NET project, which I haven't used but understand to be a pretty good wrapper for DirectShow.
0
10136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11535
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11126
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8220
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6309
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4911
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3512
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.