tkinter, annoying grid-problem

skanemupp at yahoo.se skanemupp at yahoo.se
Fri Apr 11 18:41:10 EDT 2008


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()



More information about the Python-list mailing list