Question

I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so.

Miles Per Gallon Calculator Write a GUI program that calculates a cars gas mileage. The programs window should have Entry w

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#code

from tkinter import *

#method to calculate MPG and display it
def calculate():
    try:
        #fetching values from StringVar objects (see below) and converting
        #values to float
       
miles=float(milesTxt.get())
        gallons=float(gallonsTxt.get())
        #finding mpg
       
mpg=miles/gallons
        #displaying it on mpgTxt StringVar, which will set the linked Label
       
mpgTxt.set('{:.2f} miles/gallon'.format(mpg))
    except:
        #error occurred while parsing
      
mpgTxt.set('Invalid input!')

#creating root window
root=Tk()

#creating three StringVar objects. that can be used to get and set
#values in Label and Entry objects

gallonsTxt=StringVar()
milesTxt=StringVar()
mpgTxt=StringVar()

#creating Labels and Entrys as needed, aligning them using grid layout
Label(root,text='Enter number of gallons: ').grid(row=0,column=0)
#defining Entry for gallons, and mapping to the gallonsTxt StringVar
Entry(root,textvariable=gallonsTxt).grid(row=0,column=1)

Label(root,text='Enter number of miles: ').grid(row=1,column=0)
Entry(root,textvariable=milesTxt).grid(row=1,column=1)

#defining the button to call calculate method on click
Button(root,text='Calculate MPG',command=calculate).grid(row=2,column=0)
Label(root,textvariable=mpgTxt).grid(row=3,column=0)

#staying until user quits
root.mainloop()

#output

tk Enter number of gallons: 20 Enter number of miles: 250 Calculate MPG 12.50 miles/gallon

Add a comment
Know the answer?
Add Answer to:
I need trying to figure out how I can make create a code in Python with this exercise for I am having trouble doing so. Miles Per Gallon Calculator Write a GUI program that calculates a car's gas...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi, Hi, I need someone to write a program in Phython 3.6, and please I will...

    Hi, Hi, I need someone to write a program in Phython 3.6, and please I will appreciate if the code is error free and indented correctly. The output would be preferable. thanks Write a GUI program that translates the Latin words to English. The window should have three buttons, one for each Latin word. When the user clicks a button, the program displays the English translation in a label. 3. Miles Per Gallon Calculator Write a GUI program that calculates...

  • create an application in VISUAL BASIC that calculates a cars gas mileage. The formula for calculating...

    create an application in VISUAL BASIC that calculates a cars gas mileage. The formula for calculating the miles that a car can travel per gallon of gas is MPG = Miles/Gallon In the formula MPG is miles-per-gallon, miles is the number of miles that can be driven on a full tank of gas, and gallons is the number of gallons that the tank holds. The applications form should have TextBox controls that let the user enter the number of gallons...

  • 7. Miles-per-Gallon 6pts A car’s miles-per-gallon (MPG) can be calculated with the following formula: MPG =...

    7. Miles-per-Gallon 6pts A car’s miles-per-gallon (MPG) can be calculated with the following formula: MPG = Miles driven / Gallons of gas used Design a program that asks the user for the number of miles driven and the gallons of gas used. It should calculate the car’s miles-per-gallon and display the result on the screen.Draw a flowchart in algorithmic language

  • Classwork 2-2: Input & Formatting A car's miles-per-gallon (MPG) can be calculated using the formula: MPG...

    Classwork 2-2: Input & Formatting A car's miles-per-gallon (MPG) can be calculated using the formula: MPG - miles driven/gallons of gas used Write a program that asks the user for the car's make, number of miles driven and the gallons of gas used. It should calculate the car's MPG and displays the result rounded to 2 decimal places. Example "Your Honda's MPG is 28.56" On Blackboard, submit a screenshot of successfully running and testing your code on HackerRank.com: www.hackerrank.com/csc124-chapter-2-classwork (2nd...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT