How do I let the Tkinter GUI do not replace a string?

advertisements

I was writing a code in Python (with Tkinter), and I couldn't fix a little problem. This is my code:

from tkinter import *

def show_entry_fields():
    straal = int(e1.get())
    area = 3.143 * (straal * straal)
    area_label['text'] = str(area)

master = Tk()
Label(master, text="Straal").grid(row=0)
e1 = Entry(master)
e1.grid(row=0, column=1)

area_label = Label(master, text="Oppervlakte")
area_label.grid(row=2)

Button(master, text='Sluit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Bereken oppervlakte', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)

mainloop()

When I run the program, then it does work, but if it calculates the area, then the text 'oppervlakte' gets replaced by the answer. Before you calculate: http://imgur.com/zw7wA6D

After you calculate: http://imgur.com/RV94QTw

How can I let the GUI display:

Oppervlakte: 78.5749999999

Thanks in advance


You are replacing your label in the function you have: area_label['text'] = str(area) instead of creating a new label, create a new label with no text and then populate it in your function:

from tkinter import *

def show_entry_fields():
    straal = int(e1.get())
    area = 3.143 * (straal * straal)
    calculation_label['text'] = str(area)    #using new label

master = Tk()
Label(master, text="Straal").grid(row=0)
e1 = Entry(master)
e1.grid(row=0, column=1)

area_label = Label(master, text="Oppervlakte")
area_label.grid(row=2)
calculation_label = Label(master, text="")    #new label
calculation_label.grid(row=2, column=1)

Button(master, text='Sluit', command=master.quit).grid(row=3, column=0, sticky=W, pady=4)
Button(master, text='Bereken oppervlakte', command=show_entry_fields).grid(row=3, column=1, sticky=W, pady=4)

mainloop()