problem binding function to event

Hi I'm new to both python and wxpython (and programming/scripting in
general) so please take it easy on me I am still trying to gain
knowledge!

I am writing a simple script that once a button is pressed a cmd is
sent to run a process and output a file. I cant get it to work: my
code and error is below - any help would be much appreciated

import wx
import subprocess

class window(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Yahoo', size=(300,200))
        panel=wx.Panel(self)

        pic=wx.Image("Penguins.bmp",
wx.BITMAP_TYPE_BMP).ConvertToBitmap()
        self.button=wx.BitmapButton(panel, -1, pic, pos=(10,10))
        self.Bind(wx.EVT_BUTTON, self.doMe, self.button)
        self.button.SetDefault()

    def doMe(self, event):
        print 'starting'
        p=subprocess.Popen(cmd,stdout=subprocess.PIPE, shell=True)
        return p.communicate()[0].splitlines()
        YahooTest = run("YPD.exe Yahoo.html")
        print 'complete'

if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=window(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

starting
Traceback (most recent call last):
  File "N:\test\Button Run.py", line 17, in doMe
    p=subprocess.Popen(cmd,stdout=subprocess.PIPE, shell=True)
NameError: global name 'cmd' is not defined

Hi I’m new to both python and wxpython (and programming/scripting in

general) so please take it easy on me I am still trying to gain

knowledge!

I am writing a simple script that once a button is pressed a cmd is

sent to run a process and output a file. I cant get it to work: my

code and error is below - any help would be much appreciated

def doMe(self, event):

    print 'starting'

    p=subprocess.Popen(cmd,stdout=subprocess.PIPE, shell=True)

    return p.communicate()[0].splitlines()

    YahooTest = run("YPD.exe Yahoo.html")

    print 'complete'

starting

Traceback (most recent call last):

File “N:\test\Button Run.py”, line 17, in doMe

p=subprocess.Popen(cmd,stdout=subprocess.PIPE, shell=True)

NameError: global name ‘cmd’ is not defined

This is your traceback here. It tells you where the error occurred, and (in more complicated examples) would track the code all the way back up so you’d see who called the function that crashed, who called the function that called that function, etc. Here it’s saying that there’s an error in your line with Popen, because it doesn’t know what “cmd” is. And indeed, in your function you refer to “cmd” without first defining it.

Incidentally, when binding events you can do this instead:

button.Bind(wx.EVT_BUTTON, self.onButton)

HTH. HAND.

-Chris

···

On Wed, Jun 8, 2011 at 10:43 AM, Steve5000 mobile@itforensics.org.uk wrote:

Steve5000 wrote:

Hi I'm new to both python and wxpython (and programming/scripting in
general) so please take it easy on me I am still trying to gain
knowledge!

I am writing a simple script that once a button is pressed a cmd is
sent to run a process and output a file. I cant get it to work: my
code and error is below - any help would be much appreciated
...
    def doMe(self, event):
        print 'starting'
        p=subprocess.Popen(cmd,stdout=subprocess.PIPE, shell=True)
        return p.communicate()[0].splitlines()
        YahooTest = run("YPD.exe Yahoo.html")
        print 'complete'

You must have collected this code together from a couple of different
places. You are trying to pass a command string "cmd" without defining
it, and you are calling a function called "run" that does not exist.
Plus, you are returning from the function before you are finished.

Try this instead:
    def doMe(self, event):
        print 'starting'
        p = subprocess.Popen("YPD.exe Yahoo.html",
stdout=subprocess.PIPE, shell=True)
        out = p.comminucate()[0].readlines()
        # What do you want to do with the output here?
        print out
        print 'complete'

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Many thanks for your response Tim it is very much appreciated I am new
to this so still trying to get my head around the basics.

Yes I am slowly trying to build up a repository of re-usable code from
different scripts I write and this came from two scripts one for
creating the window and button and the other which was just to send
out a command.

All I was trying to do was execute the YPD program and then output its
results to a html report.

This is fixed now.

···

On Jun 8, 9:34 pm, Tim Roberts <t...@probo.com> wrote:

Steve5000 wrote:
> Hi I'm new to both python and wxpython (and programming/scripting in
> general) so please take it easy on me I am still trying to gain
> knowledge!

> I am writing a simple script that once abuttonis pressed a cmd is
> sent to run a process and output a file. I cant get it to work: my
> code and error is below - any help would be much appreciated
> ...
> def doMe(self, event):
> print 'starting'
> p=subprocess.Popen(cmd,stdout=subprocess.PIPE, shell=True)
> return p.communicate()[0].splitlines()
> YahooTest = run("YPD.exe Yahoo.html")
> print 'complete'

You must have collected this code together from a couple of different
places. You are trying to pass a command string "cmd" without defining
it, and you are calling a function called "run" that does not exist.
Plus, you are returning from the function before you are finished.

Try this instead:
def doMe(self, event):
print 'starting'
p = subprocess.Popen("YPD.exe Yahoo.html",
stdout=subprocess.PIPE, shell=True)
out = p.comminucate()[0].readlines()
# What do you want to do with the output here?
print out
print 'complete'

--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.