?
Solved

call module from  different class

Posted on 2011-09-15
9
Medium Priority
?
388 Views
Last Modified: 2012-05-12
I want to call the populate module from the threading class ... How can I do that ? right now I have insertdata.populate and its not throwing and error but it's also not doing what I want




class InsertData(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(990,668),style=wx.DEFAULT_FRAME_STYLE|wx.WS_EX_CONTEXTHELP)
	self.Bind(wx.EVT_CLOSE, self.OnClose)
def populate(self):
        do something




class ThreadClass(threading.Thread,InsertData):
    def run(self):
          do_something
          once_done
       
	    self.repopulate()
    def repopulate(self):	           
	InsertData.populate
	print "done"
ThreadClass().start() 

Open in new window

0
Comment
Question by:dolamitejenkins
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 17

Expert Comment

by:gelonida
ID: 36547365
Very first issue. is line 18,

which should be

InsertData.populate()

If this doesn't solve the issue I'd go for an approach without multiple inheritance.

create the InsertData intance within your thread and store it for example under self.insert_data.
If you have to pass args during construction time, then use the args / kwargs of the threading object.

http://docs.python.org/library/threading.html#thread-objects



Not knowing wxWidgets and not knowing, what you try to do in populate, and whether you also try to access the widgets from other threads I'd just like to mention, that
many graphical frameworks are rather sensitive if being combined with threading, thus they have very specific means (PyQT has for example signals), which you should use to avoid breaking the framework.

Often you are not allowed to add modify GUI elements from another thread tan the originating GUI thread.



0
 

Author Comment

by:dolamitejenkins
ID: 36547390
thanks ... I will let you know how it goes... i have to do some research on wxpython ... should I build my future programs with ptqt ?
0
 
LVL 17

Expert Comment

by:gelonida
ID: 36547490
WxWidgets and PyQt are rather popular and both good options.

I think both require certain measures to work well in a multithreaded setup.

Normally only one thread is allowed to create / destroy manipulate widgets and run the main loop.
other threads had to communicate via signals/slots (Qt) or pipes (Wx)

SW licensing might be one reason to pay attention with PyQt.
- http://www.riverbankcomputing.co.uk/software/pyqt/license
- http://www.wxwidgets.org/about/newlicen.htm

You might also look at PySide  (rather recent LGPL alternative to PyQt)
- http://en.wikipedia.org/wiki/PySide


0
New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

 

Author Comment

by:dolamitejenkins
ID: 36551539
thanks
0
 
LVL 29

Accepted Solution

by:
pepr earned 1000 total points
ID: 36551574
The InsertData.populate() will not work either.  It's because the populate() expects the self as argument.  The InsertData is the class.  However, the method expect the reference to the object -- i.e. to the instance of the InsertData class.  The object of the class must be created first.

The InsertData class is part of GUI with some functionality. It should be given a noun name -- say DataFrame.

You should probably better describe your intention and the related design decisions with plain words.

To add to gelonida's comment.  The InsertData is the class, and the populate is one of its attributes -- the name of its method.  Try the following simpler example in the interactive mode:

Python 2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class Cls:
...   def method(self):
...     print 'the method called'
...
>>> Cls
<class __main__.Cls at 0x00000000022577C8>
>>> Cls.method
<unbound method Cls.method>
>>> obj = Cls()
>>> obj
<__main__.Cls instance at 0x000000000226EF48>
>>> obj.method
<bound method Cls.method of <__main__.Cls instance at 0x000000000226EF48>>
>>> obj.method()
the method called
>>>

Open in new window


The Cls.method is the same case as your InsertData.populate.  You can call also the unbound method of the class, but then you have to pass the reference to the object as the first argument -- it will be assigned to the internal self.

>>> Cls.method()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unbound method method() must be called with Cls instance as first arg
ument (got nothing instead)
>>> Cls.method(obj)
the method called
>>>

Open in new window

0
 
LVL 17

Assisted Solution

by:gelonida
gelonida earned 1000 total points
ID: 36552449
Pepr You're absolutely  right.

I was answering too quickly.

Should have been
 self.populate()
or InsertData.populate(self)

Knowing, that there's no populate method in threading.Thread, I'd probably go for
 self.populate()




0
 

Author Comment

by:dolamitejenkins
ID: 36552478
ok I will try it ... thanks to you both for your guidance
0
 
LVL 29

Expert Comment

by:pepr
ID: 36553561
It seems to me that you did not want to make your InsertData the base class of your thread class.  You probably wanted to pass the InsertData (bad name) instance (i.e. object of the class) to the thread object (say via __init__ method or somehow else).

You should think more about whether you really need a thread.  It is better to avoid them if threads are not neccessary.

Try to imagine the object personified or visualize it in some other way when thinking about how it all should work. Make the childish drawings (i.e. circles, squares, arrows... all informal).  They are powerful means to help our imagination.
0
 

Author Closing Comment

by:dolamitejenkins
ID: 36578405
thanks
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question