[wxPython-users] calling a class inside a function

Mark Erbaugh mark at microenh.com
Tue Feb 5 22:05:18 PST 2008


On Tue, 2008-02-05 at 15:45 -0800, Varsha Purohit wrote:
> Hello everyone,       
>            I have a  scenario in which i am having a button which will
> be binded to an event handler. And that event handler should call a
> function which is defined in another file. That function should
> contain a class for scrollbar. And i want to call this function from
> the button handler function. So i want to know is it okay to call a
> class within a function. I am attachin sample code.
> 
> file1.py
> 
> import wx 
> 
> def scrollimage(imageFile):
>     
>     im = Image.open(imageFile)
>     im2= self.im.resize((700,550),Image.BICUBIC)
>     FormFiche(im2) #is it okay to pass the image as parameter to the
> class or am i messing up something here ??
>     
>     class FormFiche(wx.Frame):
>          def __init__(self,name):
> 
> 
> 
> wx.Frame.__init__(self,None,-1,"Title",size=wx.Size(500,400))       
>          
>              scroll=wx.ScrolledWindow(self,-1)
> 
>              self.bmp =
> wx.Image('DSCN3378.jpg',wx.BITMAP_TYPE_JPEG).ConvertToBitmap()
>              wx.StaticBitmap(scroll, -1, self.bmp)
>              self.Show(1)
>              scroll.SetScrollbars(20, 20, 55, 40)
>                
> 
>     myapp=wx.PySimpleApp()
>     FormFiche()
>     myapp.MainLoop()
> 
> 
> ----------------------------------------------------------------------------------------------
> 
> i want to call this function and give image file as a parameter to
> this funciton which would show a frame with scrollbars. But i am not
> able to do that i am getting error. I have included file1.py in this
> main.py file.
> 
> -- 
> Varsha Purohit,
> Graduate Student
> def button1click(self, event):
>         scrollimage(self.bmp)
> 
> 

Varsha,

I'm not sure what error you are getting, but assuming your main.py is a
wxPython Frame (because it has an Button event handler), there is
already a main wxPython application object and that is already running
its MainLoop.  The end of the scrollimage function tries to create
another wxPython application object (myapp) and run its MainLoop().  You
can only have one wxPython application object and one MainLoop.  Perhaps
your code would work if you eliminated the lines

myapp=wx.PySimpleApp()

and myapp.MainLoop()

at the end of scrollimage

Also, you are trying to initialize FormFiche with no parameters, but the
__init__ method needs one parameter - name.

Mark





More information about the wxpython-users mailing list