RE: Tkinter programming problem

Discussion in 'Python' started by Mark Daley, Aug 6, 2003.

  1. Mark Daley

    Mark Daley Guest

    I have problems with sys.exit() when I'm running under IDLE, since it seems
    to cause ALL mainloops to exit, killing IDLE. I can't really give a whole
    lot of detail on this, since I tend to use the command line to launch my
    GUIs, anyway.

    - Mark

    -----Original Message-----
    From:
    [mailto:p]On Behalf Of Eric Brunel
    Sent: Wednesday, August 06, 2003 7:35 AM
    To:
    Subject: Re: Tkinter programming problem


    Kevin Burrows wrote:
    > If I follow this thread correctly we are discussing how the exit from
    > Python/Tkinter programs. As it happens I have beed experimenting with
    > these environments and have struck the same problem.Examples from
    > "Python and Tkinter Programming" by John E Grayson. These examples
    > fire up OK but when the Quit button is hit the window freezes. If the
    > X in the window corner is hit first the application exits correctly.
    >
    > Several suggestions have been suggested. I have experimented with the
    > following program:
    >
    > #!/usr/local/bin/python
    > from Tkinter import * # Interface to Tk widgets
    >
    > class Application(Frame):
    > def __init__(self, master=None):
    > Frame.__init__(self, master)
    > self.grid()
    > self.createWidgets()
    >
    > def createWidgets(self):
    > self.quitButton = Button ( self, text="Quit",
    > command=self.quit )
    > self.quitButton.grid()
    >
    > def quit(self):
    > print sys.platform
    > print "Quitting"
    > self.quit #--DOES NOT QET RID OF WINDOW
    > # sys.exit(0) #--DOES NOT GET RID OF WINDOW
    > # self.master.destroy() #--GETS RID OF WINDOW


    This can't be the code you tested, since, again, there are no () after
    self.quit. Moreover, even if you had written self.quit(), it wouldn't have
    worked since this would call again the quit method on Application
    recursively,
    ending up in an exception. What you should have written is:

    Frame.quit(self)

    > app = Application() # Instantiate the application class
    > app.master.title("Sample application")
    > app.mainloop() # Wait for events
    >
    > I changed commented out 2 of the 3 methods in the "quit" function
    > under PythonWinIDE, IDLE and Windows using a .pyw extension.
    >
    > The results of the quit methods were:
    >
    > PythonWinIDE IDLE Windows using
    > a .pyw extension
    >
    > quit No No No
    >
    > sys.exit No No Yes
    >
    > destroy No No Yes
    >
    > Interstingly the application does not freeze under IDLE and can still
    > be closed with the windows X.
    >
    > I am using Windows 98 and Python 2.2


    There always have been issues with Tkinter on Windows 98: Tkinter
    applications
    often freeze on exit, but not always. This is likely to be a Windows issue,
    since it works everywhere else, even on more recent version of Windows with
    the
    same tcl/tk version.

    > So it looks like "destroy" is the method that works. It is
    > interesting that the example and test in the Tkinter.py module use the
    > "destroy" method but Grayson uses "quit" in his examples. Perhapse
    > the problem with "quit" is a MS Windows thing.


    You definetely should use quit: quitting Tkinter's main loop is its intended
    use; see
    http://www.pythonware.com/library/tkinter/introduction/x9374-event-processin
    g.htm

    > I would be interested to here if the same thing happens under Unix et.
    > al.


    I never had any problem with the quit method on Unix (Linux or Solaris) or
    on
    recent version of Windows (2k, XP). I'm however always running the Python
    scripts from the command line, never from an IDE. So that may be an issue
    too...
    --
    - Eric Brunel <> -
    PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

    --
    http://mail.python.org/mailman/listinfo/python-list
    Mark Daley, Aug 6, 2003
    #1
    1. Advertising

  2. "Mark Daley" <> wrote in message news:<>...
    > I have problems with sys.exit() when I'm running under IDLE, since it seems
    > to cause ALL mainloops to exit, killing IDLE. I can't really give a whole
    > lot of detail on this, since I tend to use the command line to launch my
    > GUIs, anyway.
    >

    Using tkinter seems to require a bit of lateral thinking at times (and
    I think I must have failed the test!!!). I suppose the difficulty lies
    in getting the right structure (generally true of OOP). Anyway, I did
    fix the problem and working code is given below. In effect, root does
    have to be destroyed.


    Andrew.


    # Demonstration TK interface Windows application
    # Runs ok from within IDLE
    #
    # A. Gregory, 4th August 2003
    #
    from Tkinter import *


    class CommonStuff: # to get common access to variables and functions
    def __init__(self, master):
    self.master=master
    self.frame = Frame(master)
    self.frame.bind("<Destroy>",self.CleanUp)

    def say_hi(self):
    print "Hello all"

    def myquit(self):
    self.master.destroy()

    def CleanUp(self, event):
    print "Cleaning up after Tk windows closed"


    class MyWidgets(Frame, CommonStuff):
    def __init__(self, CS):
    Frame.__init__(self, CS.frame)
    quitbutton = Button(self, text='Quit', fg='red',
    command=CS.myquit)
    quitbutton.pack(side=LEFT)
    self.hi_there = Button(self, text="Hello", command=CS.say_hi)
    self.hi_there.pack(side=LEFT)


    class Application:
    def __init__(self, CS):
    displayedwidget=MyWidgets(CS)
    displayedwidget.grid(row=0, column=0)
    CS.frame.grid(row=0, column=0)
    CS.frame.columnconfigure(0)
    CS.frame.update()



    root = Tk()
    CS = CommonStuff(root)
    mainWin = Application(CS)

    # If running from IDLE in Python 2.2 or earlier must use
    root.wait_window(CS.frame)

    # Otherwise can use
    # root.mainloop()
    Andrew Gregory, Aug 7, 2003
    #2
    1. Advertising

  3. Mark Daley

    klappnase Guest

    "Mark Daley" <> wrote in message news:<>...
    > I have problems with sys.exit() when I'm running under IDLE, since it seems
    > to cause ALL mainloops to exit, killing IDLE. I can't really give a whole
    > lot of detail on this, since I tend to use the command line to launch my
    > GUIs, anyway.
    >

    sys.exit()will stop (exit) the python interpreter. If you are running
    your program from within IDLE it will exit IDLE, too, because IDLE
    runs in the interpreter as well.
    If you run your program from the command line sys.exit() will exit the
    interpreter and stop the program, of course any instructions in the
    code after sys.exit() won't be executed.
    I think normally that is exactly what I want in a gui program - stop
    everything if the main window is closed.
    widget.destroy() will destroy "widget" and if "widget" is the main
    window of your application it will be destroyed - and all it's
    children with it.
    That's the way it should work if you are running from within the
    interpreter:
    your application is stopped but the interpreter won't be affected by
    this.
    widget.quit() exits the mainloop and therefore destroys the main
    window but it will not exit the interpreter, so there may be other
    processes in your code still be executed after the main window is
    closed. If all processes in the interpreter are finished the
    interpreter will exit anyway, so this will work as well as sys.exit().
    However, if you run your program from the interpreter this will not
    work, because you do not call a mainloop there.

    Cheers

    Michael
    klappnase, Aug 7, 2003
    #3
  4. Mark Daley

    klappnase Guest

    Alex Martelli <> wrote in message news:<dmtYa.28539$>...



    > > IDLE 1.0
    > > >>> import sys
    > > >>> sys.exit(23)

    > >
    > > Traceback (most recent call last):
    > > File "<pyshell#1>", line 1, in -toplevel-
    > > sys.exit(23)
    > > SystemExit: 23
    > > >>>

    >
    > As you see, with the IDLE 1.0 which comes with Python 2.3, the
    > exception raised by sys.exit is captured and displayed as such
    > [and similarly if sys.exit is called from a module run within
    > the IDLE session, rather than directly at the IDLE prompt].
    >

    Oh, that is interesting, my IDLE (python-2.2.2) exits on this.
    Does this happen with sys.exit(0) too?



    > Often one may want to do some kind of clean-up at that point, but you
    > may achieve that with a try/finally around the mainloop call (putting
    > the cleanup code in the finally clause, of course).
    >

    Seems like you mean something like:

    def quitcmd():
    mainwindow.quit()
    do_this()
    do_that()

    If this is what you meant, what is the problem with:

    def exitcmd():
    do_this()
    do_that()
    sys.exit(0)

    I do not think that there is much difference in the behavior of these.
    May be if I launched the application from a menu and the do_that()
    function does something really stupid that causes an endless loop, I
    think with quitcmd() it might occur that the window is closed and I
    think "Fine, the application is shut down" but the buggy do_that() is
    still running in the background. With exitcmd() this won't happen, the
    window will not close before do_that() comes to an end, so I will see
    that there is something wrong.

    Best regards

    Michael
    klappnase, Aug 8, 2003
    #4
    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. Andrew Gregory

    Tkinter programming problem

    Andrew Gregory, Aug 1, 2003, in forum: Python
    Replies:
    5
    Views:
    381
    Eric Brunel
    Aug 7, 2003
  2. Jeff Epler
    Replies:
    0
    Views:
    504
    Jeff Epler
    Aug 20, 2004
  3. Jeff Epler
    Replies:
    0
    Views:
    450
    Jeff Epler
    Aug 23, 2004
  4. Pierre Dagenais
    Replies:
    0
    Views:
    308
    Pierre Dagenais
    Aug 3, 2008
  5. Hidekazu IWAKI
    Replies:
    1
    Views:
    376
    Peter Otten
    Dec 14, 2009
Loading...

Share This Page