[Tkinter-discuss] New member howdy And is there a better way to use Buttons ?

John McMonagle jmcmonagle at velseis.com.au
Thu Jun 12 07:48:32 CEST 2008


John Edens wrote:
> Hi all, I’ve just started working with Python and Tkinter.
> 
>  
> 
> One of the things I think I’ve noted is that Button commands do not pass
> parameters.
> 
>  
> 
> Is there a way around this?

The short answer is...  use a lambda or a named function.

> 
>  
> 
> And is there a better way to code the following:
> 
>  
> 
> I’ve written a small application that puts a Label in row 0 of a Frame
> and puts 26 buttons labeled A – Z in the second row.
> 
>  
> 
> When the user clicks on a button, the associated letter is displayed in
> the label.
> 
>  
> 

Simplified....

# Creates a label in row 0
# Creates 26 buttons A - Z in row 2
# Clicking a button changes the displayed label

from Tkinter import *
import string

class Application(Frame):
    def __init__(self,master=None):
        Frame.__init__(self, master)
        self.grid()
        self.tv = StringVar()
        self.tv.set("?")
        self.lText = Label(master, textvariable = self.tv)
        self.lText.grid(row = 0, columnspan = 26)
        col = 0
        for letter in string.uppercase:
            Button(master, text=letter, command=lambda input=letter:
self.tv.set(input)).grid(row=2, column=col)
            col += 1

root = Tk()
app = Application(root)
root.mainloop()



Regards,

John


More information about the Tkinter-discuss mailing list