Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无法初始化pygame_Python_Raspberry Pi_Pygame - Fatal编程技术网

Python 无法初始化pygame

Python 无法初始化pygame,python,raspberry-pi,pygame,Python,Raspberry Pi,Pygame,我正试图用我的raspberry pi在PiTFT 2.8英寸显示屏上创建一个按钮。如果我在没有sudo的情况下运行代码,则会弹出一个窗口,它看起来正常。我用sudo运行代码,它应该会出现在PiTFT显示屏上,但它会在pygame.init()函数中冻结 import pygame from pygame.locals import * import os class Button: def create_button(self, surface, color, x, y, length, h

我正试图用我的raspberry pi在PiTFT 2.8英寸显示屏上创建一个按钮。如果我在没有sudo的情况下运行代码,则会弹出一个窗口,它看起来正常。我用sudo运行代码,它应该会出现在PiTFT显示屏上,但它会在
pygame.init()函数中冻结

import pygame
from pygame.locals import *
import os

class Button:
def create_button(self, surface, color, x, y, length, height, width, text, text_color):
    surface = self.draw_button(surface, color, length, height, x, y, width)
    surface = self.write_text(surface, text, text_color, length, height, x, y)
    self.rect = pygame.Rect(x,y, length, height)
    return surface

def write_text(self, surface, text, text_color, length, height, x, y):
    font_size = int(length//len(text))
    myFont = pygame.font.SysFont("Calibri", font_size)
    myText = myFont.render(text, 1, text_color)
    surface.blit(myText, ((x+length/2) - myText.get_width()/2, (y+height/2) - myText.get_height()/2))
    return surface

def draw_button(self, surface, color, length, height, x, y, width):           
    for i in range(1,10):
        s = pygame.Surface((length+(i*2),height+(i*2)))
        s.fill(color)
        alpha = (255/(i+2))
        if alpha <= 0:
            alpha = 1
        s.set_alpha(alpha)
        pygame.draw.rect(s, color, (x-i,y-i,length+i,height+i), width)
        surface.blit(s, (x-i,y-i))
    pygame.draw.rect(surface, color, (x,y,length,height), 0)
    pygame.draw.rect(surface, (190,190,190), (x,y,length,height), 1)  
    return surface

def pressed(self, mouse):
    if mouse[0] > self.rect.topleft[0]:
        if mouse[1] > self.rect.topleft[1]:
            if mouse[0] < self.rect.bottomright[0]:
                if mouse[1] < self.rect.bottomright[1]:
                    print "Some button was pressed!"
                    return True
                else: return False
            else: return False
        else: return False
    else: return False


os.putenv('SDL_FBDEV', '/dev/fb1')
os.putenv('SDL_MOUSEDRV', 'TSLIB')
os.putenv('SDL_MOUSEDEV', '/dev/input/touchscreen')

pygame.init()
print "got here"
pygame.mouse.set_visible(False)
screen = pygame.display.set_mode((320, 240))
screen.fill((0,0,0))
pygame.display.update()
font_big = pygame.font.Font(None, 20)
print "here"
pygame.display.set_caption("Buttons.py - example")
button = Button()
screen.fill((30,144,255))
#Parameters:               surface,      color,       x,   y,           length, height, width,    text,      text_color
button.create_button(screen, (107,142,35), 100, 135, 100,    50,    0,            "Example", (255,255,255))
pygame.display.flip()
pygame.display.update()
导入pygame
从pygame.locals导入*
导入操作系统
类别按钮:
def create_按钮(自身、表面、颜色、x、y、长度、高度、宽度、文本、文本_颜色):
表面=自绘制按钮(表面、颜色、长度、高度、x、y、宽度)
表面=自身。写入文字(表面、文字、文字颜色、长度、高度、x、y)
self.rect=pygame.rect(x,y,长度,高度)
返回面
def写入文字(自身、表面、文字、文字颜色、长度、高度、x、y):
font\u size=int(长度//长度(文本))
myFont=pygame.font.SysFont(“Calibri”,字体大小)
myText=myFont.render(文本,1,文本颜色)
blit(myText,((x+length/2)-myText.get_-width()/2,(y+height/2)-myText.get_-height()/2))
返回面
def draw_按钮(自身、表面、颜色、长度、高度、x、y、宽度):
对于范围(1,10)内的i:
s=pygame.Surface((长度+(i*2),高度+(i*2)))
s、 填充(颜色)
α=(255/(i+2))
如果alpha self.rect.topleft[0]:
如果鼠标[1]>self.rect.topleft[1]:
如果鼠标[0]我的设备太忙了,这就是它无法初始化的原因。请考虑为将来的任何人张贴自己的答案。