Tkinter, a lot of buttons, make prog shorter?

7stud bbxx789_05ss at yahoo.com
Sun Apr 6 18:12:37 EDT 2008


On Apr 6, 1:59 pm, skanem... at yahoo.se wrote:
> 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)

You said you were an experienced programmer, but you keep posting code
with the same mistakes over and over again.  Why are you attaching the
buttons to self?



More information about the Python-list mailing list