Question

Why won't my python code open the GUI window when I run the module? Here's my...

Why won't my python code open the GUI window when I run the module?

Here's my code:

import tkinter

class MPGGui:
def ___init___ (self):

self.main_window = tkinter.Tk ()

self.top_frame = tkinter.Frame(self.main_window)
self.bottom_frame = tkinter.Frame(self.main_window)

self.gallons_label = tkinter.Label(self.top_frame, \
text = "Gallons: ")
self.gallons_entry = tkinter.Entry(self.top_frame, \
width=10)
self.gallons_label.pack(side='left')
self.gallons_entry.pack(side='left')

self.miles_label = tkinter.Label(self.top_frame, \
text = "Miles")
self.miles_entry = tkinter.Entry(self.top_frame, \
width=10)

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

self.calc_button = tkinter.Button(self.bottom_frame, \
text='Calculate' , \
command=self.convert)
self.quit_button = tkinter.Button(self.bottom_frame, \
text='Quit' , \
command=self.main_window.destroy)
self.calc_button.pack(side='left')
self.quit_button.pack(side='left')

self.top_frame.pack()
self.bottom_frame.pack()

tkinter.mainloop()

def convert(self):
gallon = float(self.gallons_entry.get())
mile = float(self.miles_entry.get())
ave = mile / gallon
tkinter.messagebox.showinfo('Results' , "The miles per gallon is" + str(ave) + "mpg")


mpg = MPGGui()

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

i have bolted the error:

Here is code:

import tkinter

from tkinter import messagebox

class MPGGui:

def __init__ (self):

self.main_window = tkinter.Tk ()

self.top_frame = tkinter.Frame(self.main_window)

self.bottom_frame = tkinter.Frame(self.main_window)

self.gallons_label = tkinter.Label(self.top_frame, \

text = "Gallons: ")

self.gallons_entry = tkinter.Entry(self.top_frame, \

width=10)

self.gallons_label.pack(side='left')

self.gallons_entry.pack(side='left')

self.miles_label = tkinter.Label(self.top_frame, \

text = "Miles")

self.miles_entry = tkinter.Entry(self.top_frame, \

width=10)

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

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

self.calc_button = tkinter.Button(self.bottom_frame, \

text='Calculate' , \

command=self.convert)

self.quit_button = tkinter.Button(self.bottom_frame, \

text='Quit' , \

command=self.main_window.destroy)

self.calc_button.pack(side='left')

self.quit_button.pack(side='left')

self.top_frame.pack()

self.bottom_frame.pack()

tkinter.mainloop()

def convert(self):

gallon = float(self.gallons_entry.get())

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

ave = mile / gallon

messagebox.showinfo('Results' , "The miles per gallon is " + str(ave) + " mpg")


mpg = MPGGui()

Output:

Add a comment
Know the answer?
Add Answer to:
Why won't my python code open the GUI window when I run the module? Here's my...
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
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