Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
problems getting os.system and wxmenu to read options from a file and then execute
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Eric_Dexter@msn.com  
View profile  
 More options Jun 27 2010, 7:11 pm
Newsgroups: comp.lang.python
From: "Eric_Dex...@msn.com" <Eric_Dex...@msn.com>
Date: Sun, 27 Jun 2010 16:11:04 -0700 (PDT)
Local: Sun, Jun 27 2010 7:11 pm
Subject: problems getting os.system and wxmenu to read options from a file and then execute
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[i],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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Jun 27 2010, 7:56 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Mon, 28 Jun 2010 00:56:04 +0100
Local: Sun, Jun 27 2010 7:56 pm
Subject: Re: problems getting os.system and wxmenu to read options from a file and then execute
Eric_Dex...@msn.com 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]

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
eric dexter  
View profile  
 More options Jun 27 2010, 9:43 pm
Newsgroups: comp.lang.python
From: eric dexter <irc.dex...@gmail.com>
Date: Sun, 27 Jun 2010 18:43:48 -0700 (PDT)
Local: Sun, Jun 27 2010 9:43 pm
Subject: Re: problems getting os.system and wxmenu to read options from a file and then execute
On Jun 27, 5:56 pm, MRAB <pyt...@mrabarnett.plus.com> wrote:

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
MRAB  
View profile  
 More options Jun 27 2010, 10:18 pm
Newsgroups: comp.lang.python
From: MRAB <pyt...@mrabarnett.plus.com>
Date: Mon, 28 Jun 2010 03:18:54 +0100
Local: Sun, Jun 27 2010 10:18 pm
Subject: Re: problems getting os.system and wxmenu to read options from a file and then execute

 >
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
eric dexter  
View profile  
 More options Jun 27 2010, 11:45 pm
Newsgroups: comp.lang.python
From: eric dexter <irc.dex...@gmail.com>
Date: Sun, 27 Jun 2010 20:45:10 -0700 (PDT)
Local: Sun, Jun 27 2010 11:45 pm
Subject: Re: problems getting os.system and wxmenu to read options from a file and then execute
On Jun 27, 8:18 pm, MRAB <pyt...@mrabarnett.plus.com> wrote:

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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »