Returning value from home made unit - how to?
Martin Hvidberg
martin at hvidberg.net
Sun May 30 14:58:29 EDT 2010
Dear <python-list at python.org>
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 =============
More information about the Python-list
mailing list