Search code examples
pythonpygamepane

why is my code still displaying the old panes?


So basically I have a two separate screens that I want displayed. One that has 11 Panes displayed on the screen at one time and once I click on a Pane named keyboard it should clear the screen and set up 36 Panes which should display a keyboard (for test purposes I only used three panes).

My problem is that once I click on keyboard it does clear the screen and display the 3 test panes how it also display's my other 10 panes that come from the first page.

Example: This is fine to start with: Fine Display

This is not fine. (A, B And C should be displayed but the rest shouldn't): Not Fine

Here is the code:

import pygame
import sys
white = (255,255,255)
black = (0,0,0)
objs = []
MAIN_BUTTON = 2

KEYBOARD = True
WORDS = False


class Pane():

    def __init__(self, textToDisplay, coordinates, screen):
        self.textToDisplay = textToDisplay
        self.coordinates = coordinates
        self.screen = screen
        self.font = pygame.font.SysFont('Palace Script MT', 25)
        Screen = pygame.display.set_mode((1366,768), 0, 32)
        self.screen = Screen
        self.screen.fill((white))

    def coordinates(self):
        return self.coordinates

    def text(self):
        return self.textToDisplay

    def drawPane(self):
        textCoords = self.coordinates        
        self.screen.blit(self.font.render(self.textToDisplay, True, (black)), textCoords)
        pygame.draw.rect(self.screen, (black), self.coordinates, 2)
        pygame.display.update()


class Screen():


    NoOfPanes = 0
    Panes = []
    paneLocs = [(583, 334, 300, 150), 
                (633, 150, 200, 125), 
                (633, 600, 200, 125), 
                (350, 360, 200, 100), 
                (925, 360, 200, 100), 
                (1000, 150, 150, 100), 
                (275, 150, 150, 100), 
                (275, 600, 150, 100), 
                (1000, 600, 150, 100), 
                (75, 350, 200, 100),
                (0, 668, 200, 100)
                ]    

    keyboardPaneLocs = [(0, 100, 100, 100),
                        (0, 200, 100, 100),
                        (0, 300, 100, 100),
                        (0, 400, 100, 100)
                        ]

    def __init__(self):
        pygame.init()
        pygame.display.set_caption('Box Test')

        self.font = pygame.font.SysFont('Arial', 25)
        Screen = pygame.display.set_mode((1366,768), 0, 32)
        self.screen = Screen
        self.screen.fill((white))

        pygame.display.update()

    def addPane(self, textToDisplay, keyboardFlag):      
        if (not keyboardFlag) and (self.NoOfPanes > 11):
            print("Limit Reached")            
        else:
            print(int(self.NoOfPanes))

            if keyboardFlag:
                newPane = Pane(textToDisplay, self.keyboardPaneLocs[self.NoOfPanes], Screen)
            else:
                newPane = Pane(textToDisplay, self.paneLocs[self.NoOfPanes], Screen)
            self.Panes.append(newPane)            

            self.NoOfPanes = self.NoOfPanes + 1
            pygame.display.update()


    def drawPanes(self):
        for Pane in self.Panes:
            Pane.drawPane()

    def mousePosition(self):
        global clickPos
        global releasePos
        for event in pygame.event.get():
            if event.type == MAIN_BUTTON:
                self.Pos = pygame.mouse.get_pos()
                return MAIN_BUTTON
            else:
                return False

    def mouseDown(self, posx, posy):
        textToReturn = "Nothing selected"
        for Pane in self.Panes:
            paneCoords = Pane.coordinates
            print(str(paneCoords[0]) + ":" + str(paneCoords[1]) + ":" + str(paneCoords[2]) + ":" + str(paneCoords[3]))
            if (paneCoords[0] <= posx <= paneCoords[0]+paneCoords[2]) and (paneCoords[1] <= posy <= paneCoords[1]+paneCoords[3]):            
                textToReturn = Pane.text()

        return textToReturn

    def displayKeyboard(self):
        self.addPane("A", KEYBOARD)
        self.addPane("B", KEYBOARD)
        self.addPane("C", KEYBOARD)

    def clearScreen(self):
        Screen = pygame.display.set_mode((1366,768), 0, 32)
        self.screen = Screen
        self.screen.fill((white))





if __name__ == '__main__':

    myScreen = Screen()
    myScreen.addPane("1", WORDS)
    myScreen.addPane("2", WORDS)
    myScreen.addPane("3", WORDS)
    myScreen.addPane("4", WORDS)
    myScreen.addPane("5", WORDS)
    myScreen.addPane("6", WORDS)
    myScreen.addPane("7", WORDS)
    myScreen.addPane("8", WORDS)
    myScreen.addPane("9", WORDS)
    myScreen.addPane("10", WORDS)
    myScreen.addPane("Keyboard", WORDS)

    myScreen.drawPanes()


    while True:
        ev = pygame.event.get()
        for event in ev:
            if event.type == pygame.MOUSEBUTTONUP:
                posx,posy = pygame.mouse.get_pos()
                textSelected = myScreen.mouseDown(posx, posy)
                print(textSelected)
                if textSelected == "Keyboard":
                    myScreen = Screen()
                    myScreen.clearScreen()
                    myScreen.displayKeyboard()
                    myScreen.drawPanes()

        for event in pygame.event.get():        
            if event.type == pygame.QUIT:
                pygame.quit(); sys.exit();

I think the problem that I am facing is that all of these panes are being added to the list Panes = []. How can I make sure that this list is clear before I display the new panes through the use of the function def drawPanes(self):

Don't take my word for it as it is just an assumption and I am well and truly stuck. (Any help is appreciated.)


Solution

  • You can assing empty list to Pane to make it empty

    Panes = []
    

    but you will lost all added panes forever.

    Maybe you should make seperated list for keyboard panes and than draw only this list.

    EDIT:

    By the way:

    You use one variable myScreen to both screens - when you run myScreen = Screen(...) you create new screen but loose access to previous screen.

    Using Screen = pygame.display.set_mode((1366,768), 0, 32); self.screen = Screen to clear screen is waste of time and memories. You need only self.screen.fill((white))

    You use Screen name as class name and as variable in some functions. You can have accidentally name conflict.

    EDIT:

    Full working example how code could look like:

    I use one self.screen so window isn't created/deleted everytime.

    You can press "ESC" key to show/hide keyboard.

    import pygame
    import sys
    
    #----------------------------------------------------------------------
    
    WHITE = (255,255,255)
    BLACK = (0  ,0  ,0  )
    
    #---------------------------
    
    MAIN_BUTTON = 2
    
    KEYBOARD = True
    WORDS = False
    
    #----------------------------------------------------------------------
    
    class Pane():
    
        def __init__(self, textToDisplay, coordinates, screen):
    
            self.textToDisplay = textToDisplay
            self.coordinates = coordinates
            self.screen = screen
            self.font = pygame.font.SysFont('Palace Script MT', 25)
    
        #---------------------------
    
        def coordinates(self):
            return self.coordinates
    
        #---------------------------
    
        def text(self):
            return self.textToDisplay
    
        #---------------------------
    
        def drawPane(self):
            textCoords = self.coordinates        
            self.screen.blit(self.font.render(self.textToDisplay, True, BLACK), textCoords)
            pygame.draw.rect(self.screen, BLACK, self.coordinates, 2)
    
    #----------------------------------------------------------------------
    
    class Application():
    
        NoOfPanes = 0
        NoOfKeys = 0
        Panes = []
        Keys = []
    
        paneLocs = [(583, 334, 300, 150), 
                    (633, 150, 200, 125), 
                    (633, 600, 200, 125), 
                    (350, 360, 200, 100), 
                    (925, 360, 200, 100), 
                    (1000, 150, 150, 100), 
                    (275, 150, 150, 100), 
                    (275, 600, 150, 100), 
                    (1000, 600, 150, 100), 
                    (75, 350, 200, 100),
                    (0, 668, 200, 100)
                    ]    
    
        keyboardPaneLocs = [(0, 100, 100, 100),
                            (0, 200, 100, 100),
                            (0, 300, 100, 100),
                            (0, 400, 100, 100)
                            ]
    
        #---------------------------
    
        def __init__(self):
    
            pygame.init()
            pygame.display.set_caption('Box Test')
    
            self.font = pygame.font.SysFont('Arial', 25)
            self.screen = pygame.display.set_mode((1366,768), 0, 32)
    
            self.show_panes = True
            self.show_keyboard = False
    
            self.createPanes()
            self.createKeyboard()
    
        #---------------------------
    
        def close(self):
            print "pygame quit"
            pygame.quit()
            sys.exit()
    
        #---------------------------
    
        def createPanes(self):
            self.addPane("1", WORDS)
            self.addPane("2", WORDS)
            self.addPane("3", WORDS)
            self.addPane("4", WORDS)
            self.addPane("5", WORDS)
            self.addPane("6", WORDS)
            self.addPane("7", WORDS)
            self.addPane("8", WORDS)
            self.addPane("9", WORDS)
            self.addPane("10", WORDS)
            self.addPane("Keyboard", WORDS)
    
        #---------------------------
    
        def createKeyboard(self):
            self.addPane("A", KEYBOARD)
            self.addPane("B", KEYBOARD)
            self.addPane("C", KEYBOARD)
    
        #---------------------------
    
        def addPane(self, textToDisplay, keyboardFlag):      
            if (not keyboardFlag) and (self.NoOfPanes > 11):
                print("Limit Reached")            
            else:
                print(int(self.NoOfPanes))
    
                if keyboardFlag:
                    self.Keys.append(Pane(textToDisplay, self.keyboardPaneLocs[self.NoOfKeys], self.screen))
                    self.NoOfKeys += 1
                else:
                    self.Panes.append(Pane(textToDisplay, self.paneLocs[self.NoOfPanes], self.screen))
                    self.NoOfPanes += 1
    
        #---------------------------
    
        def drawPanes(self):
            for Pane in self.Panes:
                Pane.drawPane()
    
        #---------------------------
    
        def drawKeyboard(self):
            for Key in self.Keys:
                Key.drawPane()
    
        #---------------------------
    
        def mousePosition(self, event):
                if event.type == MAIN_BUTTON:
                    self.Pos = pygame.mouse.get_pos()
                    return MAIN_BUTTON
                else:
                    return False
    
        #---------------------------
    
        def mouseDown(self):
            posx,posy = pygame.mouse.get_pos()      
            textToReturn = "Nothing selected"
            if self.show_panes:
                for Pane in self.Panes:
                    paneCoords = Pane.coordinates
                    print(str(paneCoords[0]) + ":" + str(paneCoords[1]) + ":" + str(paneCoords[2]) + ":" + str(paneCoords[3]))
                    if (paneCoords[0] <= posx <= paneCoords[0]+paneCoords[2]) and (paneCoords[1] <= posy <= paneCoords[1]+paneCoords[3]):            
                        textToReturn = Pane.text()
            elif self.show_keyboard:
                for Pane in self.Keys:
                    paneCoords = Pane.coordinates
                    print(str(paneCoords[0]) + ":" + str(paneCoords[1]) + ":" + str(paneCoords[2]) + ":" + str(paneCoords[3]))
                    if (paneCoords[0] <= posx <= paneCoords[0]+paneCoords[2]) and (paneCoords[1] <= posy <= paneCoords[1]+paneCoords[3]):            
                        textToReturn = Pane.text()
    
            return textToReturn
    
        #---------------------------
    
        def run(self):
    
            clock = pygame.time.Clock()
    
            RUNNING = True
            while RUNNING:
    
                # --- events ---
    
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        RUNNING = False
                        break
    
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_ESCAPE:
                            self.show_keyboard = not self.show_keyboard
                            self.show_panes = not self.show_panes
    
                    if event.type == pygame.MOUSEBUTTONUP:
                        textSelected = self.mouseDown()
                        print(textSelected)
                        if textSelected == "Keyboard":
                            self.show_keyboard = True
                            self.show_panes = False
    
                # --- draws ---
    
                self.screen.fill(WHITE)
    
                if self.show_panes:
                    self.drawPanes()
    
                if self.show_keyboard:
                    self.drawKeyboard()
    
                pygame.display.update()
    
                # --- FPS ---
    
                clock.tick(25)
    
            self.close()    
    #----------------------------------------------------------------------
    
    Application().run()
    

    EDIT:

    • Class Keyboard
    • Pane has one event_handler
    • Pane test if mouse is over
    • Pane change background when mouse is over (yellow)
    • Pane change background when mouse is pressed (red)
    • clicking key Q on screen to close keyboard
    • keyboard send own event to mainloop when key Q is click - to inform that keyboard is closed

    .

    import pygame
    import sys
    
    #----------------------------------------------------------------------
    
    WHITE = (255,255,255)
    BLACK = (0  ,0  ,0  )
    
    #---------------------------
    
    MAIN_BUTTON = 2
    
    KEYBOARD = True
    WORDS = False
    
    #----------------------------------------------------------------------
    
    class Pane():
    
        def __init__(self, textToDisplay, rect, screen, fgColor=(0,0,0), bgColor=(255,255,255), mouseoverBgColor=(255,255,0) ):
    
            self.textToDisplay = textToDisplay
            self.rect = pygame.Rect(*rect)
            self.screen = screen
    
            # colors
    
            self.fgColor = fgColor
            self.bgColor = bgColor
    
            self.mouseoverBgColor = mouseoverBgColor
    
            self.currentFgColor = self.fgColor
            self.currentBgColor = self.bgColor
    
            self.font = pygame.font.SysFont('Palace Script MT', 25)
    
        #---------------------------
    
        def coordinates(self):
            return self.rect
    
        #---------------------------
    
        def text(self):
            return self.textToDisplay
    
        #---------------------------
    
        def draw(self):
            # background
            self.screen.fill(self.currentBgColor, self.rect)
    
            # border
            pygame.draw.rect(self.screen, self.currentFgColor, self.rect, 2)
    
            # text
            self.screen.blit(self.font.render(self.textToDisplay, True, self.currentFgColor), self.rect)
    
        #---------------------------
    
        def test_coordinates(self, posx, posy):
            return self.rect.collidepoint(posx, posy)
    
        #---------------------------
    
        def event_handler(self, event):
    
            # standard pane color
            self.currentFgColor = self.fgColor
            self.currentBgColor = self.bgColor
    
            # if mouse over pane change color
    
            if event.type == pygame.MOUSEMOTION:
                if self.rect.collidepoint(event.pos):
                    self.currentBgColor = self.mouseoverBgColor
    
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if self.rect.collidepoint(event.pos):
                    self.currentBgColor = (255,0,0)
    
            elif event.type == pygame.MOUSEBUTTONUP:
                if self.rect.collidepoint(event.pos):
                    self.currentBgColor = self.mouseoverBgColor
    
    #----------------------------------------------------------------------
    
    class Keyboard():
    
        Keys = []
        paneLocs = []
        visible = False
        #---------------------------
    
        def __init__(self, screen):
    
            self.screen = screen
            self.font = pygame.font.SysFont('Arial', 25)
    
            self.create()
    
        #---------------------------
    
        def create(self):
    
            letter_code = ord("A")
    
            for y in range(2):
                for x in range(13):
                    lock = (100*x, 100*y, 100, 100)
                    self.paneLocs.append(lock)
                    self.Keys.append(Pane(chr(letter_code), lock, self.screen, (0,0,0), (0,255,0)))
                    letter_code += 1
    
        #---------------------------
    
        def draw(self):
            for key in self.Keys:
                key.draw()
    
        #---------------------------
    
        def event_handler(self, event):
    
            textToReturn = None
    
            # let panes handle event
            for key in self.Keys:
                key.event_handler(event)
    
            if event.type == pygame.MOUSEBUTTONUP:
                posx, posy = event.pos
                for key in self.Keys:
                    #print "debug: keyboard.event_handler", posx, posy
                    if key.test_coordinates(posx, posy):
                        textToReturn = key.text()       
    
                        print textToReturn
    
                # if Q was 
                if textToReturn == 'Q':
                    self.onKeyQ(textToReturn)
    
            return textToReturn
    
        #---------------------------
    
        def onKeyQ(self, text):
    
            # hide keyboard
            self.visible = False
    
            # send event to inform main loop that keyboard was closed
            pygame.event.post(pygame.event.Event(pygame.USEREVENT, {'code': 666, 'key':text}))
    
    #----------------------------------------------------------------------
    
    class Application():
    
        NoOfPanes = 0
        Panes = []
    
        paneLocs = [(583, 334, 300, 150), 
                    (633, 150, 200, 125), 
                    (633, 600, 200, 125), 
                    (350, 360, 200, 100), 
                    (925, 360, 200, 100), 
                    (1000, 150, 150, 100), 
                    (275, 150, 150, 100), 
                    (275, 600, 150, 100), 
                    (1000, 600, 150, 100), 
                    (75, 350, 200, 100),
                    (0, 668, 200, 100)
                    ]    
    
        #---------------------------
    
        def __init__(self):
    
            pygame.init()
            pygame.display.set_caption('Box Test')
    
            self.font = pygame.font.SysFont('Arial', 25)
            self.screen = pygame.display.set_mode((1366,768), 0, 32)
    
            self.show_panes = True
    
            self.createPanes()
    
            self.keyboard = Keyboard(self.screen)
    
        #---------------------------
    
        def close(self):
            print "pygame quit"
            pygame.quit()
            sys.exit()
    
        #---------------------------
    
        def createPanes(self):
            self.addPane("1", WORDS)
            self.addPane("2", WORDS)
            self.addPane("3", WORDS)
            self.addPane("4", WORDS)
            self.addPane("5", WORDS)
            self.addPane("6", WORDS)
            self.addPane("7", WORDS)
            self.addPane("8", WORDS)
            self.addPane("9", WORDS)
            self.addPane("10", WORDS)
            self.addPane("Keyboard", WORDS)
    
        #---------------------------
    
        def addPane(self, textToDisplay, keyboardFlag):      
            if (not keyboardFlag) and (self.NoOfPanes > 11):
                print("Limit Reached")            
            else:
                print(int(self.NoOfPanes))
    
                if not keyboardFlag:
                    self.Panes.append(Pane(textToDisplay, self.paneLocs[self.NoOfPanes], self.screen))
                    self.NoOfPanes += 1
    
        #---------------------------
    
        def drawPanes(self):
            for Pane in self.Panes:
                Pane.draw()
    
        #---------------------------
    
        def mousePosition(self, event):
                if event.type == MAIN_BUTTON:
                    self.Pos = event.pos
                    return MAIN_BUTTON
                else:
                    return False
    
        #---------------------------
    
        def event_handler(self, event):
    
            textToReturn = None
    
            # let panes handle events
            for pane in self.Panes:
                pane.event_handler(event)
    
            if event.type == pygame.MOUSEBUTTONUP:
                posx, posy = event.pos
                for pane in self.Panes:
                    #print "debug: app.event_handler", posx, posy
                    if pane.test_coordinates(posx, posy):
                        textToReturn = pane.text()
    
                        if textToReturn == "Keyboard":
                            self.keyboard.visible = True
                            self.show_panes = False
    
                        print textToReturn
            return textToReturn
    
        #---------------------------
    
        def run(self):
    
            clock = pygame.time.Clock()
    
            RUNNING = True
            while RUNNING:
    
                # --- events ---
    
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        RUNNING = False
                        break
    
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_ESCAPE:
                            self.keyboard.visible = not self.keyboard.visible
                            self.show_panes = not self.show_panes
    
    
                    if event.type == pygame.USEREVENT:
                        print "USEREVENT:", event.code
                        # event from keyboard
                        if event.code == 666:
                            # keyboar was closed so I have to show panes
                            self.show_panes = True
    
                    if self.show_panes:
                        self.event_handler(event)
    
                    if self.keyboard.visible:
                        self.keyboard.event_handler(event)
    
                # --- draws ---
    
                self.screen.fill(WHITE)
    
                if self.show_panes:
                    self.drawPanes()
    
                if self.keyboard.visible:
                    self.keyboard.draw()
    
                pygame.display.update()
    
                # --- FPS ---
    
                clock.tick(25)
    
            self.close()    
    
    #----------------------------------------------------------------------
    
    Application().run()
    

    EDIT:

    Now Pane can be clicked and moved. I put here only Pane class

    Look for: is_movable and is_moving

    class Pane():
    
        def __init__(self, textToDisplay, rect, screen, fgColor=(0,0,0), bgColor=(255,255,255), mouseoverBgColor=(255,255,0) ):
    
            self.textToDisplay = textToDisplay
            self.rect = pygame.Rect(*rect)
            self.screen = screen
    
            # colors
    
            self.fgColor = fgColor
            self.bgColor = bgColor
    
            self.mouseoverBgColor = mouseoverBgColor
    
            self.currentFgColor = self.fgColor
            self.currentBgColor = self.bgColor
    
            self.font = pygame.font.SysFont('Palace Script MT', 25)
    
            self.is_movable = True
            self.is_moving = False
        #---------------------------
    
        def coordinates(self):
            return self.rect
    
        #---------------------------
    
        def text(self):
            return self.textToDisplay
    
        #---------------------------
    
        def draw(self):
            # background
            self.screen.fill(self.currentBgColor, self.rect)
    
            # border
            pygame.draw.rect(self.screen, self.currentFgColor, self.rect, 2)
    
            # text
            self.screen.blit(self.font.render(self.textToDisplay, True, self.currentFgColor), self.rect)
    
        #---------------------------
    
        def test_coordinates(self, posx, posy):
            return self.rect.collidepoint(posx, posy)
    
        #---------------------------
    
        def event_handler(self, event):
    
            # standard pane color
            self.currentFgColor = self.fgColor
            self.currentBgColor = self.bgColor
    
            # if mouse over pane change color
    
            if event.type == pygame.MOUSEMOTION:
                if self.rect.collidepoint(event.pos):
                    self.currentBgColor = self.mouseoverBgColor
    
                    if self.is_movable and self.is_moving:
                        self.rect.move_ip(event.rel)
                        self.currentBgColor = (0,0,255)
    
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if self.rect.collidepoint(event.pos):
                    self.currentBgColor = (255,0,0)
                    self.is_moving = True
    
            elif event.type == pygame.MOUSEBUTTONUP:
                if self.rect.collidepoint(event.pos):
                    self.currentBgColor = self.mouseoverBgColor
                    self.is_moving = False
    

    EDIT:

    Clicked Pane become top-most Pane on screen:

    import pygame
    import sys
    
    #----------------------------------------------------------------------
    
    WHITE = (255,255,255)
    BLACK = (0  ,0  ,0  )
    
    #---------------------------
    
    MAIN_BUTTON = 2
    
    KEYBOARD = True
    WORDS = False
    
    #----------------------------------------------------------------------
    
    class evt_type(): # UserEvent - types
    
        PANE     = pygame.USEREVENT + 1
        KEYBOARD = pygame.USEREVENT + 2
    
    class evt_code(): # UserEvent - codes
    
       KEYBOARD_CLOSED = 100
       PANE_CLICKED = 200
    
    #----------------------------------------------------------------------
    
    class Pane():
    
        def __init__(self, textToDisplay, rect, screen, fgColor=(0,0,0), bgColor=(255,255,255), mouseoverBgColor=(255,255,0) ):
    
            self.textToDisplay = textToDisplay
            self.rect = pygame.Rect(*rect)
            self.screen = screen
    
            # colors
    
            self.fgColor = fgColor
            self.bgColor = bgColor
    
            self.mouseoverBgColor = mouseoverBgColor
    
            self.currentFgColor = self.fgColor
            self.currentBgColor = self.bgColor
    
            self.font = pygame.font.SysFont('Palace Script MT', 25)
    
            self.is_movable = True
            self.is_moving = False
        #---------------------------
    
        def coordinates(self):
            return self.rect
    
        #---------------------------
    
        def text(self):
            return self.textToDisplay
    
        #---------------------------
    
        def draw(self):
            # background
            self.screen.fill(self.currentBgColor, self.rect)
    
            # border
            pygame.draw.rect(self.screen, self.currentFgColor, self.rect, 2)
    
            # text
            self.screen.blit(self.font.render(self.textToDisplay, True, self.currentFgColor), self.rect)
    
        #---------------------------
    
        def test_coordinates(self, posx, posy):
            return self.rect.collidepoint(posx, posy)
    
        #---------------------------
    
        def event_handler(self, event):
    
            # standard pane color
            self.currentFgColor = self.fgColor
            self.currentBgColor = self.bgColor
    
            # if mouse over pane change color
    
            if event.type == pygame.MOUSEMOTION:
                if self.rect.collidepoint(event.pos):
                    self.currentBgColor = self.mouseoverBgColor
    
                    if self.is_movable and self.is_moving:
                        self.rect.move_ip(event.rel)
                        self.currentBgColor = (0,0,255)
    
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if self.rect.collidepoint(event.pos):
                    if self.is_movable:
                        pygame.event.post(pygame.event.Event(evt_type.PANE, {'code': evt_code.PANE_CLICKED, 'widget': self}))
                    self.currentBgColor = (255,0,0)
                    self.is_moving = True
    
            elif event.type == pygame.MOUSEBUTTONUP:
                if self.rect.collidepoint(event.pos):
                    self.currentBgColor = self.mouseoverBgColor
                self.is_moving = False
    
    
    #----------------------------------------------------------------------
    
    class Keyboard():
    
        Keys = []
        paneLocs = []
        visible = False
        #---------------------------
    
        def __init__(self, screen):
    
            self.screen = screen
            self.font = pygame.font.SysFont('Arial', 25)
    
            self.create()
    
        #---------------------------
    
        def create(self):
    
            letter_code = ord("A")
    
            for y in range(2):
                for x in range(13):
                    lock = (100*x, 100*y, 100, 100)
                    self.paneLocs.append(lock)
                    self.Keys.append(Pane(chr(letter_code), lock, self.screen, (0,0,0), (0,255,0)))
                    self.Keys[-1].is_movable = False
                    letter_code += 1
    
        #---------------------------
    
        def draw(self):
            for key in self.Keys:
                key.draw()
    
        #---------------------------
    
        def event_handler(self, event):
    
            textToReturn = None
    
            # let panes handle event
            for key in self.Keys:
                key.event_handler(event)
    
            if event.type == pygame.MOUSEBUTTONUP:
                posx, posy = event.pos
                for key in self.Keys:
                    #print "debug: keyboard.event_handler", posx, posy
                    if key.test_coordinates(posx, posy):
                        textToReturn = key.text()       
    
                        print textToReturn
    
                # if Q was 
                if textToReturn == 'Q':
                    self.onKeyQ(textToReturn)
    
            return textToReturn
    
        #---------------------------
    
        def onKeyQ(self, text):
    
            # hide keyboard
            self.visible = False
    
            # send event to inform main loop that keyboard was closed
            pygame.event.post(pygame.event.Event(evt_type.KEYBOARD, {'code': evt_code.KEYBOARD_CLOSED, 'key':text}))
    
    #----------------------------------------------------------------------
    
    class Application():
    
        NoOfPanes = 0
        Panes = []
    
        paneLocs = [(583, 334, 300, 150), 
                    (633, 150, 200, 125), 
                    (633, 600, 200, 125), 
                    (350, 360, 200, 100), 
                    (925, 360, 200, 100), 
                    (1000, 150, 150, 100), 
                    (275, 150, 150, 100), 
                    (275, 600, 150, 100), 
                    (1000, 600, 150, 100), 
                    (75, 350, 200, 100),
                    (0, 668, 200, 100)
                    ]    
    
        #---------------------------
    
        def __init__(self):
    
            pygame.init()
            pygame.display.set_caption('Box Test')
    
            self.font = pygame.font.SysFont('Arial', 25)
            self.screen = pygame.display.set_mode((1366,768), 0, 32)
    
            self.show_panes = True
    
            self.createPanes()
    
            self.keyboard = Keyboard(self.screen)
    
        #---------------------------
    
        def close(self):
            print "pygame quit"
            pygame.quit()
            sys.exit()
    
        #---------------------------
    
        def createPanes(self):
            self.addPane("1", WORDS)
            self.addPane("2", WORDS)
            self.addPane("3", WORDS)
            self.addPane("4", WORDS)
            self.addPane("5", WORDS)
            self.addPane("6", WORDS)
            self.addPane("7", WORDS)
            self.addPane("8", WORDS)
            self.addPane("9", WORDS)
            self.addPane("10", WORDS)
            self.addPane("Keyboard", WORDS)
    
        #---------------------------
    
        def addPane(self, textToDisplay, keyboardFlag):      
            if (not keyboardFlag) and (self.NoOfPanes > 11):
                print("Limit Reached")            
            else:
                print(int(self.NoOfPanes))
    
                if not keyboardFlag:
                    self.Panes.append(Pane(textToDisplay, self.paneLocs[self.NoOfPanes], self.screen))
                    self.NoOfPanes += 1
    
        #---------------------------
    
        def drawPanes(self):
            for Pane in self.Panes:
                Pane.draw()
    
        #---------------------------
    
        def mousePosition(self, event):
                if event.type == MAIN_BUTTON:
                    self.Pos = event.pos
                    return MAIN_BUTTON
                else:
                    return False
    
        #---------------------------
    
        def event_handler(self, event):
    
            textToReturn = None
    
            # let panes handle events
            for pane in self.Panes:
                pane.event_handler(event)
    
            if event.type == pygame.MOUSEBUTTONUP:
                posx, posy = event.pos
                for pane in self.Panes:
                    #print "debug: app.event_handler", posx, posy
                    if pane.test_coordinates(posx, posy):
                        textToReturn = pane.text()
    
                        if textToReturn == "Keyboard":
                            self.keyboard.visible = True
                            self.show_panes = False
    
                        print textToReturn
            return textToReturn
    
        #---------------------------
    
        def run(self):
    
            clock = pygame.time.Clock()
    
            RUNNING = True
            while RUNNING:
    
                # --- events ---
    
                for event in pygame.event.get():
                    if event.type == pygame.QUIT:
                        RUNNING = False
                        break
    
                    if event.type == pygame.KEYDOWN:
                        if event.key == pygame.K_ESCAPE:
                            self.keyboard.visible = not self.keyboard.visible
                            self.show_panes = not self.show_panes
    
    
                    if event.type == evt_type.KEYBOARD:
                        print "USEREVENT:: TYPE:", event.type, "CODE:", event.code
                        # event from keyboard
                        if event.code == evt_code.KEYBOARD_CLOSED:
                            # keyboar was closed so I have to show panes
                            self.show_panes = True
    
                    if event.type == evt_type.PANE:
                        print "USEREVENT:: TYPE:", event.type, "CODE:", event.code
                        if event.code == evt_code.PANE_CLICKED:
                            # move to the end - move to top of screen
                            index = self.Panes.index(event.widget)
                            self.Panes.append(self.Panes.pop(index))
    
                    if self.show_panes:
                        self.event_handler(event)
    
                    if self.keyboard.visible:
                        self.keyboard.event_handler(event)
    
                # --- draws ---
    
                self.screen.fill(WHITE)
    
                if self.show_panes:
                    self.drawPanes()
    
                if self.keyboard.visible:
                    self.keyboard.draw()
    
                pygame.display.update()
    
                # --- FPS ---
    
                clock.tick(25)
    
            self.close()    
    
    #----------------------------------------------------------------------
    
    Application().run()