Tkinter, a lot of buttons, make prog shorter?

Terry Reedy tjreedy at udel.edu
Sun Apr 6 16:34:23 EDT 2008


<skanemupp at yahoo.se> wrote in message 
news:c9bf3bb9-71e4-48f6-aae0-fa9400ca8155 at j1g2000prb.googlegroups.com...
| is there anyway to make this shorter? i hate having these big blocks
| of similar-looking code, very unaesthetic.
| maybe doesnt matter good-code-wise?
| anyway can i make some function that makes this shorter?
| like put the etiquettes on the button froma string of
| '123+456-789*0Cr/' ?
| problem is the command and lambda-func for each button is different.
|
|
|        self.btnDisplay = Button(self,text='1',command=lambda
| n="1":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=3, column=0)
|
|        self.btnDisplay = Button(self,text='2',command=lambda
| n="2":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=3, column=1)
|
|        self.btnDisplay = Button(self,text='3',command=lambda
| n="3":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=3, column=2)
|
|        self.btnDisplay = Button(self,text='+',command=lambda
| n="+":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=3, column=3)
|
|        self.btnDisplay = Button(self,text='4',command=lambda
| n="4":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=4, column=0)
|
|        self.btnDisplay = Button(self,text='5',command=lambda
| n="5":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=4, column=1)
|
|        self.btnDisplay = Button(self,text='6',command=lambda
| n="6":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=4, column=2)
|
|        self.btnDisplay = Button(self,text='-',command=lambda
| n="-":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=4, column=3)
|
|        self.btnDisplay = Button(self,text='7',command=lambda
| n="7":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=5, column=0)
|
|        self.btnDisplay = Button(self,text='8',command=lambda
| n="8":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=5, column=1)
|
|        self.btnDisplay = Button(self,text='9',command=lambda
| n="9":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=5, column=2)
|
|        self.btnDisplay = Button(self,text='*',command=lambda
| n="*":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=5, column=3)
|
|        self.btnDisplay = Button(self,text='0',command=lambda
| n="0":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=6, column=0)
|
|        self.btnDisplay =
| Button(self,text='C',command=self.Clean,width=2,height=2)
|        self.btnDisplay.grid(row=6, column=1)
|
|        self.btnDisplay = Button(self,text='r',command=lambda
| n="r":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=6, column=2)
|
|        self.btnDisplay = Button(self,text='/',command=lambda
| n="/":self.Display(n),width=2,height=2)
|        self.btnDisplay.grid(row=6, column=3)

With the exception of the 'C' button, the only thing different is the label 
and position.
 I believe (untested, obviously)

def btn(self, txt, r, c):
    self.btnDisplay = Button(self, text=txt, command=lambda: 
self.Display(txt), width=2,height=2)
    self.btnDisplay.grid(row=r, column=r)

will work.

tjr









More information about the Python-list mailing list