newbie with major "lambda" problem (perhaps a scope problem as well)

Joe Potter jm7potter at hotmail.com
Wed Jun 27 08:27:32 EDT 2001


On Tue, 26 Jun 2001 12:03:44 -0700, Chris Barker <chrishbarker at home.net> wrote:

>Joe Potter wrote:
>> >>     # the "button" below works like a champ !!
>> >>     #Button(root, text='Fetch',
>> >>                  #command=(lambda v=vars: fetch(v))).pack(side=LEFT)
>> >>
>> >>     # the "button" below does not do anything ??????
>> >>     Button(root, text='Fetch', command=(fetch(vars))).pack(side=LEFT)

<snip --- all good stuff>


>
>Because when the button is pressed, no parameters will be passed to the
>command function, and fetch needs a list of variable to be passed in.
>That's what the default parameter assignment does: my command_fun (or
>the lambda function) does not need to have any parameters passed to it,
>it will assign v to vars, and then pass that in to fetch.
>
>I'm not sure I made this very clear, but I tried 
>
>-Chris
>

Chris, 

Yes, you helped a lot. Thanks very much.

To clear up the matter, I would ask one more favor. Would you please look at the code
below and tell me how to move the "button" calls out of the class and down into the 
" if __name__ == __main__" portion of the code? I think that will take care of any
remaining problems I have.

Thanks in advance!

Regards, Joe


###################### code start (note will need a little txt file 
##############################

import sys
from Tkinter import *
from tkMessageBox import askokcancel
import Pmw

        
class SumGrid(Frame):
    def __init__(self, parent=None, numrow=5, numcol=5):
        Frame.__init__(self, parent)
        self.numrow = numrow                       # I am a frame container
        self.numcol = numcol                       # caller packs or grids me
        self.makeWidgets(numrow, numcol)           # else only usable one way

    def makeWidgets(self, numrow, numcol):
        self.rows = []
        for i in range(numrow):
            cols = []
            for j in range(numcol):
                e = Entry(self, relief=RIDGE)
                e.grid(row=i+1, column=j, sticky=NSEW)
                e.insert(END, 'xx')
                cols.append(e)
            self.rows.append(cols)

            

        Button(self, text='Print', command=self.onPrint).grid(row=0, column=0)
        Button(self, text='Clear', command=self.onClear).grid(row=0, column=1)
        Button(self, text='Load',  command=self.onLoad).grid(row=0, column=2)
   

    def onPrint(self):
        for row in self.rows:
            for col in row:
                print col.get(),
            print
        print


    def onClear(self):
        for row in self.rows:
            for col in row:
                col.delete('0', END)
                col.insert(END, '')
 
    def onLoad(self):
        import string
        #from tkFileDialog import *
        #file = askopenfilename()
        file = "joe.txt"
        if file:
            for r in self.rows:
                for c in r: c.grid_forget()

            myfile = open(file, 'r')
            filelines   = myfile.readlines()
            myfile.close()
            self.numrow = len(filelines)
            self.numcol = len(string.split(filelines[0]))
            self.makeWidgets(self.numrow, self.numcol)

            row = 0
            for line in filelines:
                fields = string.split(line)
                for col in range(self.numcol):
                    self.rows[row][col].delete('0', END)
                    self.rows[row][col].insert(END, fields[col])
                row = row+1

            
if __name__ == '__main__':

    root = Tk()
    root.title('test a button') 

    sf = Pmw.ScrolledFrame(root,
            usehullsize = 1,
            hull_width = 600,
            hull_height = 300,
        )

    sf.pack(side = "top", padx = 10, pady = 6, fill = 'both', expand = 1)

# can we move the button calls to here, and hence --- pur them in root????


    iframe = sf.interior()
    SumGrid(iframe).pack()
    mainloop()

######################## end code
##########################

You need a "joe.txt" file --- the below is as good as any

##############

1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8



More information about the Python-list mailing list