Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #44272 > unrolled thread

My gui

Started byDaniel Kersgaard <danielkersgaard@gmail.com>
First post2013-04-24 10:08 -0700
Last post2013-04-25 13:17 +1000
Articles 8 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  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

#44272 — My gui

FromDaniel Kersgaard <danielkersgaard@gmail.com>
Date2013-04-24 10:08 -0700
SubjectMy gui
Message-ID<09b96d6b-6db3-42a2-88d3-5fe16984fed1@googlegroups.com>
Today, being the last day of lectures at school, my instructor ran briefly through Tkninter and GUIs. I'd been looking forward to this particular lesson all semester, but when I got home and copied a sample program from my textbook verbatim, IDLE does nothing. No error, no nothing. Any ideas? Here  is the code from my program. I'm not sure if this is appropriate, but suggestions are helpful.

import tkinter
import tkinter.messagebox

class MyGui:
    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.prompt_label = tkinter.Label(self.top_frame, text = 'Enter a distance in Kilometers: ')
        self.kilo_entry = tkinter.Entry(self.top_frame, width = 10)

        self.prompt_label.pack(side = 'left')
        self.kilo_entry.pack(side = 'left')

        self.calc_button = tkinter.Button(self.bottom_frame, text = 'Convert', 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):
        kilo = float(self.kilo_entry.get())

        miles = kilo * 0.6214

        tkinter.messagebox.showinfo('Result', str(kilo) + ' kilometers is equal to ' + str(miles) + 'miles.')

poop = MyGui()

[toc] | [next] | [standalone]


#44278

FromDave Angel <davea@davea.name>
Date2013-04-24 13:46 -0400
Message-ID<mailman.1025.1366825599.3114.python-list@python.org>
In reply to#44272
On 04/24/2013 01:08 PM, Daniel Kersgaard wrote:
> Today, being the last day of lectures at school, my instructor ran briefly through Tkninter and GUIs. I'd been looking forward to this particular lesson all semester, but when I got home and copied a sample program from my textbook verbatim, IDLE does nothing. No error, no nothing. Any ideas? Here  is the code from my program. I'm not sure if this is appropriate, but suggestions are helpful.
>
> import tkinter
> import tkinter.messagebox
>
> class MyGui:
>      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.prompt_label = tkinter.Label(self.top_frame, text = 'Enter a distance in Kilometers: ')
>          self.kilo_entry = tkinter.Entry(self.top_frame, width = 10)
>
>          self.prompt_label.pack(side = 'left')
>          self.kilo_entry.pack(side = 'left')
>
>          self.calc_button = tkinter.Button(self.bottom_frame, text = 'Convert', 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):
>          kilo = float(self.kilo_entry.get())
>
>          miles = kilo * 0.6214
>
>          tkinter.messagebox.showinfo('Result', str(kilo) + ' kilometers is equal to ' + str(miles) + 'miles.')
>
> poop = MyGui()
>

I'm not an IDLE user, but I wouldn't be surprised if IDLE interferes 
with some GUI apps.   I'd run your code from the bash prompt.



-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#44279

FromChris “Kwpolska” Warrick <kwpolska@gmail.com>
Date2013-04-24 19:53 +0200
Message-ID<mailman.1026.1366826039.3114.python-list@python.org>
In reply to#44272
On Wed, Apr 24, 2013 at 7:08 PM, Daniel Kersgaard
<danielkersgaard@gmail.com> wrote:
> Today, being the last day of lectures at school, my instructor ran briefly through Tkninter and GUIs. I'd been looking forward to this particular lesson all semester, but when I got home and copied a sample program from my textbook verbatim, IDLE does nothing. No error, no nothing. Any ideas? Here  is the code from my program. I'm not sure if this is appropriate, but suggestions are helpful.
>
> import tkinter
> import tkinter.messagebox
>
> class MyGui:
>     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.prompt_label = tkinter.Label(self.top_frame, text = 'Enter a distance in Kilometers: ')
>         self.kilo_entry = tkinter.Entry(self.top_frame, width = 10)
>
>         self.prompt_label.pack(side = 'left')
>         self.kilo_entry.pack(side = 'left')
>
>         self.calc_button = tkinter.Button(self.bottom_frame, text = 'Convert', 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):
>         kilo = float(self.kilo_entry.get())
>
>         miles = kilo * 0.6214
>
>         tkinter.messagebox.showinfo('Result', str(kilo) + ' kilometers is equal to ' + str(miles) + 'miles.')
>
> poop = MyGui()
>
> --
> http://mail.python.org/mailman/listinfo/python-list

poop?  Seriously?  You aren’t serious about that copying, right?

Your code seems to be missing a lot of important stuff.  You don’t
inherit from tkinter.Frame.  Compare your program to the sample “Hello
world!” program:
http://docs.python.org/2/library/tkinter.html#a-simple-hello-world-program
— unfortunately, I am not a fan and I am not knowledgeable about
tkinter, but I can see a lot of stuff is missing.

Please try fixing it and running it _outside of IDLE_, which is also
built in Tk.  This may cause problems (but don’t worry, the code you
pasted doesn’t work anyways.)

--
Kwpolska <http://kwpolska.tk> | GPG KEY: 5EAAEA16
stop html mail                | always bottom-post
http://asciiribbon.org        | http://caliburn.nl/topposting.html

[toc] | [prev] | [next] | [standalone]


#44282

FromNed Batchelder <ned@nedbatchelder.com>
Date2013-04-24 14:08 -0400
Message-ID<mailman.1029.1366826929.3114.python-list@python.org>
In reply to#44272
On 4/24/2013 1:08 PM, Daniel Kersgaard wrote:
> Today, being the last day of lectures at school, my instructor ran briefly through Tkninter and GUIs. I'd been looking forward to this particular lesson all semester, but when I got home and copied a sample program from my textbook verbatim, IDLE does nothing. No error, no nothing. Any ideas? Here  is the code from my program. I'm not sure if this is appropriate, but suggestions are helpful.
>
> import tkinter
> import tkinter.messagebox
>
> class MyGui:
>      def_init_(self):

You need to define __init__, not _init_.  Python special methods are 
named with double underscore leading and trailing, and __x__ is 
pronounced "dunder-x".

--Ned.

[toc] | [prev] | [next] | [standalone]


#44288

FromArnaud Delobelle <arnodel@gmail.com>
Date2013-04-24 20:42 +0100
Message-ID<mailman.1031.1366832576.3114.python-list@python.org>
In reply to#44272
On 24 April 2013 18:53, Chris “Kwpolska” Warrick <kwpolska@gmail.com> wrote:
> On Wed, Apr 24, 2013 at 7:08 PM, Daniel Kersgaard
> <danielkersgaard@gmail.com> wrote:
>> Today, being the last day of lectures at school, my instructor ran briefly through Tkninter and GUIs. I'd been looking forward to this particular lesson all semester, but when I got home and copied a sample program from my textbook verbatim, IDLE does nothing. No error, no nothing. Any ideas? Here  is the code from my program. I'm not sure if this is appropriate, but suggestions are helpful.
>>
>> import tkinter
>> import tkinter.messagebox
>>
>> class MyGui:
>>     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.prompt_label = tkinter.Label(self.top_frame, text = 'Enter a distance in Kilometers: ')
>>         self.kilo_entry = tkinter.Entry(self.top_frame, width = 10)
>>
>>         self.prompt_label.pack(side = 'left')
>>         self.kilo_entry.pack(side = 'left')
>>
>>         self.calc_button = tkinter.Button(self.bottom_frame, text = 'Convert', 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):
>>         kilo = float(self.kilo_entry.get())
>>
>>         miles = kilo * 0.6214
>>
>>         tkinter.messagebox.showinfo('Result', str(kilo) + ' kilometers is equal to ' + str(miles) + 'miles.')
>>
>> poop = MyGui()
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
> poop?  Seriously?  You aren’t serious about that copying, right?
>
> Your code seems to be missing a lot of important stuff.  You don’t
> inherit from tkinter.Frame.  Compare your program to the sample “Hello
> world!” program:

His class is not a frame, it's just a container for the tkinter code.
It's a bit unusual but it looks correct to me (apart from the single
underscores in __init__() as spotted by Ned Batchelder).

-- 
Arnaud

[toc] | [prev] | [next] | [standalone]


#44290

FromNeil Cerutti <neilc@norwich.edu>
Date2013-04-24 20:26 +0000
Message-ID<atqtfvFa3niU1@mid.individual.net>
In reply to#44288
On 2013-04-24, Arnaud Delobelle <arnodel@gmail.com> wrote:
> His class is not a frame, it's just a container for the tkinter
> code. It's a bit unusual but it looks correct to me (apart from
> the single underscores in __init__() as spotted by Ned
> Batchelder).

I dunno if it makes any difference, but it's usual to call
mainloop of the root window, rather than tkinter.mainloop.

-- 
Neil Cerutti

[toc] | [prev] | [next] | [standalone]


#44313

FromTerry Jan Reedy <tjreedy@udel.edu>
Date2013-04-24 22:51 -0400
Message-ID<mailman.1046.1366858308.3114.python-list@python.org>
In reply to#44272
On 4/24/2013 1:53 PM, Chris “Kwpolska” Warrick wrote:

> Please try fixing it and running it _outside of IDLE_, which is also
> built in Tk

The default mode of Idle runs user code in a separate process. Editing 
tkinter code with Idle and running it (in the separate process) should 
be no problem.


[toc] | [prev] | [next] | [standalone]


#44316

FromChris Angelico <rosuav@gmail.com>
Date2013-04-25 13:17 +1000
Message-ID<mailman.1049.1366859849.3114.python-list@python.org>
In reply to#44272
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web