Question
I need the code below to calculate Distance Traveled through Speed & Time Driving Versus Miles Per Gallon. I am unable to get the formula
I need the code below to calculate Distance Traveled through Speed & Time Driving Versus Miles Per Gallon. I am unable to get the formula to work correctly. First code will be the example, and the second code will be what I have so far.
First Code:
import tkinter as tk
from tkinter import ttk
def click_calculateButton():
miles = float( milesText.get() )# get miles driven from the miles textfield
gas = float( gasText.get() )# get gas used from the gas textfield
mpg = miles / gas# do the math!
mpgText.set( round(mpg,1) )# display the output
def command_exitButton():
root.destroy()
# create root window
root = tk.Tk()
root.title("MPG Calculator")
root.geometry("280x150")# size of window
# create frame and add it to the root window
frame = ttk.Frame(root, padding="10 10 10 10")
frame.pack(fill=tk.BOTH, expand=True)
# create labels and textfields
ttk.Label(frame, text="Miles Driven:").grid(column=0, row=0, padx=5, pady=5, sticky=tk.E)# display label in grid
milesText = tk.StringVar()
ttk.Entry(frame, width=25, textvariable=milesText).grid(column=1, row=0)
ttk.Label(frame, text="Gas Used:").grid(column=0, row=1, padx=5, pady=5, sticky=tk.E)
gasText = tk.StringVar()
ttk.Entry(frame, width=25, textvariable=gasText).grid(column=1, row=1)
# createa button and add it to the window
ttk.Button(frame, text="Calculate", command=click_calculateButton).grid(column=0, row=2, padx=5, pady=5, sticky=tk.E)
ttk.Button(frame, text="Exit", command=command_exitButton).grid(column=1, row=2, padx=5, pady=5, sticky=tk.W)
ttk.Label(frame, text="MPG:").grid(column=0, row=3, padx=5, pady=5, sticky=tk.E)
mpgText = tk.StringVar()
mpgEntry = ttk.Entry(frame, width=25, textvariable=mpgText, state="readonly").grid(column=1, row=3) # notice readonly!
2nd Code:
import tkinter as tk
from tkinter import ttk
def click_calculateButton():
time = float( timeText.get() )
speed = float( speedText.get() )
distance = speed * time
distanceText.set( round(distance,1) )
def command_exitButton():
root.destroy()
root = tk.Tk()
root.title("Distance Calculator")
root.geometry("280x150")
frame = ttk.Frame(root, padding="10 10 10 10")
frame.pack(fill=tk.BOTH, expand=True)
ttk.Label(frame, text="Time Driving:").grid(column=0, row=0, padx=5, pady=5, sticky=tk.E)
timeText = tk.StringVar()
ttk.Entry(frame, width=25, textvariable=timeText).grid(column=1, row=0)
ttk.Label(frame, text="Speed:").grid(column=0, row=1, padx=5, pady=5, sticky=tk.E)
speedText = tk.StringVar()
ttk.Entry(frame, width=25, textvariable=speedText).grid(column=1, row=1)
ttk.Button(frame, text="Calculate", command=click_calculateButton).grid(column=0, row=2, padx=5, pady=5, sticky=tk.E)
ttk.Button(frame, text="Exit", command=command_exitButton).grid(column=1, row=2, padx=5, pady=5, sticky=tk.W)
ttk.Label(frame, text="Distance:").grid(column=0, row=3, padx=5, pady=5, sticky=tk.E)
distanceText = tk.StringVar()
distanceEntry = ttk.Entry(frame, width=25, textvariable=distanceText, state="readonly").grid(column=1, row=3)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started