我正在一个项目中尝试在picamera上的相机预览和使用pygame窗口在屏幕上显示一些文本之间切换。我已经可以打开picamera,然后输入一些文本,然后再次打开picamera,但是当我尝试打开pygame窗口获取更多文本时,出现了分割错误。
我认为主要问题是退出pygame窗口,而没有退出打开另一个pygame窗口所需的其他事情。像sys.exit和pygame.quit这样的命令似乎可以使很多事情退出。我尝试了一些替代方法,例如将文本放入while循环中,然后在最后将循环设为false,以便它无需实际的quit命令即可关闭窗口,但这似乎并未真正关闭任何东西。直到我第二次尝试初始化pygame时,代码才能正常工作。那时,它给了我分段错误,并在python空闲时打开了一个新窗口,其中包含了许多我未编写的其他代码。
pygame.init()
white = (255, 255, 255)
green = (0, 255, 0)
blue= (0, 0, 128)
black = (0, 0, 0)
display_surface = pygame.display.set_mode((1350,800))
pygame.display.set_caption(' ')
camera()
font = pygame.font.Font('freesansbold.ttf', 30)
text = font.render('You', True, black, white)
textRect = text.get_rect()
textRect.center = (1350//2, 800//2)
display_surface.fill(white)
display_surface.blit(text, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
time.sleep(1)
pygame.quit()
camera()
pygame.init()
white = (255, 255, 255)
green = (0, 255, 0)
blue= (0, 0, 128)
black = (0, 0, 0)
display_surface = pygame.display.set_mode((1350,800))
pygame.display.set_caption(' ')
font = pygame.font.Font('freesansbold.ttf', 30)
text = font.render('test', True, black, white)
textRect = text.get_rect()
textRect.center = (1350//2, 800//2)
display_surface.fill(white)
display_surface.blit(text, textRect)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
pygame.display.update()
time.sleep(1)
我想做的是在picamera和文本之间再切换几次,因此,如果我能弄清楚这次如何解决它,那么我只需要复制并粘贴一些代码即可。得到下一个迭代。我是编码的新手。
答案 0 :(得分:2)
好的。所以我犯了一个愚蠢的错误。由于某种原因,问题是初始化pygame两次;我仍然不了解,因为if (a < b< c) // equivalent to if(0<c) due to left associativity// firstly a<b will be evaluated which 0 and hence statement is true as c is 1.
{
if (c > b > a) // equiavelnt to if(bool(c>b)>a) which is false as c>b is 0 hence it will reduce to if(0>c) .execution goes to else block.
{
if (a > c)
{
cout << 84;
}
else
{
cout << 48;
}
}
else
{
if (b < c) // it is false. execution goes to else
{
cout << 14;
}
else
{
cout << 41; // it is printed.
}
}
应该已经退出了。但是我只是删除了第二个pygame.quit()
,而是用pygame.init()
替换了第一个pygame.quit()
。