我有以下代码。这基本上从文件夹应用程序及其子文件夹中获取所有图像。我的问题是我试图在所有图像上添加一个点击事件来做同样的事情。基本上" exec("apps/" + apps[app_count] + "/app.py"
)"
# -*- 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()
这是检查文件夹中所有内容的代码的一部分&#34; apps&#34;
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
并附加每个文件夹中的所有图像。我希望每次点击图标,执行它&#34; app.py&#34;在每个应用程序文件夹中都有两个文件:&#34; app.png&#34;和&#34; app.py&#34;。
答案 0 :(得分:0)
您可以在apps
列表中添加每个图片的坐标,然后使用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")
您所要做的就是定义图标的宽度和高度(如果每个应用程序的内容与pygame.Surface.get_size()
不一致),并替换最后一行用正确的语法。在Windows上你可以使用:
os.system("apps\\"+a[0]+"\\app.py") # of course you should import os first