首页 > 解决方案 > 带有圆角的 Pygame 按钮,border_radius 参数不起作用

问题描述

我目前正在学习pygame。在对圆角矩形进行编程时,我遇到了一个问题。

pygame.draw.rect() 画一个矩形 rect(surface, color, rect) -> Rect rect(surface, color, rect, width=0,border_radius=0,border_radius=-1,border_top_left_radius=-1,border_top_right_radius=- 1、border_bottom_left_radius=-1) -> Rect 在给定的表面上绘制一个矩形。

border_radius (int) -- (可选)用于绘制圆角矩形。支持的范围是 [0, min(height, width) / 2],其中 0 表示没有圆角的矩形。

这是 pygame 官方文档的描述。

username_rect = pg.rect.Rect(screenwidth/2 - 1/8*screenwidth, screenheight*1/5, 1/4*screenwidth, username_title_render.get_height() + 10)
pg.draw.rect(screen, (0,0,0), username_rect,border_radius = 15)

这里是主要部分。但随后我收到此错误消息:

TypeError: 'border_radius' is an invalid keyword argument for this function

我已经尝试添加一个“width=2”参数,但它说参数太多。不幸的是,取其他数字也无济于事。我目前正在使用 pygame 版本 2.0.0.dev6(SDL 2.0.10,python 3.8.1)

如果您能帮助我,我将非常高兴,非常感谢您!

标签: pythonpygamerounded-cornersrect

解决方案


该文档适用于 2.0.0.dev7,根据此 PRborder_radius是在 2.0.0.dev6 之后添加的,GitHub 参考发布。因此,您正在尝试使用尚未出现的东西。您可以尝试从源代码构建并使用关键字(尽管稳定性不是承诺)。或者你可以尝试使用矩形和圆形的组合来达到效果。


推荐阅读