Question

I am trying to make a basic area calculator that does this base*height=area

so far my code is this (at least the part you need to see)

#Labels and Boxes

def calculate():
    boxBase*boxHeight

buttonCalculate = Button(text = "Calculate area...", command = calculate)
buttonCalculate.grid(row =4, column =0)

myLabel3=Label(text="Area", fg="black")
myLabel3.grid(row =3, column =0, sticky='w')
boxSolution= Entry()
boxSolution.grid(row =3, column =1)

myLabel2=Label(text="Base", fg="black")
myLabel2.grid(row =0, column =0, sticky='w')
boxBase= Entry()
boxBase.grid(row =0, column =1)

myLabel=Label(text="Height", fg="black",)
myLabel.grid(row =1, column =0, sticky = "w")
boxHeight= Entry()
boxHeight.grid(row =1, column =1)

I want to make boxBase multiply by boxHeight and print to boxSolution when I press the buttonCalculate How can I do this?

Was it helpful?

Solution

Try this: Python 2.7

#Labels and Boxes
from Tkinter import *

WD = Tk()
WD.geometry('350x250+50+200')
WD.title("Area")

solution = DoubleVar()
base = DoubleVar()
height = DoubleVar()
def calculate():
    boxSolution.delete(0,END)
    boxSolution.insert(0,(base.get())*(height.get()))

buttonCalculate = Button(WD,text = "Calculate area...", command = calculate)
buttonCalculate.grid(row =4, column =0)

myLabel3=Label(WD,text="Area", fg="black")
myLabel3.grid(row =3, column =0, sticky='w')
boxSolution= Entry(WD,textvariable=solution)
boxSolution.grid(row =3, column =1)

myLabel2=Label(WD,text="Base", fg="black")
myLabel2.grid(row =0, column =0, sticky='w')
boxBase= Entry(WD,textvariable=base)
boxBase.grid(row =0, column =1)

myLabel=Label(WD,text="Height", fg="black",)
myLabel.grid(row =1, column =0, sticky = "w")
boxHeight= Entry(WD,textvariable=height)
boxHeight.grid(row =1, column =1)

WD.mainloop()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top