Tkinter menu

G

gh14tq5

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)
 
F

Fredrik Lundh

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.

the easiest way to do this is to create a new function object for each
file, and use default argument binding to pass in the right filename:

for file in self.allfiles:
def callback(fname=file):
self.showFile(fname, parent)
self.showfilemenu_bar.addmenuitem('Show Files',
'command',label=file, command=callback)

the "def" statement creates a new instance of the callback function for
each iteration. the "fname" variable is bound to the current file object,
while "self" and "parent" are bound to the variables with the same names
in the outer scope.

</F>
 
G

gh14tq5

the easiest way to do this is to create a new function object for each
file, and use default argument binding to pass in the right filename:

for file in self.allfiles:
def callback(fname=file):
self.showFile(fname, parent)
self.showfilemenu_bar.addmenuitem('Show Files',
'command',label=file, command=callback)

the "def" statement creates a new instance of the callback function for
each iteration. the "fname" variable is bound to the current file object,
while "self" and "parent" are bound to the variables with the same names
in the outer scope.

</F>

That was exactly what I was looking for. It worked perfectly. Thanks.

John
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top