Re-creating a Tkinter mainloop()

srinathava at gmail.com srinathava at gmail.com
Mon Apr 17 20:51:40 EDT 2006


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, ())




More information about the Python-list mailing list