PyGame and touchscreen

2019-04-15 23:09发布

问题:

I've made a pygame GUI interface with buttons who are activated when the user click on them. The GUI works well when i'm using my real mouse but when I run the GUI on the PITFT (https://www.adafruit.com/product/1601 ), buttons do not respond to clicks ( even if i use a harder material than my finger like a stylus ).

So the question is : Are Pygame click event compatible with the PITFT or are there a kind of "special" event made for it ?

Here is the current mouse event i use in my code :

def run(self):
  """Lance la boucle principale pour gérer les événements
  """
  while True:
     event = pygame.event.wait()

     if event.type == MOUSEBUTTONDOWN and event.button == 1 and not self.keep_level_2:
        self.click(event.pos)

     elif event.type == MOUSEBUTTONUP and event.button == 1:
        self.release(event.pos)`

I shearch on the internet and found that in order to use the PiTft, you need to add the following lines :

os.environ['SDL_VIDEODRIVER'] = 'fbcon'
os.environ["SDL_FBDEV"] = "/dev/fb1"
os.environ["SDL_MOUSEDEV"] = "/dev/input/touchscreen"
os.environ["SDL_MOUSEDRV"] = "TSLIB"

So i've tried adding them but the touchscreen is still not responding.

Also, when to program run, i can't quit it ( CTRL C and escape don't work ).

Note : My code is not running in python 3

回答1:

I think what's happening is you're doing a button up, button down methond.

I get my pygame GUI to work by going...

click = pygame.mouse.get_pressed()

if x+w > mouse_pos[0] > x and y+h > mouse_pos[1] > y:
    pygame.draw.rect(screen, ac,(x,y,w,h))
    if click[0] == 1 and action != None:
            action()

HOWEVER for me this creates other issues because I don't think that 1 tap on the touch screen is a SMOOTH single click. What happens for me is that my button will click, then it will for no apparent reason do a second 'click' (I think because of 'fuzz' in the click).

As a result, my 'on' buttons turn on (they work perfectly) and then they turn off. Not quite sure what to do about that yet, but hopefully it gets your project working.



回答2:

Did you check that your /dev/input/touchscreen is "linked" to the right module? Check evtest /dev/input/touchscreen and check if it does anything when using the screen, or the keyboard, or the mouse!

If this is happening you should follow the install procedure again.