1

The attached code correctly accepts numbers or letters using the restrictions (rest) but it will not accept upper case or symbols like +

There is probably something simple I have not done. I have only just started using Python-- the rest of my code seems to work correctly

'''
Text input
restrict to certain inputs
letters or numbers (rest)
'''
#  will not accept upper case or '+'

import pygame, pygame.font, pygame.event, pygame.draw, string
import sys
from pygame.locals import *


#Screen size
x_Size=600
y_Size=600

#Restrict
rest1='1234567890-+.'
rest2='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
rest3 = rest1 + rest2 # accept all

def get_key():
    while 1:
    event = pygame.event.poll()
    if event.type == KEYDOWN:
        return event.key
    else:
        pass


def display_box(screen, message, x, y, rest):
    "Print a message in a box on the screen"
    fontobject = pygame.font.Font(None,28) # font size
    
    pygame.draw.rect(screen, (255,0,0),(x, y, 250,30), 1) # red box outline
    
    if len(message) != 0:
    screen.blit(fontobject.render(message, 1, (255,0,0)),
                (x, y)) # red text 
    pygame.display.flip()

def clear_box(screen, x, y):
    pygame.draw.rect(screen,(255,255,255),(x, y, 300, 90), 0)
    pygame.display.flip()

    
def ask(screen, question, x, y, rest):
    #ask(screen, question, position x,y, restriction) answer
    pygame.font.init()
    current_string = []
    display_box(screen, (question + ": " + string.join(current_string,"")), x, y, rest)
    while 1:
    inkey = get_key()
    print inkey
    if inkey == K_BACKSPACE:
        current_string = current_string[0:-1]
        #print current_string
    elif inkey == K_RETURN:
        break
    elif inkey >= 127:
        pass

    elif chr(inkey) in rest:
        
        z = chr(inkey)
        print 'z',z
        current_string.append(chr(inkey))
    
    clear_box(screen,x,y)
    display_box(screen, (question + ": " + string.join(current_string,"")), x, y, rest)
    print string.join(current_string)
    return string.join(current_string)


def main():
    screen = pygame.display.set_mode((x_Size, y_Size))
    screen.fill((255,255,255))
    h=ask(screen,'Numbers', 40, 50, rest1)
    k=ask(screen,'Letters',40, 70, rest2)

    print h,'Numbers'
    print k,'Letters'
    
#=======================
    pygame.quit()
    sys.exit()
if __name__ == '__main__': main()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • What is the code supposed to be doing? There is a lot of code here to look through, you'll make it much easier for someone to help you if you explain the problem in a little more detail. – Haz May 07 '13 at 15:16
  • 1
    Did you try to print `inkey`? My guess is that pygame is not returning what you think for uppercase letters and other symbols, hence when you enter `M` you get an `inkey` of `x` and `chr(x) != 'M'`. – Bakuriu May 07 '13 at 15:17
  • 2
    it won't accept "capitals" because the shift key when you press it is shown as a completely different key. It sees that you are pressing shift and m not M – Chachmu May 07 '13 at 16:46
  • the programme is a module called several times to change/update values on a longer programme – user2001044 May 08 '13 at 07:23
  • I have tried to use inkey but the shift key gives a value not acceptable before I can input the letter key – user2001044 May 09 '13 at 08:01
  • In `get_key()` try changing it to check for a `KEYUP` event. Then it will catch the `KEYUP` on the letter, instead of the `KEYDOWN` on the shift key – CDspace Aug 07 '13 at 20:25
  • Considering you are making pop-up windows like this, wouldn't it be best just to use something like tkinter or wxpython? Both modules can do po-up windows just like that for you. – trevorKirkby Nov 23 '13 at 23:15

0 Answers0