Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'explicit': 0.07; 'mentioned,': 0.07; 'modified': 0.07; 'tkinter': 0.07; 'variables': 0.07; '__init__': 0.09; 'constructor': 0.09; 'oh,': 0.09; 'width': 0.09; 'windows,': 0.09; 'python': 0.11; 'def': 0.12; 'translation': 0.12; 'windows': 0.15; '10);': 0.16; 'bit.': 0.16; 'class;': 0.16; 'closed,': 0.16; 'done),': 0.16; 'flush': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'optional': 0.16; 'ought': 0.16; 'redundant': 0.16; 'renamed': 0.16; 'sorts': 0.16; 'subject:gui': 0.16; 'top-level': 0.16; '(you': 0.16; 'java,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'slightly': 0.19; 'thu,': 0.19; 'command': 0.22; 'import': 0.22; "i've": 0.25; 'changes,': 0.26; 'daniel': 0.26; 'possibly': 0.26; 'references': 0.26; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'function': 0.29; 'fixed': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '(maybe': 0.31; '25,': 0.31; 'object.': 0.31; 'skip:q 20': 0.31; 'class': 0.32; 'skip:m 30': 0.32; 'text': 0.33; 'style': 0.33; 'skip:_ 10': 0.34; 'maybe': 0.34; 'could': 0.34; 'display': 0.35; 'created': 0.35; 'convert': 0.35; 'equal': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'version': 0.36; 'really': 0.36; 'oracle': 0.36; 'returning': 0.36; 'version:': 0.36; 'changing': 0.37; 'two': 0.37; 'button': 0.38; 'handle': 0.38; 'to:addr :python-list': 0.38; 'rather': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'changed': 0.39; 'either': 0.39; 'skip:p 20': 0.39; 'remove': 0.60; 'skip:t 30': 0.61; "you're": 0.61; "you'll": 0.62; 'making': 0.63; 'pick': 0.64; 'distance': 0.65; 'here': 0.66; 'direct': 0.67; 'hang': 0.67; 'cut': 0.74; 'intelligent': 0.74; 'abandon': 0.84; 'complexity': 0.84; 'different.': 0.84; 'miles)': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=iywo96Q6aNIwbsUh+k+QULhBxLWyg+Uf3xKfAgIjfbk=; b=hGg9167lmBYxeENBkjEo9Q7iooV10OUjAsbSkCQqMQL/mheDOKgKc9Oq+oHx2lW522 xSAlgvSpvyChanqQ1hn7LT95bg7eIEA/G1hXJJSa0VgO7yj0I8+PoIXqUoHjhJ2dlEbK z18ncsMpisXBpAMT23p+q6HDvBezO89hauV6KCzFoCxCypLtxAMW/AnS8Jc6r8ZxuH58 mbKKmlIO3hilehRwONQmnnO0o8P3+xPZKagqZP8Wa9eGLHLQnRfCLVQ955cIukTiIjv2 pHCwvspt+4zHN4RQ4eV9ZmQ/gYJZIpRV1AJEADTNLt6exA0Vke+LZWJYOlZufdHCpPgG ajFQ== MIME-Version: 1.0 X-Received: by 10.220.88.145 with SMTP id a17mr26135052vcm.66.1366859846819; Wed, 24 Apr 2013 20:17:26 -0700 (PDT) In-Reply-To: <09b96d6b-6db3-42a2-88d3-5fe16984fed1@googlegroups.com> References: <09b96d6b-6db3-42a2-88d3-5fe16984fed1@googlegroups.com> Date: Thu, 25 Apr 2013 13:17:26 +1000 Subject: Re: My gui From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 109 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1366859849 news.xs4all.nl 15935 [2001:888:2000:d::a6]:36471 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:44316 On Thu, Apr 25, 2013 at 3:08 AM, Daniel Kersgaard 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