Python/Pygame的问题 导入上下文库 使用contextlib.redirect\u stdout(无): 导入pygame 导入操作系统 导入时间 #定义一些颜色 黑色=(0,0,0) 白色=(255,255,255) 绿色=(0,255,0) 红色=(255,0,0) 蓝色=(0,0255) z=0 b=0 h=0 pygame.init() 图像=[] 路径='E:\Andrew Photos' pub=open(“public.txt”,“w+”) 现在=打开(“nowhere.txt”,“w+”) sch=开放(“school.txt”,“w+”) 文件=[] 对于操作系统中的r、d、f.walk(路径): 对于f中的文件: 如果文件中有“.jpg”: files.append(os.path.join(r,file)) 打印(文件) 公共=[] 学校=[] 无处=[] #设置屏幕的宽度和高度[宽度,高度] 大小=(700500) screen=pygame.display.set_模式((0,0),pygame.FULLSCREEN) x、 y=屏幕。获取大小() pygame.display.set_标题(“暴躁的测试人员”) #循环,直到用户单击关闭按钮。 完成=错误 i=0 #用于管理屏幕更新的速度 clock=pygame.time.clock() 单击=0 #-----主程序循环----------- 虽然没有这样做: x、 y=屏幕。获取大小() 而我=x-50: 如果鼠标=x-300: 如果鼠标>=y-100: 无处。追加(文件[i]) now.write(文件[i]) i=i+1 elif mousex=y-100: pub.write(文件[i]) i=i+1 public.append(文件[i-1]) elif mousex=1100: 如果鼠标>=y-100: school.append(文件[i]) sch.write(文件[i]) i=1+1 单击=1 如果i==len(文件): pygame.QUIT() #---游戏逻辑应该放在这里 #---屏幕清除代码在这里 #在这里,我们将屏幕清除为白色。不要放置其他绘图命令 #否则,它们将被此命令擦除。 #如果您想要背景图像,请将此清除替换为blit'ing #背景图像。 屏幕填充(白色) 如果i>=len(文件): pygame.退出 #pygame.draw.rect(屏幕,红色,(x-50,0,50,30)) 如果我=y-100: #如果单击==1: #无处。追加(文件[i]) #时间。睡眠(1) #如果单击==0: #i=i+1 #---继续,用我们绘制的内容更新屏幕。 pygame.display.flip() #---限制为每秒60帧 #关闭窗口并退出。 时钟滴答(60) pygame.QUIT() 退出()

Python/Pygame的问题 导入上下文库 使用contextlib.redirect\u stdout(无): 导入pygame 导入操作系统 导入时间 #定义一些颜色 黑色=(0,0,0) 白色=(255,255,255) 绿色=(0,255,0) 红色=(255,0,0) 蓝色=(0,0255) z=0 b=0 h=0 pygame.init() 图像=[] 路径='E:\Andrew Photos' pub=open(“public.txt”,“w+”) 现在=打开(“nowhere.txt”,“w+”) sch=开放(“school.txt”,“w+”) 文件=[] 对于操作系统中的r、d、f.walk(路径): 对于f中的文件: 如果文件中有“.jpg”: files.append(os.path.join(r,file)) 打印(文件) 公共=[] 学校=[] 无处=[] #设置屏幕的宽度和高度[宽度,高度] 大小=(700500) screen=pygame.display.set_模式((0,0),pygame.FULLSCREEN) x、 y=屏幕。获取大小() pygame.display.set_标题(“暴躁的测试人员”) #循环,直到用户单击关闭按钮。 完成=错误 i=0 #用于管理屏幕更新的速度 clock=pygame.time.clock() 单击=0 #-----主程序循环----------- 虽然没有这样做: x、 y=屏幕。获取大小() 而我=x-50: 如果鼠标=x-300: 如果鼠标>=y-100: 无处。追加(文件[i]) now.write(文件[i]) i=i+1 elif mousex=y-100: pub.write(文件[i]) i=i+1 public.append(文件[i-1]) elif mousex=1100: 如果鼠标>=y-100: school.append(文件[i]) sch.write(文件[i]) i=1+1 单击=1 如果i==len(文件): pygame.QUIT() #---游戏逻辑应该放在这里 #---屏幕清除代码在这里 #在这里,我们将屏幕清除为白色。不要放置其他绘图命令 #否则,它们将被此命令擦除。 #如果您想要背景图像,请将此清除替换为blit'ing #背景图像。 屏幕填充(白色) 如果i>=len(文件): pygame.退出 #pygame.draw.rect(屏幕,红色,(x-50,0,50,30)) 如果我=y-100: #如果单击==1: #无处。追加(文件[i]) #时间。睡眠(1) #如果单击==0: #i=i+1 #---继续,用我们绘制的内容更新屏幕。 pygame.display.flip() #---限制为每秒60帧 #关闭窗口并退出。 时钟滴答(60) pygame.QUIT() 退出(),python,pygame,Python,Pygame,我正在尝试这样做,当你点击底部的方块时,它会添加到三个单独的文本文件中,每个框中的文件名,但是我有一些问题,我已经能够解决大多数问题,但无法修复它们 第一次单击不起作用。 蓝色矩形根本不起作用 文件中根本没有任何内容。我会满足于在同一个文件中使用标题和列表之间的间隙 目前我正在使用4个随机的JPG.s来完成这项工作,但目标是让它成为我可以对2000个进行排序的工具。好的。所以我是在窗口模式而不是全屏模式下,但我认为问题是if语句中的一个错误,你有一个“我不喜欢2个while循环”。如果i>=le

我正在尝试这样做,当你点击底部的方块时,它会添加到三个单独的文本文件中,每个框中的文件名,但是我有一些问题,我已经能够解决大多数问题,但无法修复它们

第一次单击不起作用。 蓝色矩形根本不起作用 文件中根本没有任何内容。我会满足于在同一个文件中使用标题和列表之间的间隙


目前我正在使用4个随机的JPG.s来完成这项工作,但目标是让它成为我可以对2000个进行排序的工具。

好的。所以我是在窗口模式而不是全屏模式下,但我认为问题是if语句中的一个错误,你有一个“我不喜欢2个while循环”。如果i>=len(files):done=true,你可以在未完成时使用
,也可以使用
,你能解释一下什么是有效的吗?我建议把打印出来的声明放在里面,看看哪个部分不起作用。@TheBigKahuna它就在那里,好像有人把它拿走了?无论使用哪个框,abox中的第一次单击都不起作用。单击时蓝色框不起作用。它没有输出到文件。老实说,我以为我已经删除了而没有复制和粘贴你的代码,把它改成了一个循环,摆脱了图像和文件编写,按钮也正常工作。所以试试一个循环,mabyeI也这么做了,但由于某种原因,中间的按钮对我来说仍然没有作用。我不知道是什么原因导致该条件不正确@TheBigKahunat修复了该问题,我现在应该能够通过文件输出修复该错误,非常感谢!
import contextlib
with contextlib.redirect_stdout(None):
    import pygame
    import os
    import time
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0,0,255)
z = 0
b = 0
h= 0

pygame.init()
image = []
path = 'E:\Andrew Photos'
pub= open("public.txt","w+")
now = open("nowhere.txt","w+")
sch = open("school.txt","w+")
files = []
for r, d, f in os.walk(path):
    for file in f:
        if '.jpg' in file:
            files.append(os.path.join(r, file))
            print (file)
public = []
school = []
nowhere = []
# Set the width and height of the screen [width, height]
size = (700, 500)
screen = pygame.display.set_mode((0,0),pygame.FULLSCREEN)
x, y = screen.get_size() 
pygame.display.set_caption("Testy Testerson")

# Loop until the user clicks the close button.
done = False
i =0
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
click = 0
# -------- Main Program Loop -----------
while not done:
      x, y = screen.get_size() 
      while i < len(files):
            mousex, mousey = pygame.mouse.get_pos()
          # --- Main event loop
            for event in pygame.event.get():
              if event.type == pygame.QUIT:
                  done = True
            # if any mouse button is pressed pygame.MOUSEBUTTONUP
              if event.type == pygame.MOUSEBUTTONUP:
                    click = 0
              if event.type == pygame.MOUSEBUTTONDOWN:
                # if the left button is pressed

                if event.button == 1:
                      if mousex >= x-50: 
                            if mousey <= 30:
                                  exit(1)
                                  pygame.quit()
                                  done = True


                      if mousex >= x-300:
                            if mousey >= y-100:
                                  nowhere.append(files[i])
                                  now.write(files[i])
                                  i = i +1

                      elif mousex <= 300:
                            if mousey >=y-100:
                                  pub.write(files[i])
                                  i = i+1
                                  public.append(files[i-1])

                      elif mousex <=800:
                            if mousex >= 1100:
                                  if mousey >= y-100:
                                        school.append(files[i])
                                        sch.write(files[i])
                                        i = 1 +1


                      click = 1
                      if i == len(files):
                            pygame.QUIT()
          # --- Game logic should go here

          # --- Screen-clearing code goes here


          # Here, we clear the screen to white. Don't put other drawing commands
          # above this, or they will be erased with this command.

          # If you want a background image, replace this clear with blit'ing the
          # background image.
            screen.fill(WHITE)
            if i >= len(files):
                  pygame.QUIT
            #pygame.draw.rect(screen, RED,(x-50,0,50,30))
            if i < len(files):
                  image = pygame.image.load(files[i])
            screen.blit(image, (0,0))
            pygame.draw.rect(screen, RED,(x-300,y-100,300,100))
            pygame.draw.rect(screen, GREEN,(0,y-100,300,100))
            pygame.draw.rect(screen, BLUE,(800,y-100,300,100))
          # --- Drawing code should go here
         #   i = 0
       #     while i < len(files):

       #           image = pygame.image.load(files[i])
       #           screen.blit(image, (0,0))

       #           pygame.draw.rect(screen, RED,(x-300,y-100,300,100))
       #           pygame.display.flip()
       #           if mousex >= x-300:
        #                if mousey >= y-100:
         #                     if click == 1:
          #                          nowhere.append(files[i])
           #                         time.sleep(1)
            #                        if click == 0:
             #                             i = i+1



          # --- Go ahead and update the screen with what we've drawn.
            pygame.display.flip()

          # --- Limit to 60 frames per second

# Close the window and quit.
            clock.tick(60)

pygame.QUIT()
exit()
                if mousex >= x-300:
                    if mousey >= y-100:
                        print("red")
                        i = i +1

                elif mousex <= 300:
                    if mousey >=y-100:
                        print("green")
                        i += 1

                elif mousex >=500:
                    if mousex <= 800:
                        if mousey >= y-100: # this was also 1100 which doesnt help
                            print("blue")
                            i = 1 +1
pygame.draw.rect(screen, RED,(x-300,y-100,300,100))
pygame.draw.rect(screen, GREEN,(0,y-100,300,100))
pygame.draw.rect(screen, BLUE,(500,y-100,300,100))