Qinghao
Qinghao

Reputation: 374

How to open mutiple frames via thread in tkinter?

I tried to open mutiple frames by mutiple threads. Here is my code.

'''
This is the module for test and studying.
Author:Roger
Date: 2010/10/10
Python version: 2.6.5
'''


import threading, Tkinter


class Application(Tkinter.Frame):
    def __init__(self, master=None):
        Tkinter.Frame.__init__(self, master)
        self.columnconfigure(50)
        self.rowconfigure(50)
        self.grid()
        self.createWidgets()
        self.mainloop()

    def createWidgets(self):
        self.quitButton = Tkinter.Button (self, text='Quit', command=self.quit )
        self.quitButton.grid()


class lab_404(threading.Thread):
    '''
    Is this the doc_string of lab_404?

    Can there be mutiple_window?
    I do know why is it like this?
    Why is the button still on the frame?
    '''

    myWindow = None

    def __init__(self, computer = 10, server = None, table = 1, chair = 1, student = 2, myWindow = None):
        threading.Thread.__init__(self)
        self.__computer = computer
        self.__server = server
        self.__table = table
        self.__chair = chair
        self.__student = student
        self.myWindow = Application()
        #self.myWindow.mainloop()    #mainloop method is here, I don't where to put it.

    def getComputer(self):
        return self.__computer

    def getServer(self):
        return self.__server

    def getMyWindow(self):
        return self.myWindow


    def setServer(self, Server):
        self.__server = Server


    def run(self):
        print super(lab_404, self).getName(), 'This thread is starting now!'
        print super(lab_404, self).getName(), 'This thread is ending.'


if __name__ == '__main__':
    for n in xrange(1, 10, 1):
        tn = lab_404(server = n)  #Try to make a loop.
        tn.start()

The code above has been running as a frame, then stop (mainloop?). It won't continue to the next frame until I close the formmer window. It's fitful. How could I make it open new frames automatically?

Upvotes: 2

Views: 361

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385940

I don't think you can do what you want. Generally speaking, there's no need to run multiple frames and multiple event loops. You can have multiple frames within a single thread and within a single event loop.

Upvotes: 1

Related Questions