Quit-command not quiting

K

K Viltersten

I entered the code from tkinter.pdf, section
2 but for reason, the application doesn't
close as i press the quit-button.

The wondow itself vanishes if i click the
cross in the upper-right corner but pressing
the quit-button only makes it "pressed".
Then, the program freezes.

This is the code.

from Tkinter import *
class Demo (Frame):
def __init__ (self, master = None):
Frame.__init__ (self, master)
self.grid ()
self.doLayout ()
def doLayout (self):
self.quitButton = Button (
self,
text = "Quit",
command = self.quit)
self.quitButton.grid ()

d = Demo ()
d.master.title ("the coolest demo ever")
d.mainloop ()
 
G

Gabriel Genellina

I entered the code from tkinter.pdf, section
2 but for reason, the application doesn't
close as i press the quit-button.

The wondow itself vanishes if i click the
cross in the upper-right corner but pressing
the quit-button only makes it "pressed".
Then, the program freezes.

How did you run it? From inside IDLE? IDLE itself is written using Tk, and
I think that your mainloop interferes with the one inside it.
If you run your program from the command line it should work fine.
from Tkinter import *
class Demo (Frame):
def __init__ (self, master = None):
Frame.__init__ (self, master)
self.grid ()
self.doLayout ()
def doLayout (self):
self.quitButton = Button (
self,
text = "Quit",
command = self.quit)
self.quitButton.grid ()

d = Demo ()
d.master.title ("the coolest demo ever")
d.mainloop ()

There is only one thing I hate more than spaces after a parens: spaces
before it :)
Please read PEP8, about the suggested style for writting Python code.
http://www.python.org/dev/peps/pep-0008/
 
K

K Viltersten

The window itself vanishes if i click the
How did you run it? From inside IDLE? IDLE itself is written
using Tk, and I think that your mainloop interferes with the
one inside it. If you run your program from the command line
it should work fine.

I press F5 while in the editor window. Is there a way to run the
program without going to the console window?

Perhaps i'm just making things unneccesarily complicated
and Python IS supposed to be run from console window?
There is only one thing I hate more than spaces after a
parens: spaces before it :)
Please read PEP8, about the suggested style for writting
Python code. http://www.python.org/dev/peps/pep-0008/

I've got no issues one way or the other. Most likely i'll forget
from time to time but other than that, i'll try to keep it in mind.
 
G

Gabriel Genellina

I press F5 while in the editor window. Is there a way to run the
program without going to the console window?
Perhaps i'm just making things unneccesarily complicated
and Python IS supposed to be run from console window?

No, use IDLE if you prefer, or any other editor/IDE. But in your case
there is an unfortunate coupling between IDLE and your script. Fix: change
the quit method as suggested in this thread:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/92bee52a3e2a325e/

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

This is OK if used on the top level widget on the application (your Demo
class, for instance). A more general solution:

def quit(self):
parent = self
while parent.winfo_class() != 'Tk':
if parent.master is None:
break;
parent = parent.master
else:
parent.destroy()

(from
https://sourceforge.net/tracker/index.php?func=detail&aid=661324&group_id=9579&atid=109579
)

This appears to work fine. But the loop, as written, could exit without
calling destroy() on anything; perhaps some other people knowing better
how Tkinter and Tk work could improve it or confirm it's fine as it is.
 
K

K Viltersten

Gabriel Genellina said:
No, use IDLE if you prefer, or any other editor/IDE. But in your case
there is an unfortunate coupling between IDLE and your script. Fix: change
the quit method as suggested in this thread:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/92bee52a3e2a325e/

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

This is OK if used on the top level widget on the application (your Demo
class, for instance). A more general solution:

def quit(self):
parent = self
while parent.winfo_class() != 'Tk':
if parent.master is None:
break;
parent = parent.master
else:
parent.destroy()

(from
https://sourceforge.net/tracker/index.php?func=detail&aid=661324&group_id=9579&atid=109579
)

This appears to work fine. But the loop, as written, could exit without
calling destroy() on anything; perhaps some other people knowing better
how Tkinter and Tk work could improve it or confirm it's fine as it is.


Thank you. I'll try that tomorrow.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top