Returning value from home made unit - how to?

M

Martin Hvidberg

Dear <[email protected]>

A may-bee beginner’s question

I have a Python program, which has until now, been running in command line mode only. I wish to add a GUI.

I would like to develop (and maintain) the GUI part in a separate module, i.e. in its own .py file, and then ‘import’ that into the old main program.

I jumped into wxPython, as it seems to be the right GUI for me, and downloaded some examples that I took apart and joined. Now the basic GUI is running, though neither beautiful nor complete.

The main task for my GUI is to allow the user to point to an input file.
It can now obtain a filename from a file selection dialog, but I can’t figure out how to get the filename, i.e. the string variable containing the file name, send back to the main program…

I attach the two .py files hereunder.

My question is:
How do I get the information from variable strSequenceFile, back to the main module in file jacxl.py ?

Best Regards
Martin Hvidberg

# ====== File jacxl.py =============
import jacXlgui

if __name__ == "__main__":
print "Hello World - from Main";

result = jacXlgui.app.MainLoop()

print 'Result:',result
# ====== End of jacxl.py =============

# ====== File jacXlgui.py =============
#----------------------------------------------------------------------
# A very simple wxPython GUI.
# To go with JakXl.py
#----------------------------------------------------------------------

import wx, os

class JacXlFrame(wx.Frame):
"""
This is a Frame. It just shows a few controls on a wxPanel,
and has a simple menu.
"""
def __init__(self, parent, title):
wx.Frame.__init__(self, parent, -1, title, pos=(150, 150), size=(350, 350))

# Create the menubar
menuBar = wx.MenuBar()
# and some menus
filemenu = wx.Menu()
helpmenu = wx.Menu()
# add an item to the menu
filemenu.Append(wx.ID_EXIT, "E&xit\tAlt-X", "Leave without running anything ...")
helpmenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
# bind the menu event to an event handler
self.Bind(wx.EVT_MENU, self.OnTimeToClose, id=wx.ID_EXIT)
self.Bind(wx.EVT_MENU, self.OnShowAbout, id=wx.ID_ABOUT)
# and put the menu on the menubar
menuBar.Append(filemenu, "&File")
menuBar.Append(helpmenu, "&Help")
self.SetMenuBar(menuBar)

# Status bar
self.CreateStatusBar()

# Now create the Panel to put the other controls on.
panel = wx.Panel(self)
# A text...
text = wx.StaticText(panel, -1, "Please select a \"Sequence\" file")
text.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL)) # , wx.BOLD
text.SetSize(text.GetBestSize())
# Where is the Sequence file ?
self.txcI = wx.TextCtrl(panel, -1, "<file name>", size=(300, -1))

# Some buttons
btnFileFind = wx.Button(panel, -1, "Browse for file")
btnClose = wx.Button(panel, -1, "Exit")
btnRun = wx.Button(panel, -1, "Run")
# bind the button events to handlers
self.Bind(wx.EVT_BUTTON, self.OnFindFileButton, btnFileFind)
self.Bind(wx.EVT_BUTTON, self.OnRunButton, btnRun)
self.Bind(wx.EVT_BUTTON, self.OnTimeToClose, btnClose)
# Use a sizer to layout the controls, stacked vertically and with
# a 10 pixel border around each
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(text, 0, wx.ALL, 10)
sizer.Add(self.txcI, 0, wx.ALL, 10)
sizer.Add(btnFileFind, 0, wx.ALL, 10)
sizer.Add(btnRun, 0, wx.ALL, 10)
sizer.Add(btnClose, 0, wx.ALL, 10)
panel.SetSizer(sizer)
panel.Layout()

def OnFindFileButton(self, evt):
strReturn = ''
wildcard = "Sequence Log (*.LOG)|*.LOG|" \
"All files (*.*)|*.*"
dialog = wx.FileDialog(None, "Choose a file", os.getcwd(), "", wildcard, wx.OPEN)
if dialog.ShowModal() == wx.ID_OK:
strSequenceFile = dialog.GetPath()
self.txcI.ChangeValue(strSequenceFile) # Copying filename to text box
dialog.Destroy()
print 'Found file:',strSequenceFile
return strSequenceFile

def OnShowAbout(self, evt):
print "About this program ..."

def OnRunButton(self, evt):
"""Event handler for the button click."""
print 'Run :',self.txcI.GetValue()
return self.txcI.GetValue()

def OnTimeToClose(self, evt):
"""Event handler for the button click."""
print "See ya later!"
self.Close()

class JacXlApp(wx.App):
def OnInit(self):
frame = JacXlFrame(None, "JacXl - converting ... to eXcel")
self.SetTopWindow(frame)
print "Print statements go to this stdout window by default."
frame.Show(True)
return True

app = JacXlApp(redirect=False)
# ====== End of jacXlgui.py =============
 
M

Mel

Martin said:
I have a Python program, which has until now, been running in command line
mode only. I wish to add a GUI.

I would like to develop (and maintain) the GUI part in a separate module,
i.e. in its own .py file, and then ‘import’ that into the old main
program.

I jumped into wxPython, as it seems to be the right GUI for me, and
downloaded some examples that I took apart and joined. Now the basic GUI
is running, though neither beautiful nor complete.

The main task for my GUI is to allow the user to point to an input file.
It can now obtain a filename from a file selection dialog, but I canÂ’t
figure out how to get the filename, i.e. the string variable containing
the file name, send back to the main programÂ…

I attach the two .py files hereunder.

My question is:
How do I get the information from variable strSequenceFile, back to the
main module in file jacxl.py ?

AFAIK, typically, you don't -- the way it is here. Returning a value from a
Button handler, or any event handler generally, won't have any effect. The
event handlers are called from deep in wx code by routines that don't deal
with anything specific to the data-processing side of the program.

What I think you might do is to make strSequenceFile an attribute of your
Frame, so that OnFindFile button does ``self.strSequenceFile =
dialog.GetPath()'' rather than returning that value.

Then your main level can do ``jacXlgui.app.GetTopWindow().strSequenceFile''
..

There are probably refinements to be added to this, but I think it's a good
beginning strategy. The first new place I would take the whole program
would be to remove the command-line controls from the command line program,
so you're left with a sort of "business model" that contains only the data
processing. Then you can write a new GUI program based on jacXlgui that
imports the data processing module and calls computations and reports
results from the model.


Mel.
 
B

bobicanprogram

AFAIK, typically, you don't -- the way it is here. Returning a value from a
Button handler, or any event handler generally, won't have any effect. The
event handlers are called from deep in wx code by routines that don't deal
with anything specific to the data-processing side of the program.

What I think you might do is to make strSequenceFile an attribute of your
Frame, so that OnFindFile button does ``self.strSequenceFile =
dialog.GetPath()'' rather than returning that value.

Then your main level can do ``jacXlgui.app.GetTopWindow().strSequenceFile''
.

There are probably refinements to be added to this, but I think it's a good
beginning strategy. The first new place I would take the whole program
would be to remove the command-line controls from the command line program,
so you're left with a sort of "business model" that contains only the data
processing. Then you can write a new GUI program based on jacXlgui that
imports the data processing module and calls computations and reports
results from the model.

Mel.


Another way to encapsulate functionality like a GUI is to "hide" it
behind a nice clean messaging interface. The SIMPL toolkit (http://
www.icanprogram.com/06py/lesson1/lesson1.html) promotes this kind of
design. SIMPL allows Python to Python messaging as well as Python to
C, C++, JAVA or Tcl/Tk so what you ultimately choose for the GUI has
only minimal impact on what you choose for your "processing engine".

Happy coding.

bob
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top