469,902 Members | 1,966 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,902 developers. It's quick & easy.

Re-creating a Tkinter mainloop()

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 '06 #1
0 995

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nel | last post: by
4 posts views Thread by Craig Bailey | last post: by
1 post views Thread by Chris | last post: by
11 posts views Thread by James | last post: by
4 posts views Thread by Alan Walkington | last post: by
1 post views Thread by John Ryan | last post: by
10 posts views Thread by James | last post: by
8 posts views Thread by Beowulf | last post: by
1 post views Thread by joost | last post: by
3 posts views Thread by presspley | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.