[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Quit-command not quiting

K Viltersten

3/7/2008 2:57:00 PM

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 ()


--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

4 Answers

Gabriel Genellina

3/7/2008 3:18:00 PM

0

En Fri, 07 Mar 2008 12:56:44 -0200, K Viltersten <tmp1@viltersten.com>
escribi�:

> 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...

--
Gabriel Genellina

K Viltersten

3/7/2008 3:57:00 PM

0

>> The window 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.

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?

>> 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...

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.

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy

Gabriel Genellina

3/8/2008 10:14:00 PM

0

En Fri, 07 Mar 2008 13:56:45 -0200, K Viltersten <tmp1@viltersten.com>
escribi�:

>>> The window 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.
>
> 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/92bee52...

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&a...
)

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.

--
Gabriel Genellina

K Viltersten

3/9/2008 12:56:00 AM

0

"Gabriel Genellina" <gagsl-py2@yahoo.com.ar> skrev i meddelandet
news:mailman.1750.1205017517.9267.python-list@python.org...
> En Fri, 07 Mar 2008 13:56:45 -0200, K Viltersten <tmp1@viltersten.com>
> escribi�:
>
>>>> The window 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.
>>
>> 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/92bee52...
>
> 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&a...
> )
>
> 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.

--
Regards
Konrad Viltersten
--------------------------------
sleep - a substitute for coffee for the poor
ambition - lack of sense to be lazy