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!
3 4345 bvdet 2,851
Expert Mod 2GB
Is this what you mean? - >>> 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'
-
>>>
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 - # 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()
-
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. -
# 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()
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: awan |
last post by:
Is there a function in dotnet which is equivalent as "app.Path" in VB 6?
Thanks in advance!
|
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...
|
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 !
|
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...
|
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...
|
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...
|
by: Brahm |
last post by:
hi folks !
What is the equivalent parameter in vb 2005 for "any" from vb 6 ?
Thanks,
Daniel
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
| |