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) 2 2386
<gh*****@yahoo.com> wrote: 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> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Josh |
last post by:
Caution, newbie approaching...
I'm trying to come up with a very simple Tkinter test application that
consists of a window with a drop-down menu bar at the top and a grid
of colored rectangles...
|
by: midtoad |
last post by:
I'm trying to display a GIF image in a label as the central area to a
Tkinter GUI. The image does not appear, though a space is made for it.
Why is this so?
I notice that I can display a GIF...
|
by: Paul Rubin |
last post by:
I have a gui with a bunch of buttons, labels, the usual stuff. It
uses the grid manager:
gui = Frame()
gui.grid()
gui.Label(....).grid() # put some widgets into the gui
... # more widgets...
|
by: Stewart Midwinter |
last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item
that does nothing more than fetch a directory listing and display it
in a Toplevel window (I'd use a tkMessageBox showinfo...
|
by: ishtar2020 |
last post by:
Hi everybody
I'd appreciate some help on creating a tear off menu with TkInter. I've
been reading some documentation but still no luck.
Please don't get confused: when I mean "tear off" menu I...
|
by: Dustan |
last post by:
I don't know if this is because of Tkinter (ie Tk) itself or the
Windows default way of handling things, but when I create a very long
menu (my test is shown below), the way it displays is rather...
|
by: Edward K. Ream |
last post by:
Hello all,
Creating a 'Help' menu 'by hand' on the Mac does not work, or rather, it
creates a *second* Help menu.
There are hints about how to do this at:
|
by: Doran, Harold |
last post by:
I am currently reading An Intro to Tkinter (1999) by F. Lundh. This doc
was published in 1999 and I wonder if there is a more recent version.
I've googled a bit and this version is the one I keep...
|
by: joshdw4 |
last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes,
it's that pesky root window and I have tried withdraw to no avail. I'm
assuming this is because of the methods I'm using. I...
|
by: Eric Brunel |
last post by:
Hello all,
I'm trying out Python 2.6 and I found what might be a bug in the Tkinter
module. How can I report it?
The possible bug is a traceback when trying to delete a menu item in a
menu...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |