468,168 Members | 1,720 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,168 developers. It's quick & easy.

tkinter, annoying grid-problem

so my little calculator works perfectly now. just having some trouble
with the layout.
this whole tkinter-thing seems to be more tricky than it should be.
how can i make the 4 column of buttons have the same distance and
size between them as the other 3 columns?
and how can i make the top entry end where the 2nd row entry
ends(meaning the top entry will be longer)?

why are the 4th row split from the others? hard to fix the problems
when u dont even understand why things happen. seems so llogical a lot
of it. i change something then something unexpected happens.

from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title("Calculator")

l = Label(mygui, text="Answer: ")
l.grid(row=2, column=1, columnspan=2, sticky=W)

e = Entry(mygui)
e.grid(row=1, column=1, columnspan=4, sticky=W)

c = Entry(mygui)
c.grid(row=2, column=3, columnspan=4, sticky=W)

def Disp(nstr):
e.insert(INSERT, nstr)

def Calc():
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, "Not computable")

def Erase():
e.delete(0,END)
c.delete(0, END)

def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)
x = 1
y = 4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char, command=lambda n=char:Disp(n),
width=2, height=1)
b.grid(row=y, column=x, sticky=W)
x=x+1
if x==5:
x=1
y=y+1

b = Button(mygui, text="^", command=lambda n="**":Disp(n), width=2,
height=1)
b.grid(row=8, column=2, sticky=W)
b = Button(mygui, text="C",command=Erase, width=2, height=1)
b.grid(row=8, column=3, sticky=W)
b = Button(mygui, text="c",command=Backspace, width=2, height=1)
b.grid(row=8, column=4, sticky=W)
b = Button(mygui, text="=",command=Calc, width=18, height=1)
b.grid(row=9, column=1, columnspan=4, sticky=W)

mygui.mainloop()
Jun 27 '08 #1
2 3199
On Friday 11 April 2008 18:41,
sk*******@yahoo.se wrote:
so my little calculator works perfectly
now. just having some trouble with the
layout.
this whole tkinter-thing seems to be more
tricky than it should be. how can i make
the 4 column of buttons have the same
distance and size between them as the
other 3 columns? and how can i make the
top entry end where the 2nd row entry
ends(meaning the top entry will be
longer)?

why are the 4th row split from the others?
hard to fix the problems when u dont even
understand why things happen. seems so
llogical a lot of it. i change something
then something unexpected happens.
Look this file over.

I built two additional frames.
You can control the looks of the progect
easier with the additional frames.

The calculator DOES NOT WORK. You will have
to have to play with it to get the results
that you want, which you can do.

jim-on-linux
http://www.inqvista.com
######################################
class Calc :
def __init__ (self) :
self.mygui = Tk()
self.mygui.title("Calculator")
self.MkButtons()

def MkButtons(self):
mygui = self.mygui
dataFra = Frame(mygui)
dataFra.grid(row = 0, column = 0)
l = Label(dataFra, text="Answer: ")
l.grid(row=0, column=0, sticky=EW)
self.e = Entry(dataFra)
self.e.grid(row=1, column=0,
sticky=EW)
c = Entry(dataFra)
c.grid(row=2, column=0, sticky=EW)

x = 1
y = 4

butFra = Frame(mygui)
butFra.grid(row=1, column=0)
for n in '123+456-789*0()/.':
b = Button(butFra, text= n,
command =
lambda :self.Disp(n),
width=2, height=1)
b.grid(row=y, column=x, sticky=W)
x=x+1
if x==5:
x=1
y=y+1
b = Button(butFra, text="^",
command=self.Disp, width=2,
height=1)
b.grid(row=8, column=2, sticky=W)
b = Button(butFra,
text="C",command=self.Erase, width=2,
height=1)
b.grid(row=8, column=3, sticky=W)
b = Button(butFra,
text="c",command=self.Backspace, width=2,
height=1)
b.grid(row=8, column=4, sticky=W)
b = Button(butFra,
text="=",command=self.Calc, width=18,
height=1)
b.grid(row=9, column=1, columnspan=4,
sticky=EW)

def Disp(self, n):
print n, '## n cal 68\n'
n = ord(n)
self.e.insert( 0,str( n))

def Calc(self):
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, "Not computable")
def Erase(self):
e.delete(0,END)
c.delete(0, END)

def Backspace(self):
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)

if __name__ == '__main__' :
Calc()
mainloop()

###################################
###################################
from __future__ import division
import Tkinter
from Tkinter import *

mygui = Tkinter.Tk()

mygui.title("Calculator")

l = Label(mygui, text="Answer: ")
l.grid(row=2, column=1, columnspan=2,
sticky=W)

e = Entry(mygui)
e.grid(row=1, column=1, columnspan=4,
sticky=W)

c = Entry(mygui)
c.grid(row=2, column=3, columnspan=4,
sticky=W)

def Disp(nstr):
e.insert(INSERT, nstr)

def Calc():
expr=e.get()
c.delete(0, END)
try:
c.insert(END, eval(expr))
except:
c.insert(END, "Not computable")

def Erase():
e.delete(0,END)
c.delete(0, END)

def Backspace():
a=len(e.get())
e.delete(a-1,END)
#e.delete(INSERT, END)
#e.delete(ANCHOR,END)
x = 1
y = 4
for char in '123+456-789*0()/.':
b = Button(mygui, text=char,
command=lambda n=char:Disp(n), width=2,
height=1)
b.grid(row=y, column=x, sticky=W)
x=x+1
if x==5:
x=1
y=y+1

b = Button(mygui, text="^", command=lambda
n="**":Disp(n), width=2, height=1)
b.grid(row=8, column=2, sticky=W)
b = Button(mygui, text="C",command=Erase,
width=2, height=1) b.grid(row=8, column=3,
sticky=W) b = Button(mygui,
text="c",command=Backspace, width=2,
height=1) b.grid(row=8, column=4,
sticky=W) b = Button(mygui,
text="=",command=Calc, width=18, height=1)
b.grid(row=9, column=1, columnspan=4,
sticky=W)

mygui.mainloop()
Jun 27 '08 #2
sk*******@yahoo.se wrote:
so my little calculator works perfectly now. just having some trouble
with the layout.
this whole tkinter-thing seems to be more tricky than it should be.
how can i make the 4 column of buttons have the same distance and
size between them as the other 3 columns?
and how can i make the top entry end where the 2nd row entry
ends(meaning the top entry will be longer)?

why are the 4th row split from the others? hard to fix the problems
when u dont even understand why things happen. seems so llogical a lot
of it. i change something then something unexpected happens.
The best answer I can give (being a Tk expert but not yet a tkinter
expert) is to start with a piece of graph paper. Draw the GUI out and
you'll probably see what the problems are. For one, the second entry
(for the answer) spans 4 columns and begins at column 3, so it ends up
in column 6. This ends up affecting the whole layout because nothing
else goes to column six.

You'll probably find it much easier going to break down your GUI into
sections. One for the calculator buttons and one for everything else.
Create a frame for the buttons and it becomes trivial to layout out all
the buttons in a 4x6 grid, unaffected by things outside that grid. Then,
create your other widgets and grid the whole frame of buttons as a
single unit inside the outermost frame. I quite often use grid for
interior groupings, then use pack on the outter-most frame to manager
the various groups.

Using the grid layout manager is trivial if you do a little thinking and
planning up front. If you don't, you can spend all day chasing down why
you end up with an extra blank row or column, unusually sized rows and
columns, etc. Again, get a piece of graph paper and draw it out -- that
helps immensely when you're first coming up to speed using grid.
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Patrick L. Nolan | last post: by
2 posts views Thread by anx | last post: by
3 posts views Thread by dwelch91 | last post: by
1 post views Thread by Stan Cook | last post: by
5 posts views Thread by crystalattice | last post: by
6 posts views Thread by Adam | last post: by
kaarthikeyapreyan
1 post views Thread by kaarthikeyapreyan | last post: by
1 post views Thread by skanemupp | last post: by
reply views Thread by Kevin McKinley | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.