2017-09-26 9 views
0

J'ai le code suivant. Ce qui obtient essentiellement toutes les images des applications de dossier et les sous-dossiers de celui-ci. Mon problème est que j'essaye d'ajouter un événement de clic à toutes les images pour faire la même chose. Au fond « exec("apps/" + apps[app_count] + "/app.py") »Comment ajouter un événement click pour les images dans Pygame?

# -*- coding: utf-8 -*- 
from pygame import * 
import os 
import pygame 
import time 
import random 
import sys 

_image_library = {} 

class SeedOS(): 

    def home(self): 
     (width, height) = (240, 320) 
     screen = pygame.display.set_mode((width, height)) 
     pygame.display.set_caption('Seed OS') 
     pygame.font.init() 
     Font30 = pygame.font.SysFont('Arial', 30) 
     WHITE = (255,255,255) 
     BLACK = (0,0,0) 
     screen.fill(WHITE) 
     apps = os.walk("apps").next()[1] 
     app_count = 0 
     icon_width = 15 
     icon_height = 0 
     max_width = 155 
     pygame.display.flip() 
     while True: 
      while app_count < len(apps): 
       print apps[app_count] 
       image = pygame.image.load("apps/" + apps[app_count] + "/app.png").convert() 
       screen.blit(image, (icon_width, icon_height)) 
       icon_width+=70 
       if icon_width > max_width: 
        icon_width = 15 
        icon_height +=70 
       app_count += 1 
      time2 = time.strftime('%H:%M:%S') 
      pygame.display.flip() 
      pygame.draw.rect(screen,BLACK,(0,290,240,30)) 
      clock = Font30.render(time2, False, WHITE) 
      screen.blit(clock,(60,288)) 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        pygame.quit() 
        sys.quit() 

phone = SeedOS() 
phone.home() 

Ceci est la partie du code qui vérifie toutes les choses dans le dossier « apps »

while app_count < len(apps): 
        print apps[app_count] 
        image = pygame.image.load("apps/" + apps[app_count] + "/app.png").convert() 
        screen.blit(image, (icon_width, icon_height)) 
        icon_width+=70 
        if icon_width > max_width: 
         icon_width = 15 
         icon_height +=70 
        app_count += 1 

et toutes les images ajoute de chaque dossier. Je veux à chaque clic d'icône, exécute c'est "app.py" comme dans chaque dossier d'application il y a deux fichiers: "app.png" et "app.py".

Répondre

0

Vous pouvez ajouter les coordonnées de chaque image dans la liste apps et ensuite utiliser ces coordonnées avec la méthode pygame.mouse.get_pos():

while True: 
      while app_count < len(apps): 
       print apps[app_count] 
       apps[app_count] = (apps[app_count], icon_width, icon_height) # Adding coordinates to the list 
       image = pygame.image.load("apps/" + apps[app_count][0] + "/app.png").convert() # Adding an index to find the image in the tuple 
       screen.blit(image, (icon_width, icon_height)) 
       icon_width+=70 
       if icon_width > max_width: 
        icon_width = 15 
        icon_height +=70 
       app_count += 1 
      time2 = time.strftime('%H:%M:%S') 
      pygame.display.flip() 
      pygame.draw.rect(screen,BLACK,(0,290,240,30)) 
      clock = Font30.render(time2, False, WHITE) 
      screen.blit(clock,(60,288)) 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        pygame.quit() 
        sys.quit() 
       if event.type == pygame.MOUSEBUTTONDOWN: 
        if event.button == 1: # MOUSEBUTTONLEFT 
         for a in apps: 
          if a[1] < pygame.mouse.get_pos()[0] < a[1]+IMAGEWIDTH and a[2] < pygame.mouse.get_pos()[1] < a[2] + IMAGEHEIGHT: 
           # Instruction to launch ("apps/" + a[0] + "/app.py") 

Tout ce que vous avez à faire est de définir est la largeur et la hauteur de vos icônes (ce que vous pouvez faire avec pygame.Surface.get_size() si ce n'est pas la même chose pour chaque application) et remplacer la dernière ligne par la bonne syntaxe. Sur les fenêtres, vous pouvez utiliser:

os.system("apps\\"+a[0]+"\\app.py") # of course you should import os first 
+0

Merci. Ça a marché comme sur des roulettes. Tu as sauvé ma journée :-) –