Okay so my code works, but I need a few more requirements for it and I can't seem to get them right even though they should be pretty simple. I just need the window to display an error message when the values entered are not numerical and when the values entered are less than 0. For example when # of gallons used is -12 I need it to display an error message below the buttons, and same when letters are entered instead of numbers. I also need the calculated mpg to display to 2 decimal places and keep getting errors for that as well. Here is my code, thanks for any help.

import tkinter
class MPG: 
    def __init__(self):
    #create window
    self.main_window = tkinter.Tk()

    #create 4 frames
    self.gallons_frame = tkinter.Frame(self.main_window)
    self.miles_frame = tkinter.Frame(self.main_window)
    self.mpg_frame = tkinter.Frame(self.main_window)
    self.button_frame = tkinter.Frame(self.main_window)

    self.gallons_label = tkinter.Label(self.gallons_frame, \
                                    text= "Enter the number of gallons:")
    self.gallons_entry = tkinter.Entry(self.gallons_frame, \
                                        width=10)
    self.gallons_label.pack(side="left")
    self.gallons_entry.pack(side="left")
    self.miles_label = tkinter.Label(self.miles_frame, \
                                    text= "Enter the number of miles:")
    self.miles_entry = tkinter.Entry(self.miles_frame, \
                                        width=10)
    self.miles_label.pack(side="left")
    self.miles_entry.pack(side="left")
    self.result_label = tkinter.Label(self.mpg_frame, \
                                    text="Miles Per Gallon")
    self.mpg = tkinter.StringVar()
    self.mpg_label = tkinter.Label(self.mpg_frame, \
                                    text variable=self.mpg)
    self.result_label.pack(side="left")
    self.mpg_label.pack(side="left")
    self.calc_button = tkinter.Button(self.button_frame, \
                                        text="Calculate MPG" , \
                                        command=self.calc_mpg)
    self.quit_button = tkinter.Button(self.button_frame, \
                                        text="Quit", \
                                        command=self.main_window.destroy)
    self.calc_button.pack(side="left")
    self.quit_button.pack(side="left")

    self.gallons_frame.pack()
    self.miles_frame.pack()
    self.mpg_frame.pack()
    self.button_frame.pack()
#the calc_mpg is the callback function for the calc_button
def calc_mpg(self):
#Get miles and gallons and store them in variables
    self.gallons = float(self.gallons_entry.get())

    self.miles = float(self.miles_entry.get())

#calculate the MPG
    self.miles_per_gallon = (self.miles / self.gallons)

#update mpg_label
    self.mpg.set(str(round(self.miles_per_gallon)))

carmpg = MPG()

Please post a working code, to help others. Try to copy-paste your own posted code into a .py file and see, that it does not work. There is a syntax error, not just intendation problems. The mainloop call is missing, too.

I have corrected the code, and hopefully solved your problem:

import tkinter

class MPG: 
    def __init__(self):
    #create window
        self.main_window = tkinter.Tk()

        #create 4 frames
        self.gallons_frame = tkinter.Frame(self.main_window)
        self.miles_frame = tkinter.Frame(self.main_window)
        self.mpg_frame = tkinter.Frame(self.main_window)
        self.button_frame = tkinter.Frame(self.main_window)
        self.error_frame = tkinter.Frame(self.main_window)

        self.gallons_label = tkinter.Label(self.gallons_frame, \
                                        text= "Enter the number of gallons:")
        self.gallons_entry = tkinter.Entry(self.gallons_frame, \
                                            width=10)
        self.gallons_label.pack(side="left")
        self.gallons_entry.pack(side="left")
        self.miles_label = tkinter.Label(self.miles_frame, \
                                        text= "Enter the number of miles:")
        self.miles_entry = tkinter.Entry(self.miles_frame, \
                                            width=10)
        self.miles_label.pack(side="left")
        self.miles_entry.pack(side="left")
        self.result_label = tkinter.Label(self.mpg_frame, \
                                        text="Miles Per Gallon")
        self.mpg = tkinter.StringVar()
        self.mpg_label = tkinter.Label(self.mpg_frame, \
                                        textvariable=self.mpg)
        self.result_label.pack(side="left")
        self.mpg_label.pack(side="left")
        self.calc_button = tkinter.Button(self.button_frame, \
                                            text="Calculate MPG" , \
                                            command=self.calc_mpg)
        self.quit_button = tkinter.Button(self.button_frame, \
                                            text="Quit", \
                                            command=self.main_window.destroy)
        self.calc_button.pack(side="left")
        self.quit_button.pack(side="left")

        self.error=tkinter.StringVar()
        self.error_label=tkinter.Label(self.error_frame, textvariable=self.error)
        self.error_label.pack(side="left")

        self.gallons_frame.pack()
        self.miles_frame.pack()
        self.mpg_frame.pack()
        self.button_frame.pack()
        self.error_frame.pack()

    #check gallons and miles numbers. If it can be converted, then return it, if not set the errorlabel.
    def checknumber(self, stringvalue, numbername):
        try:
            to_return=float(stringvalue)
        except:
            self.error.set(numbername+" is not a number")
            return None
        if to_return<0:
            self.error.set(numbername+" is negative")
            return None
        return to_return

    #the calc_mpg is the callback function for the calc_button    
    def calc_mpg(self):
        self.error.set("")
        self.mpg.set("")

        gallons = self.checknumber(self.gallons_entry.get(),"Gallons")
        if gallons is None: return

        miles = self.checknumber(self.miles_entry.get(), "Miles")
        if miles is None: return

    #calculate the MPG
        miles_per_gallon = round((miles / gallons),2)

    #update mpg_label
        self.mpg.set(str(miles_per_gallon))


carmpg = MPG()

carmpg.main_window.mainloop()
Comments
Sorry for the errors, I was in a rush when typing it. Thank you, this did fix my problem. Much appreciated.
This question has already been answered. Start a new discussion instead.