emin
emin

Reputation: 53

Python 2.7; How to clear an entry in the GUI

I have an example code, here just for BMI index. I would like to clear the input and output fields in the GUI. It seems that i can clear the entries, but the BMI calculation is not being removed (row 79 does not seem to have an effect) (# self.text.delete(0, 'end'))

Thanks Emin

import math

from Tkinter import *

class Application(Frame):
    """A GUI application with three buttons"""

    def __init__(self, master):
        """Initialize the Frame"""
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()
        self.title = Label(self, text = "BMI index calculation")
        self.title.grid(row = 0, column = 0, columnspan = 2 , sticky =W)

    def create_widgets(self):
        """Create button, text and entry widgets"""
        self.name = Label(self, text = "What is your name?")
        self.name.grid(row = 1, column = 0, columnspan = 2 , sticky =W)
        self.name_io = Entry(self)
        self.name_io.grid(row = 1, column =2, sticky = W)

        self.age = Label(self, text = "How old are you?")
        self.age.grid(row = 2, column = 0, columnspan = 2 , sticky =W)
        self.age_io = Entry(self)
        self.age_io.grid(row = 2, column =2, sticky = W)

        self.height = Label(self, text = "How tall are you?")
        self.height.grid(row = 3, column = 0, columnspan = 2 , sticky =W)
        self.height_io = Entry(self)
        self.height_io.grid(row = 3, column =2, sticky = W)

        self.weight = Label(self, text = "How much do you weigh in kg?")
        self.weight.grid(row = 4, column = 0, columnspan = 2 , sticky =W)
        self.weight_io = Entry(self)
        self.weight_io.grid(row = 4, column =2, sticky = W)

        self.submit_button = Button(self, text = "Calculate", command = self.reveal)
        self.submit_button.grid(row = 5, column = 0, sticky = W)

        self.text = Text(self, width = 40, height = 5, wrap = WORD)
        self.text.grid(row = 6, column = 0, columnspan = 3, sticky = W)

        self.clear_button = Button(self, text = "Clear", command = self.clear_text)
        self.clear_button.grid(row = 7, column = 0, sticky = W)

    def reveal(self):
        """Display message based on the password typed in"""
        content_name = self.name_io.get()
        content_age = float(self.age_io.get())
        content_height = float(self.height_io.get())
        content_weight = float(self.weight_io.get())

        BMI = round((content_weight/(content_height/100)**2.),1)

        underBMI = 18.5
        NormalBMI = 24.9
        OverweightBMI = 29.9
        ObesityBMI = 30

        if BMI <= underBMI:
            message = content_name + ", " + "your BMI index is" + " " + str(BMI) + ", " + "you are underweight, so you need to eat!"
        elif (BMI > underBMI) and (BMI <= NormalBMI):
            message = content_name + ", " + "your BMI index is" + " " + str(BMI) + ", " + "your BMI is Normal"
        elif (BMI > NormalBMI) and (BMI <= OverweightBMI):
            message = content_name + ", " + "your BMI index is" + " " + str(BMI) + ", " + "you are Overweight - need to exercise!"
        elif (BMI > OverweightBMI):
            message = content_name + ", " + "your BMI index is" + " " + str(BMI) + ", " + "you are in Obesity"

        self.text.insert(0.0, message)



    def clear_text(self):
            self.name_io.delete(0, 'end')
            self.age_io.delete(0, 'end')
            self.height_io.delete(0, 'end')
            self.weight_io.delete(0, 'end')
#           self.text.delete(0, 'end')


root = Tk()
root.title("BMI Index")
root.geometry("600x350")
app = Application(root)
root.mainloop ()

Upvotes: 1

Views: 1381

Answers (2)

hadi xd
hadi xd

Reputation: 63

Simply

your_entry.delete(0,END)

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 386342

The problem is that you're giving an index that is 0.0. Text widget indexes are a string of the form line.column but you're giving it a floating point number.

The proper index for the first character is the string "1.0".

self.text.delete("1.0", 'end')

Upvotes: 1

Related Questions