By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,675 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,675 IT Pros & Developers. It's quick & easy.

Tkinter, a lot of buttons, make prog shorter?

P: n/a
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,he ight=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)
Apr 6 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a

<sk*******@yahoo.sewrote in message
news:c9**********************************@j1g2000p rb.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,he ight=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


Apr 6 '08 #2

P: n/a
sk*******@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.
You can look up the command in a dictionary:

commands = {"C": self.Clean}
top = 3
for i, text in enumerate("123+456-789*0Cr/"):
row, column = divmod(i, 4)
try:
command = commands[text]
except KeyError:
def command(n=text):
self.Display(n)
button = Button(self, text=text, command=command, width=2, height=2)
button.grid(row=top+row, column=column)

The problem with this is that the shorter code may take a bit longer to
understand, so that there is no net win for the reader. A compromise would be
to define a helper function:

def make_button(self, row, column, text, command):
# implementation left as an exercise

# use it:
self.make_button(3, 0, "1", lambda n="1": ...)
self.make_button(3, 1, "2", lambda n="2": ...)
....

Peter
Apr 6 '08 #3

P: n/a
On Apr 6, 1:59*pm, skanem...@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,he ight=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?
Apr 6 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.