Hi,

I'm writing a small GUI program in Python/Tkinter (my first Python
program).  I want to make a menu which lists the names of a number of
text files that my program uses/generates.  When I select one of the
files from the menu, I would like a new window to open up a scroll box
containing the file.  I was able to get it to work by hard a separate
function for each file name in the addmenuitem for each file, but as the
number of files grows, that gets to be tedious.  So I'm trying to write
the menu bar as a loop, but I don't know how to get the file name passed
to the function if the I use only one function call.  Below is what I'm
trying to do, but it doesn't work since the file name or parent window
is passed to the function.  Can anyone give me some tips on a good way
to accomplish this without hard-coding a different function for each
menu item?  Thanks.

John

class App:
  def __init__(self,parent):

    self.inputfiles = ('input.txt', 'EdgeSlotArray.txt', 'RectWave.txt',
      'RectWaveWithBaffles.txt', 'Suppressor.txt', 'gaopt.txt')
    self.outputfiles = ('s11.dat', 's21.dat')
    self.allfiles = self.inputfiles+self.outputfiles

    self.master = parent
    top = Frame(parent)
    top.pack(side='top')

    firstrow = Frame(top)
    firstrow.pack(side='top')

    self.showfilemenu_bar = Pmw.MenuBar(firstrow,
        hull_borderwidth=1)
    self.showfilemenu_bar.pack(side='left')
    self.showfilemenu_bar.addmenu('Show Files',None,tearoff=True)
    for file in self.allfiles:
      self.showfilemenu_bar.addmenuitem('Show Files',
        'command',label=file, command=self.ShowFile)

  def ShowFile(self,fname,parent):
    filewindow=Toplevel(parent)
    file = open(fname,'r')
    filestr = file.read()
    file.close()
    filetext=Pmw.ScrolledText(filewindow,
      borderframe=5, vscrollmode='dynamic',
      labelpos='n',label_text=fname,
      text_font=Pmw.logicalfont('Courier'),
      text_width=70,text_height=25,text_wrap='none')
    filetext.pack(expand=True,fill='both')
    filetext.insert('end',filestr)

    Button(filewindow,text='Close',
      command=filewindow.destroy).pack(pady=10)
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to