Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Tkinter - problem closing window

Reply
Thread Tools

Tkinter - problem closing window

 
 
Djames Suhanko
Guest
Posts: n/a
 
      01-05-2009
Hello!
I'm sorry my terrible english (my native language is portuguese).
I has a litle program that open another window. When I close de root
window in quit button, I need clicking 2 times to close. is where the
problem?

The source:
1 #!/usr/bin/env python
2 from Tkinter import *
3 import sys
4 import random
5 class App:
6 def __init__(self, master):
7 frame = Frame(master)
8 frame.pack()
9 rotulo = Label(frame, text="Clique em 'Gerar' e boa
sorte!",borderwidth=2,bg="gray",justify=C ENTER,relief=SUNKEN)
10 rotulo.pack()
11
12 self.button = Button(frame, text="Sair", fg="red",
command=frame.quit,borderwidth=1)
13 self.button.pack(side=LEFT)
14 self.hi_there = Button(frame, text="Gerar Numero",
command=self.say_hi,borderwidth=1)
15 self.hi_there.pack(side=RIGHT,padx=2,pady=2)
16
17 def gera_seis(self):
18 a = {}
19 for i in range(6):
20 a[i] = "%02d" % int (random.randint(0,60))
21 resultadoA = "%s-%s-%s-%s-%s-%s" %
(str(a[0]),str(a[1]),str(a[2]),str(a[3]),str(a[4]),str(a[5]))
22 return resultadoA
23
24 def say_hi(self):
25 resultado = self.gera_seis()
26 raiz = Tk()
27 F = Frame(raiz)
28 F.pack()
29 hello = Label(F, text=resultado)
30 hello.pack()
31 F.mainloop()
32
33 root = Tk()
34 root.title("$$$ Loteria $$$")
35 app = App(root)
36 root.mainloop()

--
Djames Suhanko
LinuxUser 158.760
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-05-2009
Djames Suhanko wrote:

> Hello!
> I'm sorry my terrible english (my native language is portuguese).
> I has a litle program that open another window. When I close de root
> window in quit button, I need clicking 2 times to close. is where the
> problem?
>
> The source:
> 1 #!/usr/bin/env python
> 2 from Tkinter import *
> 3 import sys
> 4 import random
> 5 class App:
> 6 def __init__(self, master):
> 7 frame = Frame(master)
> 8 frame.pack()
> 9 rotulo = Label(frame, text="Clique em 'Gerar' e boa
> sorte!",borderwidth=2,bg="gray",justify=C ENTER,relief=SUNKEN)
> 10 rotulo.pack()
> 11
> 12 self.button = Button(frame, text="Sair", fg="red",
> command=frame.quit,borderwidth=1)
> 13 self.button.pack(side=LEFT)
> 14 self.hi_there = Button(frame, text="Gerar Numero",
> command=self.say_hi,borderwidth=1)
> 15 self.hi_there.pack(side=RIGHT,padx=2,pady=2)
> 16
> 17 def gera_seis(self):
> 18 a = {}
> 19 for i in range(6):
> 20 a[i] = "%02d" % int (random.randint(0,60))
> 21 resultadoA = "%s-%s-%s-%s-%s-%s" %
> (str(a[0]),str(a[1]),str(a[2]),str(a[3]),str(a[4]),str(a[5]))
> 22 return resultadoA
> 23
> 24 def say_hi(self):
> 25 resultado = self.gera_seis()
> 26 raiz = Tk()
> 27 F = Frame(raiz)
> 28 F.pack()
> 29 hello = Label(F, text=resultado)
> 30 hello.pack()
> 31 F.mainloop()


You need only one mainloop(). Remove line 31 and you should be OK.

> 32
> 33 root = Tk()
> 34 root.title("$$$ Loteria $$$")
> 35 app = App(root)
> 36 root.mainloop()


Peter

 
Reply With Quote
 
 
 
 
Collin D
Guest
Posts: n/a
 
      01-05-2009
On Jan 5, 6:25*am, "Djames Suhanko" <(E-Mail Removed)> wrote:
> Hello!
> I'm sorry my terrible english (my native language is portuguese).
> I has a litle program that open another window. When I close de root
> window in quit button, I need clicking 2 times to close. is where the
> problem?
>
> The source:
> * 1 #!/usr/bin/env python
> * 2 from Tkinter import *
> * 3 import sys
> * 4 import random
> * 5 class App:
> * 6 *def __init__(self, master):
> * 7 * *frame = Frame(master)
> * 8 * *frame.pack()
> * 9 * *rotulo = Label(frame, text="Clique em 'Gerar' e boa
> sorte!",borderwidth=2,bg="gray",justify=C * *ENTER,relief=SUNKEN)
> *10 * *rotulo.pack()
> *11
> *12 * *self.button = Button(frame, text="Sair", fg="red",
> command=frame.quit,borderwidth=1)
> *13 * *self.button.pack(side=LEFT)
> *14 * *self.hi_there = Button(frame, text="Gerar Numero",
> command=self.say_hi,borderwidth=1)
> *15 * *self.hi_there.pack(side=RIGHT,padx=2,pady=2)
> *16
> *17 *def gera_seis(self):
> *18 * *a = {}
> *19 * *for i in range(6):
> *20 * * * a[i] = "%02d" % *int (random.randint(0,60))
> *21 * *resultadoA = "%s-%s-%s-%s-%s-%s" %
> (str(a[0]),str(a[1]),str(a[2]),str(a[3]),str(a[4]),str(a[5]))
> *22 * *return resultadoA
> *23
> *24 *def say_hi(self):
> *25 * *resultado = self.gera_seis()
> *26 * *raiz = Tk()
> *27 * *F = Frame(raiz)
> *28 * *F.pack()
> *29 * *hello = Label(F, text=resultado)
> *30 * *hello.pack()
> *31 * *F.mainloop()
> *32
> *33 root = Tk()
> *34 root.title("$$$ Loteria $$$")
> *35 app = App(root)
> *36 root.mainloop()
>
> --
> Djames Suhanko
> LinuxUser 158.760


Also for style, you might want to group the import lines so they look
like this:

from Tkinter import *
import sys, random

A bit more pythonic.
 
Reply With Quote
 
Roger
Guest
Posts: n/a
 
      01-05-2009
On Jan 5, 11:52*am, Collin D <(E-Mail Removed)> wrote:
> On Jan 5, 6:25*am, "Djames Suhanko" <(E-Mail Removed)> wrote:
>
>
>
> > Hello!
> > I'm sorry my terrible english (my native language is portuguese).
> > I has a litle program that open another window. When I close de root
> > window in quit button, I need clicking 2 times to close. is where the
> > problem?

>
> > The source:
> > * 1 #!/usr/bin/env python
> > * 2 from Tkinter import *
> > * 3 import sys
> > * 4 import random
> > * 5 class App:
> > * 6 *def __init__(self, master):
> > * 7 * *frame = Frame(master)
> > * 8 * *frame.pack()
> > * 9 * *rotulo = Label(frame, text="Clique em 'Gerar' e boa
> > sorte!",borderwidth=2,bg="gray",justify=C * *ENTER,relief=SUNKEN)
> > *10 * *rotulo.pack()
> > *11
> > *12 * *self.button = Button(frame, text="Sair", fg="red",
> > command=frame.quit,borderwidth=1)
> > *13 * *self.button.pack(side=LEFT)
> > *14 * *self.hi_there = Button(frame, text="Gerar Numero",
> > command=self.say_hi,borderwidth=1)
> > *15 * *self.hi_there.pack(side=RIGHT,padx=2,pady=2)
> > *16
> > *17 *def gera_seis(self):
> > *18 * *a = {}
> > *19 * *for i in range(6):
> > *20 * * * a[i] = "%02d" % *int (random.randint(0,60))
> > *21 * *resultadoA = "%s-%s-%s-%s-%s-%s" %
> > (str(a[0]),str(a[1]),str(a[2]),str(a[3]),str(a[4]),str(a[5]))
> > *22 * *return resultadoA
> > *23
> > *24 *def say_hi(self):
> > *25 * *resultado = self.gera_seis()
> > *26 * *raiz = Tk()
> > *27 * *F = Frame(raiz)
> > *28 * *F.pack()
> > *29 * *hello = Label(F, text=resultado)
> > *30 * *hello.pack()
> > *31 * *F.mainloop()
> > *32
> > *33 root = Tk()
> > *34 root.title("$$$ Loteria $$$")
> > *35 app = App(root)
> > *36 root.mainloop()

>
> > --
> > Djames Suhanko
> > LinuxUser 158.760

>
> Also for style, you might want to group the import lines so they look
> like this:
>
> from Tkinter import *
> import sys, random
>
> A bit more pythonic.


In that case you probably want to take out the 'from' import and:

import Tkinter, sys, random

in order to avoid any namespace issues especially if you have a large
project with lots of gui manipulations. But that's just me being
pedantic.
 
Reply With Quote
 
Collin D
Guest
Posts: n/a
 
      01-05-2009
On Jan 5, 9:21*am, Roger <(E-Mail Removed)> wrote:
> On Jan 5, 11:52*am, Collin D <(E-Mail Removed)> wrote:
>
>
>
> > On Jan 5, 6:25*am, "Djames Suhanko" <(E-Mail Removed)> wrote:

>
> > > Hello!
> > > I'm sorry my terrible english (my native language is portuguese).
> > > I has a litle program that open another window. When I close de root
> > > window in quit button, I need clicking 2 times to close. is where the
> > > problem?

>
> > > The source:
> > > * 1 #!/usr/bin/env python
> > > * 2 from Tkinter import *
> > > * 3 import sys
> > > * 4 import random
> > > * 5 class App:
> > > * 6 *def __init__(self, master):
> > > * 7 * *frame = Frame(master)
> > > * 8 * *frame.pack()
> > > * 9 * *rotulo = Label(frame, text="Clique em 'Gerar' e boa
> > > sorte!",borderwidth=2,bg="gray",justify=C * *ENTER,relief=SUNKEN)
> > > *10 * *rotulo.pack()
> > > *11
> > > *12 * *self.button = Button(frame, text="Sair", fg="red",
> > > command=frame.quit,borderwidth=1)
> > > *13 * *self.button.pack(side=LEFT)
> > > *14 * *self.hi_there = Button(frame, text="Gerar Numero",
> > > command=self.say_hi,borderwidth=1)
> > > *15 * *self.hi_there.pack(side=RIGHT,padx=2,pady=2)
> > > *16
> > > *17 *def gera_seis(self):
> > > *18 * *a = {}
> > > *19 * *for i in range(6):
> > > *20 * * * a[i] = "%02d" % *int (random.randint(0,60))
> > > *21 * *resultadoA = "%s-%s-%s-%s-%s-%s" %
> > > (str(a[0]),str(a[1]),str(a[2]),str(a[3]),str(a[4]),str(a[5]))
> > > *22 * *return resultadoA
> > > *23
> > > *24 *def say_hi(self):
> > > *25 * *resultado = self.gera_seis()
> > > *26 * *raiz = Tk()
> > > *27 * *F = Frame(raiz)
> > > *28 * *F.pack()
> > > *29 * *hello = Label(F, text=resultado)
> > > *30 * *hello.pack()
> > > *31 * *F.mainloop()
> > > *32
> > > *33 root = Tk()
> > > *34 root.title("$$$ Loteria $$$")
> > > *35 app = App(root)
> > > *36 root.mainloop()

>
> > > --
> > > Djames Suhanko
> > > LinuxUser 158.760

>
> > Also for style, you might want to group the import lines so they look
> > like this:

>
> > from Tkinter import *
> > import sys, random

>
> > A bit more pythonic.

>
> In that case you probably want to take out the 'from' import and:
>
> import Tkinter, sys, random
>
> in order to avoid any namespace issues especially if you have a large
> project with lots of gui manipulations. *But that's just me being
> pedantic.


I agree... you could have conflicting functions.. not fun. XD
 
Reply With Quote
 
Marc 'BlackJack' Rintsch
Guest
Posts: n/a
 
      01-06-2009
On Mon, 05 Jan 2009 12:25:53 -0200, Djames Suhanko wrote:

> I has a litle program that open another window. When I close de root
> window in quit button, I need clicking 2 times to close. is where the
> problem?
>
> […]
>
> 17 def gera_seis(self):
> 18 a = {}
> 19 for i in range(6):
> 20 a[i] = "%02d" % int (random.randint(0,60))
> 21 resultadoA = "%s-%s-%s-%s-%s-%s" %
> (str(a[0]),str(a[1]),str(a[2]),str(a[3]),str(a[4]),str(a[5]))
> 22 return resultadoA


Not the problem but unnecessary complex. `random.randint()` already
returns an int, no need to call `int()` on it. The string formatting
with ``%`` returns strings, so there is no need to call `str()` on the
values. Even if the values where not strings: The '%s' place holder
implies a call to `str()` while formatting. If you put something into a
dictionary with consecutive `int` keys, you might use a list instead.

All this can be written as a simple one liner::

'-'.join(str(random.randint(0, 60)) for dummy in xrange(6))

> 24 def say_hi(self):
> 25 resultado = self.gera_seis()
> 26 raiz = Tk()


The problem is here…

> 27 F = Frame(raiz)
> 28 F.pack()
> 29 hello = Label(F, text=resultado) 30 hello.pack()
> 31 F.mainloop()


…and here.

There is only one `Tk` instance and mainloop allowed per `Tkinter`
application. Otherwise really strange things can happen. Additional
windows have to be created as `Toplevel` instances.

Ciao,
Marc 'BlackJack' Rintsch
 
Reply With Quote
 
Djames Suhanko
Guest
Posts: n/a
 
      01-07-2009
Wow, nice!
But, with join i can't padding with 0.
'-'.join(str(random.randint(0, 60)) for dummy in xrange(6))

Then, i has been used:
a[i] = "%02d" % int(random.randint(0,60))
I will change int(random.randint(0,60)) for random.randint(0,60), only.
Thank you, guys !!

The problem was solved when removed the second "mainloop".

I will use the Toplevel instead Tk() again. Thank you for the nice lesson !!


On Tue, Jan 6, 2009 at 6:47 AM, Marc 'BlackJack' Rintsch <(E-Mail Removed)> wrote:
> On Mon, 05 Jan 2009 12:25:53 -0200, Djames Suhanko wrote:
>
>> I has a litle program that open another window. When I close de root
>> window in quit button, I need clicking 2 times to close. is where the
>> problem?
>>
>> []
>>
>> 17 def gera_seis(self):
>> 18 a = {}
>> 19 for i in range(6):
>> 20 a[i] = "%02d" % int (random.randint(0,60))
>> 21 resultadoA = "%s-%s-%s-%s-%s-%s" %
>> (str(a[0]),str(a[1]),str(a[2]),str(a[3]),str(a[4]),str(a[5]))
>> 22 return resultadoA

>
> Not the problem but unnecessary complex. `random.randint()` already
> returns an int, no need to call `int()` on it. The string formatting
> with ``%`` returns strings, so there is no need to call `str()` on the
> values. Even if the values where not strings: The '%s' place holder
> implies a call to `str()` while formatting. If you put something into a
> dictionary with consecutive `int` keys, you might use a list instead.
>
> All this can be written as a simple one liner::
>
> '-'.join(str(random.randint(0, 60)) for dummy in xrange(6))
>
>> 24 def say_hi(self):
>> 25 resultado = self.gera_seis()
>> 26 raiz = Tk()

>
> The problem is here
>
>> 27 F = Frame(raiz)
>> 28 F.pack()
>> 29 hello = Label(F, text=resultado) 30 hello.pack()
>> 31 F.mainloop()

>
> and here.
>
> There is only one `Tk` instance and mainloop allowed per `Tkinter`
> application. Otherwise really strange things can happen. Additional
> windows have to be created as `Toplevel` instances.
>
> Ciao,
> Marc 'BlackJack' Rintsch
> --
> http://mail.python.org/mailman/listinfo/python-list
>




--
Djames Suhanko
LinuxUser 158.760
 
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
Closing popup window when closing parent window? =?Utf-8?B?Vk1J?= ASP .Net 3 02-15-2007 08:29 AM
Closing dialog window in Tkinter Svennglenn Python 3 03-03-2005 02:40 AM
Closing child window when opening a new Window Patrick Olurotimi Ige ASP .Net 2 12-14-2004 04:59 AM
Closing child window and refreshing Parent window automatically =?Utf-8?B?U2lsZWVzaA==?= ASP .Net 4 10-14-2004 02:01 AM
Closing child window WITHOUT closing parent thomas Javascript 0 10-23-2003 04:10 PM



Advertisments