Hi,

I'm new to both python and Wxpython so I was wondering if someone could help me with a problem I have.

I made a button that runs the UpdateNebula function. the function takes a string called path but I haven't been able to find a way to have the function accept the string.

Does anyone know how this is done? This is my code:

Update_Nebula = wx.Button(self.panel, 1305, "Update_Nebula")
        self.Bind(wx.EVT_BUTTON, self.UpdateNebula(), self.Update_Nebula)

and the function

def UpdateNebula(self,event,path):
        print path

Thanks :)

Use class attributes. When you get the path from the user do something like this:

self.path = path

Then your updateNebula method can be written like this:

def UpdateNebula(self, event=None):
    if 'path' in dir( self ) and self.path:
        print self.path
self.Bind(wx.EVT_BUTTON, self.UpdateNebula(), self.Update_Nebula)

This will not work, you are actually calling the fucntion self.UpdateNebula when you put the parentheses there, so if you get rid of them you will be fine :)

Another way i do it a lot is just to bind them to the objects themselves:

self.Update_Nebula.Bind(wx.EVT_LEFT_DOWN, self.UpdateNebula)
#this will bind it to the same as if you did the previous code, i just find it has less issues

But its really up to your own taste what you want to do.

Here is a typical wxPython template you can start with ...

# a wxPython general frame and button template
# click the button and show a string in a label

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent, mytitle, mysize):
        wx.Frame.__init__(self, parent, -1, mytitle, size=mysize)
        self.SetBackgroundColour("yellow")
        # self allows path to be passed to buttonClick function
        self.path = "path test"

        # create input/action widgets
        self.button = wx.Button(self, -1, label='Click me')
        # bind mouse event to an action
        self.button.Bind(wx.EVT_BUTTON, self.buttonClick)
        
        # create an output widget
        self.label = wx.StaticText(self, -1, "-------")
       
        # layout the widgets in a vertical order
        sizer_v = wx.BoxSizer(wx.VERTICAL)
        # Add(widget, proportion, flag, border)
        sizer_v.Add(self.button, 0, flag=wx.ALL, border=10)
        sizer_v.Add(self.label, 0, flag=wx.ALL, border=10)
        self.SetSizer(sizer_v)        

    def buttonClick(self, event):
        """button has been clicked, do something"""
        self.label.SetLabel(self.path)


app = wx.App(0)
# create a MyFrame instance and show the frame
mytitle = 'basic button action'
width = 300
height = 150
MyFrame(None, mytitle, (width, height)).Show()
app.MainLoop()
This question has already been answered. Start a new discussion instead.