On Thu, Apr 25, 2013 at 3:08 AM, Daniel Kersgaard <danielkersga...@gmail.com> wrote: > import tkinter > import tkinter.messagebox > > class MyGui: > def _init_(self): > ... all code here > > poop = MyGui()
As already mentioned, changing that to __init__ makes everything work (I just tested in Python 3.3 on Windows). But since Python is not Java, there's no reason to bury all this code in a class; just move all that code flush left and abandon the explicit instantiation. The constructor doesn't return until the window's been closed, so there's really no point in returning an object. (Maybe if you remove the mainloop() call, you could possibly make use of two of these sorts of windows, but YAGNI - don't bother with the complexity required to handle multiple simultaneous windows until you have a use-case.) So here's a direct translation to top-level code: import tkinter import tkinter.messagebox def convert(): kilo = float(kilo_entry.get()) miles = kilo * 0.6214 tkinter.messagebox.showinfo('Result', str(kilo) + ' kilometers is equal to ' + str(miles) + 'miles.') main_window = tkinter.Tk() top_frame = tkinter.Frame(main_window) bottom_frame = tkinter.Frame(main_window) prompt_label = tkinter.Label(top_frame, text = 'Enter a distance in Kilometers: ') kilo_entry = tkinter.Entry(top_frame, width = 10) prompt_label.pack(side = 'left') kilo_entry.pack(side = 'left') calc_button = tkinter.Button(bottom_frame, text = 'Convert', command = convert) quit_button = tkinter.Button(bottom_frame, text = 'Quit', command = main_window.destroy) calc_button.pack(side = 'left') quit_button.pack(side = 'left') top_frame.pack() bottom_frame.pack() tkinter.mainloop() -- cut -- (You may want to bury the code inside a main(), but that's optional too.) And here's a slightly modified version: import tkinter import tkinter.messagebox main_window = tkinter.Tk() top_frame = tkinter.Frame(main_window) bottom_frame = tkinter.Frame(main_window) tkinter.Label(top_frame, text = 'Enter a distance in Kilometers: ').pack(side = 'left') km_entry = tkinter.Entry(top_frame, width = 10); km_entry.pack(side = 'left') def convert(): km = float(km_entry.get()) miles = km * 0.6214 tkinter.messagebox.showinfo('Result', '%.2f kilometers is equal to %.2f miles.' % (km, miles) ) tkinter.Button(bottom_frame, text = 'Convert', command = convert).pack(side = 'left') tkinter.Button(bottom_frame, text = 'Quit', command = main_window.destroy).pack(side = 'left') top_frame.pack() bottom_frame.pack() tkinter.mainloop() -- cut -- I've removed some redundant variables (you don't need to hang onto references to your labels,just pack 'em in and be done), and reordered the code a bit to put the Convert button's function right near where the Convert button is created (take your pick - do you prefer that, or to have all the other functions up top? Both are viable); I also changed your message box to use percent-formatting with fixed decimals rather than str(), to tidy up the display a bit. Oh, and renamed "kilo" to "km", because to me "kilo" means "kilogram". :) This version of the code isn't necessarily better in every way, but it's different. Like the Oracle in The Matrix, I want you to make up your own mind as to what you ought to do; maybe you'll like some of my changes, maybe you won't, but either way you're making an intelligent decision about code style :) ChrisA -- http://mail.python.org/mailman/listinfo/python-list