tkinter, overwrite Label-text?

skanemupp at yahoo.se skanemupp at yahoo.se
Fri Apr 11 13:01:42 CEST 2008


On 10 Apr, 18:03, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Thu, 10 Apr 2008 07:37:08 -0700, skanemupp wrote:
> > i know how to do this already. the problem is i want the text to stay
> > in the windowa nd not start overwriting "Answer:".
>
> Then don't use `place()` but let Tkinter handle the layout with the pack
> and/or grid layout manager.  GUIs with `place()` are a bad idea because
> the GUI may look odd or is even unusable on other peoples computers with
> other screen resolutions, fonts, and font sizes.
>
> Ciao,
>         Marc 'BlackJack' Rintsch

ok but i have trouble using grid. if i try to use a Label witht he
text answer in the following code it doenst work very well.


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)

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

c = Entry(mygui)
c.grid(row=2, column=1, columnspan=4)

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)
    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)
b = Button(mygui, text="C",command=Erase, width=2, height=1)
b.grid(row=8, column=3)
b = Button(mygui, text="c",command=Backspace, width=2, height=1)
b.grid(row=8, column=4)
b = Button(mygui, text="=",command=Calc, width=18, height=1)
b.grid(row=9, column=1, columnspan=8)

mygui.mainloop()



More information about the Python-list mailing list