Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #44316
| References | <09b96d6b-6db3-42a2-88d3-5fe16984fed1@googlegroups.com> |
|---|---|
| Date | 2013-04-25 13:17 +1000 |
| Subject | Re: My gui |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1049.1366859849.3114.python-list@python.org> (permalink) |
On Thu, Apr 25, 2013 at 3:08 AM, Daniel Kersgaard
<danielkersgaard@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
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
My gui Daniel Kersgaard <danielkersgaard@gmail.com> - 2013-04-24 10:08 -0700
Re: My gui Dave Angel <davea@davea.name> - 2013-04-24 13:46 -0400
Re: My gui Chris “Kwpolska” Warrick <kwpolska@gmail.com> - 2013-04-24 19:53 +0200
Re: My gui Ned Batchelder <ned@nedbatchelder.com> - 2013-04-24 14:08 -0400
Re: My gui Arnaud Delobelle <arnodel@gmail.com> - 2013-04-24 20:42 +0100
Re: My gui Neil Cerutti <neilc@norwich.edu> - 2013-04-24 20:26 +0000
Re: My gui Terry Jan Reedy <tjreedy@udel.edu> - 2013-04-24 22:51 -0400
Re: My gui Chris Angelico <rosuav@gmail.com> - 2013-04-25 13:17 +1000
csiph-web