Re-creating a Tkinter mainloop()

S

srinathava

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

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

No members online now.

Forum statistics

Threads
473,965
Messages
2,570,148
Members
46,710
Latest member
FredricRen

Latest Threads

Top