473,414 Members | 1,688 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Tkinter, a lot of buttons, make prog shorter?

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
3 1841

<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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Mulac | last post by:
Is Tkinter also compatible wih GNOME? I wrote a program in python that displays 2 buttons for starting and stopping the execution of a program. I used Tkinter for the gui. I want the program to...
2
by: Jim Anderson | last post by:
I'm running Kubuntu a derivative of Debian Linux. I'm using Python 2.4 and tcl/tk 8.4. I'm running Tkinter programs and they were running about a month ago. When I tried them again yesterday, I...
8
by: Jay | last post by:
I'm having a problem using lambda to use a command with an argument for a button in Tkinter. buttons = range(5) for x in xrange(5): buttons = Button(frame, text=str(x+1), command=lambda:...
6
by: Eric_Dexter | last post by:
Instead of creating my buttons and waiting for me to press them to execute they are executing when I create them and won't do my callback when I press them.. thanks for any help in advance ...
0
bartonc
by: bartonc | last post by:
You can find the original author of the script by ggling " Py2Exe version 6.3 setup" The cool thing about this is that it calls py2exe, just in case you're uncomfortable with the command line. I had...
4
by: Miki | last post by:
Hello, I have a simple Tkinter window with and buttons at the bottom. When I resize the window to be shorter, the first thing to disappear are the buttons, however I want these button to be...
13
by: Daniel Fetchinson | last post by:
Was looking at PEP 3108, http://www.python.org/dev/peps/pep-3108/ , Is it just me or others also think that it would be a major loss to remove tkinter from the python core? PEP 3108 starts off...
1
by: viv1tyagi | last post by:
Hi everyone ! ! ! I'm just a month old in the world of Python and trying to develop an application using Tkinter in which a new window pops out on a particular button press.The code for this new...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.