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

John Edens jedens at cabinc.com
Wed Jun 11 21:52:21 CEST 2008


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?

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.

Code follows:


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

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)


        butA = Button(master, text= 'A', command = self.choseA).grid(row = 2, column = 0 )

        butB = Button(master, text= 'B', command = self.choseB).grid(row = 2, column = 1 )

        butC = Button(master, text= 'C', command = self.choseC).grid(row = 2, column = 2 )

        butD = Button(master, text= 'D', command = self.choseD).grid(row = 2, column = 3 )

        butE = Button(master, text= 'E', command = self.choseE).grid(row = 2, column = 4 )

        butF = Button(master, text= 'F', command = self.choseF).grid(row = 2, column = 5 )

        butG = Button(master, text= 'G', command = self.choseG).grid(row = 2, column = 6 )

        butH = Button(master, text= 'H', command = self.choseH).grid(row = 2, column = 7 )

        butI = Button(master, text= 'I', command = self.choseI).grid(row = 2, column = 8 )

        butJ = Button(master, text= 'J', command = self.choseJ).grid(row = 2, column = 9 )

        butK = Button(master, text= 'K', command = self.choseK).grid(row = 2, column = 10 )

        butL = Button(master, text= 'L', command = self.choseL).grid(row = 2, column = 11 )

        butM = Button(master, text= 'M', command = self.choseM).grid(row = 2, column = 12 )

        butN = Button(master, text= 'N', command = self.choseN).grid(row = 2, column = 13 )

        butO = Button(master, text= 'O', command = self.choseO).grid(row = 2, column = 14 )

        butP = Button(master, text= 'P', command = self.choseP).grid(row = 2, column = 15 )

        butQ = Button(master, text= 'Q', command = self.choseQ).grid(row = 2, column = 16 )

        butR = Button(master, text= 'R', command = self.choseR).grid(row = 2, column = 17 )

        butS = Button(master, text= 'S', command = self.choseS).grid(row = 2, column = 18 )

        butT = Button(master, text= 'T', command = self.choseT).grid(row = 2, column = 19 )

        butU = Button(master, text= 'U', command = self.choseU).grid(row = 2, column = 20 )

        butV = Button(master, text= 'V', command = self.choseV).grid(row = 2, column = 21 )

        butW = Button(master, text= 'W', command = self.choseW).grid(row = 2, column = 22 )

        butX = Button(master, text= 'X', command = self.choseX).grid(row = 2, column = 23 )

        butY = Button(master, text= 'Y', command = self.choseY).grid(row = 2, column = 24 )

        butZ = Button(master, text= 'Z', command = self.choseZ).grid(row = 2, column = 25 )




    def choseA(self):
        self.resolveChoice( 65 )


    def choseB(self):
        self.resolveChoice( 66 )


    def choseC(self):
        self.resolveChoice( 67 )


    def choseD(self):
        self.resolveChoice( 68 )


    def choseE(self):
        self.resolveChoice( 69 )


    def choseF(self):
        self.resolveChoice( 70 )


    def choseG(self):
        self.resolveChoice( 71 )


    def choseH(self):
        self.resolveChoice( 72 )


    def choseI(self):
        self.resolveChoice( 73 )


    def choseJ(self):
        self.resolveChoice( 74 )


    def choseK(self):
        self.resolveChoice( 75 )


    def choseL(self):
        self.resolveChoice( 76 )


    def choseM(self):
        self.resolveChoice( 77 )


    def choseN(self):
        self.resolveChoice( 78 )


    def choseO(self):
        self.resolveChoice( 79 )


    def choseP(self):
        self.resolveChoice( 80 )


    def choseQ(self):
        self.resolveChoice( 81 )


    def choseR(self):
        self.resolveChoice( 82 )


    def choseS(self):
        self.resolveChoice( 83 )


    def choseT(self):
        self.resolveChoice( 84 )


    def choseU(self):
        self.resolveChoice( 85 )


    def choseV(self):
        self.resolveChoice( 86 )


    def choseW(self):
        self.resolveChoice( 87 )


    def choseX(self):
        self.resolveChoice( 88 )


    def choseY(self):
        self.resolveChoice( 89 )


    def choseZ(self):
        self.resolveChoice( 90 )



    def resolveChoice(self,asciiVal):
        self.tv.set(chr(asciiVal))

root = Tk()

app = Application(root)

root.mainloop()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20080611/1ef9a982/attachment.htm>


More information about the Tkinter-discuss mailing list