def initialize(self):
        self.msg = Messages(self)
        self.patterns = []
        self.pattern = None
        self.currentDatFile = None

        self.initConfig()
        self.initializeUtilities()

        self.gui = Gui()
        self.gui.initializeMainWindow(self)
        self.updatePatternCanvasLastSize()
        self.patternListBox.bind('<<ListboxSelect>>', self.patternSelected)
        self.after_idle(self.canvasConfigured)
        self.deviceEntry.entryText.set(self.getConfig().device)
        self.datFileEntry.entryText.set(self.getConfig().datFile)

        self.initEmulator()
        self.after_idle(self.reloadPatternFile)
class KnittingApp(Tkinter.Tk):

    def __init__(self,parent=None):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()
        #self.startEmulator()
        
    def initialize(self):
        self.msg = Messages(self)
        self.patterns = []
        self.pattern = None
        self.currentDatFile = None

        self.initConfig()
        self.initializeUtilities()

        self.gui = Gui()
        self.gui.initializeMainWindow(self)
        self.updatePatternCanvasLastSize()
        self.patternListBox.bind('<<ListboxSelect>>', self.patternSelected)
        self.after_idle(self.canvasConfigured)
        self.deviceEntry.entryText.set(self.getConfig().device)
        self.datFileEntry.entryText.set(self.getConfig().datFile)

        self.initEmulator()
        self.after_idle(self.reloadPatternFile)
        
    def initializeUtilities(self):
        self.patternDumper = PatternDumper()
        self.patternDumper.printInfoCallback = self.msg.showInfo
        self.patternInserter = PatternInserter()
        self.patternInserter.printInfoCallback = self.msg.showInfo
        self.patternInserter.printErrorCallback = self.msg.showError
        
    def initEmulator(self):
        self.emu = PDDemulator(self.getConfig().imgdir)
        self.emu.listeners.append(PDDListener(self))
        #self.emu = lambda: 1
        self.setEmulatorStarted(False)
    
    def emuButtonClicked(self):
        self.getConfig().device = self.deviceEntry.entryText.get()
        if self.emu.started:
            self.stopEmulator()
        else:
            self.startEmulator()
        
    def startEmulator(self):
        self.msg.showInfo('Preparing emulator. . . Please Wait')

        if self.getConfig().simulateEmulator:
            self.msg.showInfo('Simulating emulator, emulator is not started...')
            self.setEmulatorStarted(True)
        else:
            try:
                port = self.getConfig().device
                self.emu.open(cport=port)
                self.msg.showInfo('PDDemulate Version 1.1 Ready!')
                self.setEmulatorStarted(True)
                self.after_idle(self.emulatorLoop)
            except Exception, e:
                self.msg.showError('Ensure that TFDI cable is connected to port ' + port + '\n\nError: ' + str(e))
                self.setEmulatorStarted(False)