2

当我查看 Dropbox 上传器https://github.com/andreafabrizi/Dropbox-Uploader时,也许我错过了一些东西。我正在创建一个 python 脚本,该脚本从插入 RPi 的 USB 上传文件,但需要它,所以当上传完成时,布尔值更改为 false。有没有办法检测到这一点?在我希望发生这种情况的地方对代码进行了注释。

import pygame
from pygame.locals import *
from subprocess import call

uploadSym = pygame.image.load('uploadsymbol.png')
loadingSym = pygame.image.load('loading.png')
uploadSym = pygame.transform.scale(uploadSym, (250,300))
loadingSym = pygame.transform.scale(loadingSym, (300,300))
rect = uploadSym.get_rect()
rect = rect.move((200,60))
rect = loadingSym.get_rect()
rect = rect.move((200,60))
firstSlide = False

def toggle_fullscreen():
    screen = pygame.display.get_surface()
    tmp = screen.convert()
    caption = pygame.display.get_caption()
    cursor = pygame.mouse.get_cursor()  
    w,h = screen.get_width(),screen.get_height()
    flags = screen.get_flags()
    bits = screen.get_bitsize()

    pygame.display.quit()
    pygame.display.init()

    screen = pygame.display.set_mode((w,h),flags^FULLSCREEN,bits)
    screen.blit(tmp,(0,0))
    pygame.display.set_caption(*caption)
    pygame.key.set_mods(0)
    pygame.mouse.set_cursor( *cursor )

    return screen

if __name__ == '__main__':
    SW,SH = 640,480
    screen = pygame.display.set_mode((SW,SH),pygame.NOFRAME)
    pygame.display.set_caption('Uploader')

    _quit = False
    while not _quit:
        for e in pygame.event.get():
            if (e.type is KEYDOWN and e.key == K_RETURN and (e.mod&(KMOD_LALT|KMOD_RALT)) != 0):
                toggle_fullscreen()
            elif e.type is QUIT:
                _quit = True
            elif e.type is KEYDOWN and e.key == K_ESCAPE:
                _quit = True

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()  
    screen = pygame.display.get_surface()
    screen.fill([255, 255, 255])    

    #Click within the given area and upload begins  
    if 450 > mouse[0] > 250 and 360 > mouse[1] > 60 and click[0] == 1:
        firstSlide = True
    #T/F keeps png up for new slide
    elif firstSlide == True:
        loadRot = pygame.transform.rotate(loadingSym,0)
        screen.blit(loadRot, rect)

        #Upload test file to dropbox
        Upload = "/home/pi/Uploader/Dropbox-Uploader/dropbox_uploader.sh upload loading.png /"
        call ([Upload], shell=True)
        #Here I need an if statement that says, when upload done firstSlide = False
        #{
        #
        #
        #}

    else:
        screen.blit(uploadSym, rect)
    pygame.display.update()
4

1 回答 1

2

subprocess.call() 函数返回命令的退出代码。所以call()意志(应该)0在成功时返回,在错误时非零。

所以:

uploaded_ok = ( subprocess.call( Upload, shell=True) == 0 )

会给你一个真/假的成功。

注意:链接到 2.7 doco,因为 OP 标记了问题 Python2.7

于 2019-09-17T22:28:29.420 回答