Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: Tkinter programming problem

Reply
Thread Tools

RE: Tkinter programming problem

 
 
Mark Daley
Guest
Posts: n/a
 
      08-06-2003
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: http://www.velocityreviews.com/forums/(E-Mail Removed)
[mailto(E-Mail Removed)]On Behalf Of Eric Brunel
Sent: Wednesday, August 06, 2003 7:35 AM
To: (E-Mail Removed)
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/tk...vent-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 <(E-Mail Removed)> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

--
http://mail.python.org/mailman/listinfo/python-list



 
Reply With Quote
 
 
 
 
Andrew Gregory
Guest
Posts: n/a
 
      08-07-2003
"Mark Daley" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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()
 
Reply With Quote
 
 
 
 
klappnase
Guest
Posts: n/a
 
      08-07-2003
"Mark Daley" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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
 
Reply With Quote
 
klappnase
Guest
Posts: n/a
 
      08-08-2003
Alex Martelli <(E-Mail Removed)> wrote in message news:<dmtYa.28539$(E-Mail Removed)>...



> > 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the differences between tkinter in windows and Tkinter in theother platform? Hidekazu IWAKI Python 1 12-14-2009 03:44 PM
from Tkinter import *,win = Tk() "from Tkinter import *" Pierre Dagenais Python 0 08-03-2008 10:33 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-23-2004 02:31 PM
Re: [Tkinter-discuss] Please help -- Tkinter Scale widget withDoubleVar is acting weird Jeff Epler Python 0 08-20-2004 12:07 PM
Tkinter programming problem Andrew Gregory Python 5 08-07-2003 09:15 AM



Advertisments