Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.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-将文本添加到rect的类实例_Python_Python 3.x_Text_Pygame_Rect - Fatal编程技术网

Python Pygame-将文本添加到rect的类实例

Python Pygame-将文本添加到rect的类实例,python,python-3.x,text,pygame,rect,Python,Python 3.x,Text,Pygame,Rect,我正在尝试向rect的类实例添加文本 我可以通过在类外设置“font”变量并在绘图函数内的“font.render_to”中输入所需的文本来实现这一点,但这意味着所有按钮的文本都将与类内设置的相同 我想为类实例中指定的每个按钮(button1、button2、button3)设置不同的文本,如下面的代码所示,但我得到以下错误: Traceback (most recent call last): File "path_to_file/test.py", line 30, in

我正在尝试向rect的类实例添加文本

我可以通过在类外设置“font”变量并在绘图函数内的“font.render_to”中输入所需的文本来实现这一点,但这意味着所有按钮的文本都将与类内设置的相同

我想为类实例中指定的每个按钮(button1、button2、button3)设置不同的文本,如下面的代码所示,但我得到以下错误:

Traceback (most recent call last):
File "path_to_file/test.py", line 30, in <module>
button1 = Button(10, 10, 100, 50, 'One')
File "path_to_file/test.py", line 23, in __init__
self.font = font.pygame.freetype.SysFont('Arial', 25)
AttributeError: 'str' object has no attribute 'pygame'.

构造函数的第五个参数是文本。必须将文本存储在属性中并渲染文本。
font.pygame.freetype.SysFont('Arial',25)
没有任何意义。字体对象由pygame.freetype.SysFont('Arial',25)构造。
render_to
的目标位置取决于按钮的位置:

class按钮:
定义初始值(self、rect_x、rect_y、rect_宽度、rect_高度、文本):
self.rect_x=rect_x
self.rect_y=rect_y
self.rect\u width=rect\u width
self.rect\u height=rect\u height
self.text=文本
self.font=pygame.freetype.SysFont('Arial',25)
def牵引(自):
pygame.draw.rect(屏幕,(200200200200),
(self.rect_x、self.rect_y、self.rect_宽度、self.rect_高度))
self.font.render_to(屏幕,(self.rect_x+42,self.rect_y+25),self.text,背景)

也许您应该将
font
参数重命名为
text
;然后就应该清楚你做错了什么(尝试对font对象和文本值使用属性
font
)。您正在执行的是
self.font='One'.pygame.freetype.SysFont('Arial',25)
,这意味着str(在本例中为'One')没有pygame属性。正如@sloth所建议的,您应该使用变量名
text
self.text
来存储字符串,并使用
self.font
来存储字体。您还必须在
绘图
功能中对其进行更改。工作正常,谢谢@rabbi76
import pygame
import sys
import pygame.freetype


pygame.display.init()
pygame.freetype.init()
width = 300
height = 350
bg = (255, 255, 255)

# Sets the window size
screen = pygame.display.set_mode((width, height))

# Sets the background colour to white
screen.fill(bg)


# Button class - All buttons are created using the below parameters
class Button:

    def __init__(self, rect_x, rect_y, rect_width, rect_height, font):
        self.rect_x = rect_x
        self.rect_y = rect_y
        self.rect_width = rect_width
        self.rect_height = rect_height
        self.font = font.pygame.freetype.SysFont('Arial', 25)

# Draw function - Creates the rectangle and adds text
    def draw(self):
        pygame.draw.rect(screen, (200, 200, 200), (self.rect_x, self.rect_y, self.rect_width, self.rect_height))
        self.font.render_to(screen, (42, 25), self.font, bg)


# Class instances - Defines button size and location in the PyGame window
button1 = Button(10, 10, 100, 50, 'One')
button2 = Button(10, 70, 100, 50, 'Two')
button3 = Button(10, 130, 100, 50, 'Three')

# game loop
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    button1.draw()
    button2.draw()
    button3.draw()
    pygame.display.update()