954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Call function from a different module

Hi,

I have two modules: Mod1 and Mod2. I want to call the function ‘ ProgressBar’ which is present in Mod1 inside a class ‘clsMain’. In the function ‘ProgressBar’, I set the increment to the progress bar. The function looks something like this:

def ProgressBar(self,intIncrement):
self.gauge1.SetValue(intIncrement)

It takes integer as an input. Self refers to the frame on which it is set.
How would I call this function from Mod2???
Any help is appreciated!!!!

Regards,
Rajendra

dinilkarun
Posting Whiz in Training
206 posts since Feb 2008
Reputation Points: 18
Solved Threads: 0
 

couldnt you just import mod1 into your mod2? That way you could then access all methods.

Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
 

I tried that... I have imported mod1 into mod2. But its giving the following error: module object has no attribute 'ProgressBar'.
When I call that function within the same module, its working fine.

Regards,
Dinil

dinilkarun
Posting Whiz in Training
206 posts since Feb 2008
Reputation Points: 18
Solved Threads: 0
 

Well is ProgressBar inside a class in the module cause then you might have to go:

import mod1
mod1.class.ProgressBar()
Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
 

yes the function 'ProgressBar' is inside a class 'clsMain’.
I tried mod1.clsMain.ProgressBar()

its still showing the same error.

Regards,
Dinil

dinilkarun
Posting Whiz in Training
206 posts since Feb 2008
Reputation Points: 18
Solved Threads: 0
 

why don't you just post Mod1 and Mod2, or attach them to your post ?

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Hi,

I have two modules: Mod1 and Mod2. I want to call the function ‘ ProgressBar’ which is present in Mod1 inside a class ‘clsMain’. In the function ‘ProgressBar’, I set the increment to the progress bar. The function looks something like this:

def ProgressBar(self,intIncrement):
self.gauge1.SetValue(intIncrement)

It takes integer as an input. Self refers to the frame on which it is set.
How would I call this function from Mod2???

I have tried this, but its not working!
This function is present in Mod2:

def GetUserInfo():
   
    #Call Function ProgressBar present in Mod1
    progress_bar = Mod1.clsMain(parent)
    progress_bar.ProgressBar(20)


The structure of Mod1 is as follows:

class clsMain(wx.Frame):
    def ProgressBar(self,intIncreament):
         self.gauge1.SetValue(intIncreament)

Any help is appreciated!!!!

Regards,
Dinil

dinilkarun
Posting Whiz in Training
206 posts since Feb 2008
Reputation Points: 18
Solved Threads: 0
 

I have attcahed the sample code:
Instead of Mod1 and Mod2, I have renamed it to as frame1 and module2.

Attachments Test_Code.zip (4.39KB)
dinilkarun
Posting Whiz in Training
206 posts since Feb 2008
Reputation Points: 18
Solved Threads: 0
 

In principle, it should work if you put import Mod1 in Mod2. However this will fail if Mod1 imports Mod2, because there should be no cyclic importations in python code. There are ways around this. A solution could be to put the import statement in the function GetUserInfo

def GetUserInfo():
    import Mod1
    #Call Function ProgressBar present in Mod1
    progress_bar = Mod1.clsMain(parent)
    progress_bar.ProgressBar(20)

so that the import Mod1 will only be executed when GetUserInfo is called and not when Mod2 is imported.

I see in your code that frame1.py imports module2 and that module2.py imports frame1, and that's a bad idea :)

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

Hi,

Its still not working...
Can u please look into the sample code?

Regards,
Dinil

dinilkarun
Posting Whiz in Training
206 posts since Feb 2008
Reputation Points: 18
Solved Threads: 0
 

There is an inconsistency in your code. The __init__ function in class Frame1 (which is called when you create a new Frame1 object) calls GetUserInfo which in turn creates a new Frame1 by a call to Frame1.Frame1. This gives an infinite loop.

Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691
 

So, How should I call the function 'ProgressBar' only once?

dinilkarun
Posting Whiz in Training
206 posts since Feb 2008
Reputation Points: 18
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You