My gui

Discussion in 'Python' started by Daniel Kersgaard, Apr 24, 2013.

  1. 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 = 'Entera 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()
     
    Daniel Kersgaard, Apr 24, 2013
    #1
    1. Advertising

  2. Daniel Kersgaard

    Dave Angel Guest

    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
     
    Dave Angel, Apr 24, 2013
    #2
    1. Advertising

  3. On Wed, Apr 24, 2013 at 7: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? Hereis 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 isequal 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
     
    Chris “Kwpolska†Warrick, Apr 24, 2013
    #3
  4. 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.
     
    Ned Batchelder, Apr 24, 2013
    #4
  5. On 24 April 2013 18:53, Chris “Kwpolska†Warrick <> wrote:
    > On Wed, Apr 24, 2013 at 7: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 mytextbook 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
     
    Arnaud Delobelle, Apr 24, 2013
    #5
  6. Daniel Kersgaard

    Neil Cerutti Guest

    On 2013-04-24, Arnaud Delobelle <> 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
     
    Neil Cerutti, Apr 24, 2013
    #6
  7. 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.
     
    Terry Jan Reedy, Apr 25, 2013
    #7
  8. 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
     
    Chris Angelico, Apr 25, 2013
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. paul.foreman

    GUI - GUI value passing

    paul.foreman, Oct 22, 2004, in forum: Java
    Replies:
    5
    Views:
    753
    Michael Rauscher
    Oct 25, 2004
  2. ulysses
    Replies:
    4
    Views:
    768
    Werner Schiendl
    Oct 22, 2003
  3. Andrew Lapidas

    PyGTK GUI update without signals from GUI

    Andrew Lapidas, Apr 13, 2008, in forum: Python
    Replies:
    0
    Views:
    417
    Andrew Lapidas
    Apr 13, 2008
  4. Stefan Ram
    Replies:
    3
    Views:
    496
    Arne Vajhøj
    Nov 20, 2011
  5. Hal Fulton

    GUI and non-GUI data

    Hal Fulton, Aug 5, 2004, in forum: Ruby
    Replies:
    1
    Views:
    144
    Austin Ziegler
    Aug 5, 2004
Loading...

Share This Page