OOP Concept

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2007
Posts: 1,401
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

OOP Concept

 
0
  #1
Oct 11th, 2008
Hello All,
Greetings!
I have question concerning OOP with Python. I want to know when Python class or being specific, how py class ends in package like wxpython.

What I mean is I have one class let say of frame and another class of a dialog box all in same py file. When Frame runs, uses self i.e self.button = wx.Button. This object belongs to frame. And let say we have text control in the other class (dialog). How do I call method of another class from the other class, i.e how do I call button from dialog and dialog from button.

Suppose I want to manipulate the button, let say its label, but from another class which it doesn't belong (In this case Dialog)?

I hhave done these without grasp thge real concept. I have dug up but I find difficult to grasp it wel; so, need your support!
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 174
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: OOP Concept

 
0
  #2
Oct 11th, 2008
Maybe this short example will help:
  1. # reference across class instances
  2. # from one panel class to another panel class
  3.  
  4. import wx
  5. import time
  6.  
  7. class MyPanel1(wx.Panel):
  8. def __init__(self, parent):
  9. wx.Panel.__init__(self, parent, wx.ID_ANY,
  10. pos=(0,0), size=(300, 100))
  11. self.SetBackgroundColour("green")
  12. self.button = wx.Button(self, label=" panel1 button ")
  13. self.button.Bind(wx.EVT_BUTTON, self.buttonClick )
  14.  
  15. def buttonClick(self, event):
  16. """
  17. use the panel2 label and
  18. reference via the parent --> frame
  19. """
  20. # display something that indicates a change
  21. s = time.strftime("%H:%M:%S", time.localtime())
  22. frame.panel2.label.SetLabel(s)
  23.  
  24.  
  25. class MyPanel2(wx.Panel):
  26. def __init__(self, parent):
  27. wx.Panel.__init__(self, parent, wx.ID_ANY,
  28. pos=(0, 100), size=(300, 100))
  29. self.SetBackgroundColour("yellow")
  30. self.label = wx.StaticText(self, wx.ID_ANY, " panel2 label ")
  31.  
  32.  
  33. app = wx.App(0)
  34. # create a frame, no parent, default ID, title, size
  35. caption = "wx.Panel()"
  36. frame = wx.Frame(None, wx.ID_ANY, caption, size=(300, 220))
  37.  
  38. # note that frame is the parent for the two panels
  39. frame.panel1 = MyPanel1(frame)
  40. frame.panel2 = MyPanel2(frame)
  41.  
  42. frame.Show(True)
  43. app.MainLoop()
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,075
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 939
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: OOP Concept

 
1
  #3
Oct 11th, 2008
Ene, you are using global instances, so the frame.panel1 or frame.panel2 reference is actually not needed. You can just use panel1 and panel2. However, your approach keeps things together.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,401
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: OOP Concept

 
0
  #4
Oct 12th, 2008
hello Ene, Vega and All Daniweb,
Please assist me on how calling is done on this line:
  1. frame.panel2.label.SetLabel(s)
Does it change the label of
  1. self.label = wx.StaticText(self, wx.ID_ANY, " panel2 label ")
????

What are you really doing with this line of codes? Are you appending them to table??
I ask because I have never used before this such way of coding in py
[
  1. frame.panel1 = MyPanel1(frame)
  2. frame.panel2 = MyPanel2(frame)

Thanks for your answers
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,075
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 939
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: OOP Concept

 
0
  #5
Oct 12th, 2008
Yes, in Ene's code she references
self.label = wx.StaticText(self, wx.ID_ANY, " panel2 label ")
created in panel2 with
frame.panel2.label.SetLabel(s)
used in panel1

The trick here is to create the instances for both panels in __main__ to make them globally available.

If you don't want to use global instances, then you can let the parent carry the connection ...
  1. # reference across class instances
  2. # from one panel class to another panel class
  3. # via common parent
  4.  
  5. import wx
  6. import time
  7.  
  8. class MyFrame(wx.Frame):
  9. def __init__(self, parent, mytitle, mysize):
  10. wx.Frame.__init__(self, parent, wx.ID_ANY, mytitle, size=mysize)
  11.  
  12. # here panel1 and panel2 are local
  13. # but can be accessed via the parent
  14. self.panel1 = MyPanel1(parent=self)
  15. self.panel2 = MyPanel2(parent=self)
  16.  
  17.  
  18. class MyPanel1(wx.Panel):
  19. def __init__(self, parent):
  20. wx.Panel.__init__(self, parent, wx.ID_ANY,
  21. pos=(0,0), size=(300, 100))
  22. # parent is common to both panels and
  23. # in this case cross references the panels
  24. self.parent = parent
  25. self.SetBackgroundColour("green")
  26. self.button = wx.Button(self, label=" panel1 button ")
  27. self.button.Bind(wx.EVT_BUTTON, self.buttonClick )
  28.  
  29. def buttonClick(self, event):
  30. """
  31. use the panel2 label and
  32. reference via the parent --> frame
  33. """
  34. # display something that indicates a change
  35. s = time.strftime("%H:%M:%S", time.localtime())
  36. # parent, or in this case self.parent, allows you to
  37. # get to widgets created in panel2
  38. self.parent.panel2.label.SetLabel(s)
  39.  
  40.  
  41. class MyPanel2(wx.Panel):
  42. def __init__(self, parent):
  43. wx.Panel.__init__(self, parent, wx.ID_ANY,
  44. pos=(0, 100), size=(300, 100))
  45. self.SetBackgroundColour("yellow")
  46. self.label = wx.StaticText(self, wx.ID_ANY, " panel2 label ")
  47.  
  48.  
  49. app = wx.App(0)
  50. # create a frame parent=None, title, size
  51. mytitle = "cross reference via common parent"
  52. MyFrame(None, mytitle, (300, 220)).Show(True)
  53. app.MainLoop()
This is IMHO a little cleaner and safer. No offence Ene!
Last edited by vegaseat; Oct 12th, 2008 at 11:11 am. Reason: common parent
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,401
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: OOP Concept

 
0
  #6
Oct 12th, 2008
code is self explanatory. However I have to be sure that I grasped the concept and then go for practice:

  1. self.parent.panel2.label.SetLabel(s)
suppose I have two dialog boxes and both have parent called frm and dialogs dia1 and dia2
dia1 a button, and dia2 a button. The dialog1 appears automatically after some time (that is easy to do) and when you press the button it calls dialog2 and get destroyed. I need its theory and even veeery simple example and the rest is my exercise!

Sorry for bothering and thanks for response!
Steve
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,284
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 177
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: OOP Concept

 
0
  #7
Oct 13th, 2008
At this point you better give us some code, or we all get lost.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,401
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: OOP Concept

 
0
  #8
Oct 14th, 2008
  1. #call dialog, type new caption, set it to static text and destroy the dialog
  2. import wx
  3. class Main(wx.Frame):
  4. def __init__ (self, parent, id, title):
  5. wx.Frame.__init__(self, parent, id, title)
  6. panel = wx.Panel(self, -1)
  7. #Make button
  8. bu = wx.Button(panel, -1, "Call")
  9. self.Bind(wx.EVT_BUTTON, self.OnCall, id=bu.GetId())
  10. #create static text ctrl
  11. title = wx.StaticText(panel,-1, "Original Text", style=wx.ALIGN_LEFT)
  12. #Add to sizer
  13. sizer = wx.BoxSizer(wx.VERTICAL)
  14. sizer.Add(title, 1, wx.EXPAND |wx.ALL, 10)
  15. sizer.Add(bu, 0, wx.ALL, 10)
  16. panel.SetSizer(sizer)
  17. panel.Layout()
  18.  
  19. def OnCall(self, event):
  20. dialog = Dialog(self, -1, "Change caption")
  21. dialog.Show(True)
  22. Main.Hide()
  23.  
  24. #Dialog box as another class
  25. class Dialog(wx.Dialog):
  26. def __init__(self, parent, id, title):
  27. wx.Dialog.__init__(self, parent, id, title)
  28. self.text = wx.TextCtrl(self, -1)
  29. ok = wx.Button(self, -1, "Ok")
  30. #Bind to event tochange caption of Title=text
  31. self.Bind(self, wx.EVT_BUTTON, self.OnChangeCaption, id = ok.GetId())
  32. sizer2 = wx.BoxSizer()
  33. sizer2.Add(self.text, 1, wx.EXPAND |wx.ALL, 10)
  34. sizer2.Add(ok, 0, wx.ALL, 10)
  35. self.SetSizer(sizer2)
  36. self.Layout()
  37.  
  38. def OnChangeCaption(self, event):
  39. newlabel = self.text.GetValue()
  40. self.title.SetLabel(newlabel)
  41.  
  42. app = wx.App(False)
  43. f = Main(None, -1, "Call Children from parent")
  44. f.Show(True)
  45. app.MainLoop()
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,075
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 939
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: OOP Concept

 
0
  #9
Oct 15th, 2008
So what is your error?
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,401
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 128
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: OOP Concept

 
0
  #10
Oct 15th, 2008
when i click call button I get:

Traceback (most recent call last):
File "C:\Users\Elijah Ministries\Documents\NetBeansProjects\Learning Python\src\database - Griboullis.py", line 20, in OnCall
dialog = Dialog(self, -1, "Change caption")
File "C:\Users\Elijah Ministries\Documents\NetBeansProjects\Learning Python\src\database - Griboullis.py", line 31, in __init__
self.Bind(self, wx.EVT_BUTTON, self.OnChangeCaption, id = ok.GetId())
File "C:\Python25\Lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 3885, in Bind
id = source.GetId()
AttributeError: 'function' object has no attribute 'GetId'
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC