Tkinter - problem closing window

Discussion in 'Python' started by Djames Suhanko, Jan 5, 2009.

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

    --
    Djames Suhanko
    LinuxUser 158.760
     
    Djames Suhanko, Jan 5, 2009
    #1
    1. Advertising

  2. Djames Suhanko

    Peter Otten Guest

    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 = "%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
     
    Peter Otten, Jan 5, 2009
    #2
    1. Advertising

  3. Djames Suhanko

    Collin D Guest

    On Jan 5, 6:25 am, "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 = "%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. :p
     
    Collin D, Jan 5, 2009
    #3
  4. Djames Suhanko

    Roger Guest

    On Jan 5, 11:52 am, Collin D <> wrote:
    > On Jan 5, 6:25 am, "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 = "%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. :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. ;)
     
    Roger, Jan 5, 2009
    #4
  5. Djames Suhanko

    Collin D Guest

    On Jan 5, 9:21 am, Roger <> wrote:
    > On Jan 5, 11:52 am, Collin D <> wrote:
    >
    >
    >
    > > On Jan 5, 6:25 am, "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 = "%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. :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
     
    Collin D, Jan 5, 2009
    #5
  6. 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 = "%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
     
    Marc 'BlackJack' Rintsch, Jan 6, 2009
    #6
  7. 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 !!


    On Tue, Jan 6, 2009 at 6:47 AM, Marc 'BlackJack' Rintsch <> 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 = "%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
     
    Djames Suhanko, Jan 7, 2009
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?U2lsZWVzaA==?=
    Replies:
    4
    Views:
    34,045
    =?Utf-8?B?U29sZWwgU29mdHdhcmU=?=
    Oct 14, 2004
  2. Patrick Olurotimi Ige

    Closing child window when opening a new Window

    Patrick Olurotimi Ige, Dec 14, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    462
    Patrick Olurotimi Ige
    Dec 14, 2004
  3. Svennglenn

    Closing dialog window in Tkinter

    Svennglenn, Mar 1, 2005, in forum: Python
    Replies:
    3
    Views:
    446
    Harlin Seritt
    Mar 3, 2005
  4. =?Utf-8?B?Vk1J?=

    Closing popup window when closing parent window?

    =?Utf-8?B?Vk1J?=, Feb 14, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    679
    Thomas Hansen
    Feb 15, 2007
  5. thomas
    Replies:
    0
    Views:
    251
    thomas
    Oct 23, 2003
Loading...

Share This Page