[Tutor] Entry box + browse button

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Aug 15 09:44:36 CEST 2004


> Some idea i had was to call a function from the "command" in
> self.inputBrowseButton (self.openFile) and change the value of
self.infile in
> that function. But i am not so sure about that idea anymore.

Thats pretty close.

The simplest way is two nearly identical methods:

def browseInput(self)
    name = tkFileDialog.askopenfilename()
    self.inputEntry.insert(INSERT,name)

def browseInput(self)
    name = tkFileDialog.askopenfilename()
    self.outputEntry.insert(INSERT,name)


and set those as the command options in the two browse buttons:

self.inputBrowseButton = Button(master, command = self.browseInput)

self.outputBrowseButton = Button(master, command = self.browseOutput)

We can be slightly more clever than that by writing a single browse
method


def setFilename(self, entry=self.inputEntry):
    name = tkFileDialog.askopenfilename()
    entry.inset(INSERT,name)

And use a lambda to pass the appropriate widget:

self.inputBrowseButton = Button(master,
           command = lambda : self.setFilename(self.inputEntry))

self.outputBrowseButton = Button(master,
           command = lambda : self.setFilename(self.outputEntry))


HTH.

Alan G.



More information about the Tutor mailing list