Python中的GUI问题

时间:2014-12-17 23:15:49

标签: python

我正在制作一个GUI,我必须计算每加仑汽车的里程数。我一直收到语法错误,但我不确定我在做错的是我的代码:我编辑了它以反映一些更正

class GasMileage:
 def__init__(self)
        #Create main window
        self.main_window = tkinter.Tk()

        #Create 4 frames
        self.gallons_frame = tkinter.frame(self.main_windown)
        self.miles_frame = tkinter.frame(self.main_windown)
        self.button_frame = tkinter.frame(self.main_windown)
        self.mpg_frame = tkinter.frame(self.main_window)

        #Create and pack widgets Gallons and Miles

        self.gallons_label = tkinter.Label(self.gallons_frame, \
                                           text "Enter # of Gallons Used")
        self.gallons_entry = tkinter.Entry(self.gallons_frame, \
                                           width = 10)

        self.miles_label.pack(side='left')
        self.miles_entry.pack(side='left')

        self.miles_label = tkinter.Label(self.miles_frame, \
                                           text="Enter miles traveled")
        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='MPG=')
        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')

        #Create and pack button Widgets

        self.calc_button = tkinter.Button(self.button_frame, \
                                          text='Calculate MPG', \
                                          command=self.calc_gallons)

        self.quit_button = tkinter.Button(self.button_frame, \
                                          text='Exit', \
                                          command=self.main_window.destroy)
        self.calc_button.pack(side='left')
        self.quit_button.pack(side='left')

        #Pack the frames

        self.gallons_frame.pack()
        self.miles_frame.pack()
        self.buttons_frame.pack()
        self.mpg_frame.pack()

        tkinter.mainloop()

        #Calculate Miles per Gallon

        def calc_gallons(self):

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

            #Calculate Miles per Gallon

            self.mpg = self.miles / self.gallons

        gas_miles = GasMileage()

1 个答案:

答案 0 :(得分:0)

我修改了你的代码,但它充满了错误和不一致。现在至少它编译并执行:

import tkinter



class GasMileage:

    def __init__(self):
        #Create main 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.button_frame = tkinter.Frame(self.main_window)
        self.mpg_frame = tkinter.Frame(self.main_window)

        #Create and pack widgets Gallons and Miles

        self.gallons_label = tkinter.Label(self.gallons_frame, 
                                           text = "Enter # of Gallons Used")
        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 miles traveled")
        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='MPG=')
        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')

        #Create and pack button Widgets

        self.calc_button = tkinter.Button(self.button_frame, 
                                          text='Calculate MPG', 
                                          command=self.calc_gallons)

        self.quit_button = tkinter.Button(self.button_frame, 
                                          text='Exit',
                                          command=self.main_window.destroy)
        self.calc_button.pack(side='left')
        self.quit_button.pack(side='left')

        #Pack the frames

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

        tkinter.mainloop()

    #Calculate Miles per Gallon


![enter image description here][1]

    def calc_gallons(self):

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

        print(self.gallons, self.miles)

        #Calculate Miles per Gallon

        self.mpg = self.miles / self.gallons
        self.result_label['text'] = "'MPG = '{:0.2f}".format(self.mpg)

gas_miles = GasMileage()

截图:

enter image description here