problems getting os.system and wxmenu to read options from a file andthen execute

E

Eric_Dexter

I managed to get the program running and the menu options are
appearing on the list but the programs are not running. I suspect it
is my onexecutemethod

# Get the GUI stuff
import wx

# We're going to be handling files and directories
import os
menufile = open('menufile.txt','r')

# Set up some button numbers for the menu

ID_ABOUT=101
ID_OPEN=102
ID_SAVE=103
ID_BUTTON1=300
ID_EXIT=200
#ID_TOOL = 400

class MainWindow(wx.Frame):
def __init__(self,parent,title):
# based on a frame, so set up the frame
wx.Frame.__init__(self,parent,wx.ID_ANY, title)

# Add a text editor and a status bar
# Each of these is within the current instance
# so that we can refer to them later.
self.control = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE)
self.CreateStatusBar() # A Statusbar in the bottom of the
window

# Setting up the menu. filemenu is a local variable at this
stage.
filemenu= wx.Menu()
# use ID_ for future easy reference - much better that "48",
"404" etc
# The & character indicates the short cut key
filemenu.Append(ID_OPEN, "&Open"," Open a file to edit")
filemenu.AppendSeparator()
filemenu.Append(ID_SAVE, "&Save"," Save file")
filemenu.AppendSeparator()
filemenu.Append(ID_ABOUT, "&About"," Information about this
program")
filemenu.AppendSeparator()
filemenu.Append(ID_EXIT,"E&xit"," Terminate the program")
#add options from a text file
idtool = 400
menuoptions = []
for line in menufile:
t = line.split(' ')
filemenu.Append(idtool, t[0], t[1])
menuoptions.append(t[1])
idtool += 1

# Creating the menubar.
menuBar = wx.MenuBar()
menuBar.Append(filemenu,"&File") # Adding the "filemenu" to
the MenuBar
self.SetMenuBar(menuBar) # Adding the MenuBar to the Frame
content.
# Note - previous line stores the whole of the menu into the
current object

# Define the code to be run when a menu option is selected
wx.EVT_MENU(self, ID_ABOUT, self.OnAbout)
wx.EVT_MENU(self, ID_EXIT, self.OnExit)
wx.EVT_MENU(self, ID_OPEN, self.OnOpen)
wx.EVT_MENU(self, ID_SAVE, self.OnSave) # just "pass" in our
demo
#add execute files from the text file list
idtool = 400
for e in menuoptions:
wx.EVT_MENU(self, idtool, self.OnExecute(idtool, e))
idtool += 1
##print e

# Set up a series of buttons arranged horizontally
self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
self.buttons=[]
# Note - give the buttons numbers 1 to 6, generating events
301 to 306
# because IB_BUTTON1 is 300
for i in range(6):
# describe a button
bid = i+1
self.buttons.append(wx.Button(self, ID_BUTTON1+i, "Button
&"+str(bid)))
# add that button to the sizer2 geometry
self.sizer2.Add(self.buttons,1,wx.EXPAND)

# Set up the overall frame verically - text edit window above
buttons
# We want to arrange the buttons vertically below the text
edit window
self.sizer=wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.control,1,wx.EXPAND)
self.sizer.Add(self.sizer2,0,wx.EXPAND)

# Tell it which sizer is to be used for main frame
# It may lay out automatically and be altered to fit window
self.SetSizer(self.sizer)
self.SetAutoLayout(1)
self.sizer.Fit(self)

# Show it !!!
self.Show(1)

# Define widgets early even if they're not going to be seen
# so that they can come up FAST when someone clicks for them!
self.aboutme = wx.MessageDialog( self, " A sample editor \n"
" in wxPython","About Sample Editor",
wx.OK)
self.doiexit = wx.MessageDialog( self, " Exit - R U Sure? \n",
"GOING away ...", wx.YES_NO)

# dirname is an APPLICATION variable that we're choosing to
store
# in with the frame - it's the parent directory for any file
we
# choose to edit in this frame
self.dirname = ''

def OnAbout(self,e):
# A modal show will lock out the other windows until it has
# been dealt with. Very useful in some programming tasks to
# ensure that things happen in an order that the programmer
# expects, but can be very frustrating to the user if it is
# used to excess!
self.aboutme.ShowModal() # Shows it
# widget / frame defined earlier so it can come up fast when
needed

def OnExit(self,e):
# A modal with an "are you sure" check - we don't want to exit
# unless the user confirms the selection in this case ;-)
igot = self.doiexit.ShowModal() # Shows it
if igot == wx.ID_YES:
self.Close(True) # Closes out this simple application

def OnOpen(self,e):
# In this case, the dialog is created within the method
because
# the directory name, etc, may be changed during the running
of the
# application. In theory, you could create one earlier, store
it in
# your frame object and change it when it was called to
reflect
# current parameters / values
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "",
"*.*", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.filename=dlg.GetFilename()
self.dirname=dlg.GetDirectory()

# Open the file, read the contents and set them into
# the text edit window
filehandle=open(os.path.join(self.dirname,
self.filename),'r')
self.control.SetValue(filehandle.read())
filehandle.close()

# Report on name of latest file read
self.SetTitle("Editing ... "+self.filename)
# Later - could be enhanced to include a "changed" flag
whenever
# the text is actually changed, could also be altered on
"save" ...
dlg.Destroy()

def OnSave(self,e):
# Save away the edited text
# Open the file, do an RU sure check for an overwrite!
dlg = wx.FileDialog(self, "Choose a file", self.dirname, "",
"*.*", \
wx.SAVE | wx.OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
# Grab the content to be saved
itcontains = self.control.GetValue()

# Open the file for write, write, close
self.filename=dlg.GetFilename()
self.dirname=dlg.GetDirectory()
filehandle=open(os.path.join(self.dirname,
self.filename),'w')
filehandle.write(itcontains)
filehandle.close()
dlg.Destroy()
def OnExecute(self,tool,e):
print tool
os.system(e)
#print tool
# Get rid of the dialog to keep things tidy


# Set up a window based app, and create a main window in it
app = wx.PySimpleApp()
view = MainWindow(None, "Sample editor")
# Enter event loop
app.MainLoop()

my text file is

option1 test1.py
option2 test1.py
dir dir
adom adom.exe

I am currently using python 2.5, I suspect it is something simple any
help would be nice.
 
M

MRAB

I managed to get the program running and the menu options are
appearing on the list but the programs are not running. I suspect it
is my onexecutemethod
[snip]

#add execute files from the text file list
idtool = 400
for e in menuoptions:
wx.EVT_MENU(self, idtool, self.OnExecute(idtool, e))

This calls the OnExecute method and then passes its result to
wx.EVT_MENU. If, for example, OnExecute is returning None, then that's
what is being passed into wx.EVT_MENU.

What you should actually be doing is passing a function which can be
called when the menu item is clicked, something like this:

wx.EVT_MENU(self, idtool, lambda idtool=idtool, e=e:
self.OnExecute(idtool, e))
idtool += 1
##print e
[snip]
def OnExecute(self,tool,e):
print tool
os.system(e)
#print tool
# Get rid of the dialog to keep things tidy
[snip]
 
E

eric dexter

I managed to get the program running and the menu options are
appearing on the list but the programs are not running.  I suspect it
is my onexecutemethod
[snip]

        #add execute files from the text file list
        idtool = 400
        for e in menuoptions:
            wx.EVT_MENU(self, idtool, self.OnExecute(idtool, e))

This calls the OnExecute method and then passes its result to
wx.EVT_MENU. If, for example, OnExecute is returning None, then that's
what is being passed into wx.EVT_MENU.

What you should actually be doing is passing a function which can be
called when the menu item is clicked, something like this:

             wx.EVT_MENU(self, idtool, lambda idtool=idtool, e=e:
self.OnExecute(idtool, e))
            idtool += 1
            ##print e
[snip]
    def OnExecute(self,tool,e):
        print tool
        os.system(e)
        #print tool
        # Get rid of the dialog to keep things tidy

[snip]

wx.EVT_MENU(self, idtool, self.OnExecute(idtool, e))

I changed it to this and it seems to be calling self.OnExecute befour
the editor comes up and then after the editor is up it doesn't respond
to anything. I got rid off all the spaces but one in the text file (I
supose I need to use the strip method in real like though).. I was
able to get an infinite loop by calling the editor as one of the
options and then it keeps calling itself. It may be another problem
or perhaps I didn't grasp the answer.
 
M

MRAB

eric said:
I managed to get the program running and the menu options are
appearing on the list but the programs are not running. I suspect it
is my onexecutemethod [snip]

#add execute files from the text file list
idtool = 400
for e in menuoptions:
wx.EVT_MENU(self, idtool, self.OnExecute(idtool, e))
This calls the OnExecute method and then passes its result to
wx.EVT_MENU. If, for example, OnExecute is returning None, then that's
what is being passed into wx.EVT_MENU.

What you should actually be doing is passing a function which can be
called when the menu item is clicked, something like this:

wx.EVT_MENU(self, idtool, lambda idtool=idtool, e=e:
self.OnExecute(idtool, e))
idtool += 1
##print e [snip]
def OnExecute(self,tool,e):
print tool
os.system(e)
#print tool
# Get rid of the dialog to keep things tidy
[snip]

wx.EVT_MENU(self, idtool, self.OnExecute(idtool, e))

I changed it to this and it seems to be calling self.OnExecute befour
the editor comes up and then after the editor is up it doesn't respond
to anything.
>
That is what the line was in your original post, and I explained the
change you needed to do.
> I got rid off all the spaces but one in the text file (I
supose I need to use the strip method in real like though).. I was
able to get an infinite loop by calling the editor as one of the
options and then it keeps calling itself. It may be another problem
or perhaps I didn't grasp the answer.
>
I've noticed that you're opening the menu file before the class
statement but reading from it in the __init__ method. It would be better
if you opened the file in the __init__ method itself. You can split a
line on whitespace with:

t = line.split()

It won't then matter how many spaces or tabs are between the fields.
 
E

eric dexter

eric said:
(e-mail address removed) wrote:
I managed to get the program running and the menu options are
appearing on the list but the programs are not running.  I suspect it
is my onexecutemethod
[snip]
        #add execute files from the text file list
        idtool = 400
        for e in menuoptions:
            wx.EVT_MENU(self, idtool, self.OnExecute(idtool, e))
This calls the OnExecute method and then passes its result to
wx.EVT_MENU. If, for example, OnExecute is returning None, then that's
what is being passed into wx.EVT_MENU.
What you should actually be doing is passing a function which can be
called when the menu item is clicked, something like this:
             wx.EVT_MENU(self, idtool, lambda idtool=idtool, e=e:
self.OnExecute(idtool, e))
            idtool += 1
            ##print e
[snip]
    def OnExecute(self,tool,e):
        print tool
        os.system(e)
        #print tool
        # Get rid of the dialog to keep things tidy
[snip]
wx.EVT_MENU(self, idtool, self.OnExecute(idtool, e))
I changed it to this and it seems to be calling self.OnExecute befour
the editor comes up and then after the editor is up it doesn't respond
to anything.

 >
That is what the line was in your original post, and I explained the
change you needed to do.

 > I got rid off all the spaces but one in the text file (I> supose I need to use the strip method in real like though)..  I was
able to get an infinite loop by calling the editor as one of the
options and then it keeps calling itself.  It may be another problem
or perhaps I didn't grasp the answer.

 >
I've noticed that you're opening the menu file before the class
statement but reading from it in the __init__ method. It would be better
if you opened the file in the __init__ method itself. You can split a
line on whitespace with:

     t = line.split()

It won't then matter how many spaces or tabs are between the fields.

I think it may and that is why there is a strip method. all 4 files
want to execute befour the editor comes up so there is a problem of
some sort that isn't adressed by that but it probily would be nice if
I coded it that way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top