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

    Re: Tk mainloop()

    John Roth, Jul 20, 2003, in forum: Python
    Replies:
    0
    Views:
    406
    John Roth
    Jul 20, 2003
  2. dan

    Re: Tk mainloop()

    dan, Jul 20, 2003, in forum: Python
    Replies:
    0
    Views:
    482
  3. k2riddim

    Problem with tkinter mainloop

    k2riddim, Nov 30, 2004, in forum: Python
    Replies:
    2
    Views:
    1,454
    k2riddim
    Nov 30, 2004
  4. Peter Otten
    Replies:
    2
    Views:
    787
    John McMonagle
    Mar 15, 2009
  5. Frederic Rentsch

    Tkinter: IDLE can't get out of mainloop

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

Share This Page