Re-creating a Tkinter mainloop()

Discussion in 'Python' started by srinathava@gmail.com, Apr 18, 2006.

  1. Guest

    Hi,

    I am trying to run a Tkinter application in a thread and it
    works pretty well to an extent. However, when I try to
    recreate the application after the thread exits, the new
    application never shows up. The code below the message
    explains what I am trying.

    On running this, you should see a simple two-button frame
    pop-up, while a sting will keep being printed on the
    console. Please press the "QUIT" button before the counter
    counts down to 0. You should see "done with main()" on the
    console. When the counter hits 0, I see

    opening new thread

    on the console, so main() is being called, but no window
    appears.

    I am quite new to Tkinter, so I hope I am being clear about
    my intentions...

    Thanks,
    Srinath

    from Tkinter import *
    import thread
    import time

    class Application(Frame):
    def say_hi(self):
    print 'hello world'

    def createWidgets(self):
    self.QUIT = Button(self)
    self.QUIT["text"] = "QUIT"
    self.QUIT["fg"] = "red"
    self.QUIT["command"] = self.quit

    self.QUIT.pack({"side": "left"})

    self.hi_there = Button(self)
    self.hi_there["text"] = "Hello",
    self.hi_there["command"] = self.say_hi

    self.hi_there.pack({"side": "left"})

    def __init__(self, master=None):
    Frame.__init__(self, master)
    self.pack()
    self.createWidgets()

    def main():
    app = Application()
    app.mainloop()
    print 'done with main()'
    app.destroy()

    t = thread.start_new_thread(main, ())

    n = 20
    while 1:
    print 'in main thread, n = %d' % n
    time.sleep(0.5)
    n -= 1
    if n == 0:
    print 'opening new thread'
    t2 = thread.start_new_thread(main, ())
     
    , Apr 18, 2006
    #1
    1. Advertisements

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

    Problem with tkinter mainloop

    k2riddim, Nov 30, 2004, in forum: Python
    Replies:
    2
    Views:
    1,639
    k2riddim
    Nov 30, 2004
  2. John Hunter

    detect tk mainloop

    John Hunter, Jan 26, 2005, in forum: Python
    Replies:
    0
    Views:
    393
    John Hunter
    Jan 26, 2005
  3. Kreedz
    Replies:
    2
    Views:
    541
    Kreedz
    Sep 12, 2005
  4. zxo102
    Replies:
    10
    Views:
    921
    zxo102
    Aug 8, 2006
  5. [david]

    wxPython before MainLoop

    [david], Aug 9, 2007, in forum: Python
    Replies:
    23
    Views:
    1,360
    [david]
    Aug 21, 2007
  6. globalrev

    question about the mainloop

    globalrev, Apr 21, 2008, in forum: Python
    Replies:
    6
    Views:
    824
    globalrev
    Apr 22, 2008
  7. Peter Otten
    Replies:
    2
    Views:
    856
    John McMonagle
    Mar 15, 2009
  8. Frederic Rentsch

    Tkinter: IDLE can't get out of mainloop

    Frederic Rentsch, Mar 31, 2012, in forum: Python
    Replies:
    0
    Views:
    391
    Frederic Rentsch
    Mar 31, 2012
Loading...