Tkinter - problem closing window

D

Djames Suhanko

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 = "%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()
 
P

Peter Otten

Djames said:
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 = "%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
 
C

Collin D

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 = "%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()


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. :p
 
R

Roger

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 = "%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()


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


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. ;)
 
C

Collin D

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 = "%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()

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

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
 
M

Marc 'BlackJack' Rintsch

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 = "%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
 
D

Djames Suhanko

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 = "%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 !!


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 = "%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
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top