[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] getting what an user has written
- To: pygame-users@xxxxxxxx
- Subject: Re: [pygame] getting what an user has written
- From: "Santiago Romero" <nop.compiler@xxxxxxxxx>
- Date: Tue, 22 Jan 2008 13:57:38 +0100
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: pygame-users-outgoing@xxxxxxxx
- Delivered-to: pygame-users@xxxxxxxx
- Delivery-date: Tue, 22 Jan 2008 07:57:46 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed;        d=gmail.com; s=gamma;        h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth;        bh=qTVMKtPgv5XRt/vjks0kyXwqa3IGDRrCfjWMHuhdKms=;        b=VmGZiAlGJMR7Q+s1lk/JZ9jhwsHzMsZBv69Xl+7hpAVdwy1YxyHUT8Laubeg3cFHjmA3xjPIrdtUlOhKqbkwVKUByNQzVr3Q/3aOHyb7rwNCWZTMoCmbop8FQoawWZJ5igsGQ/okeszP01U9LaWxIr7nkNkFflRM2Hedy3Ue/S0=
- Domainkey-signature: a=rsa-sha1; c=nofws;        d=gmail.com; s=gamma;        h=message-id:date:from:sender:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references:x-google-sender-auth;        b=lCVsRzSx0PFo+kTXmd+YEm5ttma7TW63YxVCrgAQKac+RmxyPhxYnB2cqN1aGrqwaA9sKkwmuxn69XPfKsiznowfntzHoNSCZ8ODBZTRK8sHdxGJ7EUFA7BQEayG4Rurkor/KtwtGRzu0oQLj0TXXs26BLzsEAAlnQs7FTd2Wvw=
- In-reply-to: <114.183-17476-1330468310-1201006381@xxxxxxxxx>
- References: <114.183-17476-1330468310-1201006381@xxxxxxxxx>
- Reply-to: pygame-users@xxxxxxxx
- Sender: owner-pygame-users@xxxxxxxx
> Hello. I use this script in a class to get the string what user had written:
> string.string=u""
> strfont=pygame.font.Font(None,16)
> for udalost in pygame.event.get():
>           if udalost.type == KEYDOWN:
>             if udalost.key == K_RETURN: exit=1
>             elif udalost.key == K_BACKSPACE:
>               self.string=self.string[:-1]
>               text=strfont.render(self.string,0,(0,0,0))
>             elif not strfont.size(self.string+udalost.unicode)[0]>mw-8:
>               self.string+=udalost.unicode
>               text=strfont.render(self.string,0,(0,0,0))
 Well ... I wrote this, but surely is not the right way to do it,
because I started working with python+pygame 2-3 weeks ago:
   # Loop to get keystrokes and insert them into the string
   done = 0
   mystring = ""
   while not done:
      clock.tick(20)
      text = font.render( str(mystring) + "_", 1, (180,180,0))
      newsize = text.get_rect()
      screen.fill( bg, [tx+textsize.w, ty, w-textsize.w-32, newsize.h+2] )
      screen.blit( text, (tx+textsize.w, ty) )
      pygame.display.flip()
      for event in pygame.event.get():
         if event.type == KEYDOWN:
            key = event.key
            if key == K_ESCAPE: done = 1
            # Delete key
            elif key == K_BACKSPACE and mystring != []:
               mystring = mystring[:-1]
            # Enter: check if the string is valid
            elif key == K_RETURN and mystring != [] :
                  value, done = mystring, 1
            # Any other key: numbers, letters...
            else:
               the_key = event.unicode
               ascii = ""
               if the_key.isalnum() \
                  or the_key in ( '.', '_', '-', ' ' ):
                     ascii = the_key.encode('latin1')
               if len(mystring) < string_limit and ascii != "":
                  mystring = mystring[:] + ascii
 Hope that helps.
-- 
Santiago Romero (NoP/Compiler)
KUbuntu GNU/Linux
http://www.sromero.org