用于Pygame的小部件
pygame-widgets的Python项目详细描述
Pygame小部件
一个帮助模块,用于使用Pygame开发应用程序时可能需要的通用小部件。 它支持完全自定义的按钮、按钮集合、文本框和滑块。 如果您希望看到添加的任何小工具,请创建一个问题!在
新功能
- 动画:创建一个动画,在一段时间内更改小部件属性,运行在一个单独的线程上
先决条件
安装
确保安装了python3和pip并将其添加到环境路径中。在
python -m pip install pygame-widgets
打开Python控制台并运行以下命令。在
import pygame_widgets
如果没有收到错误,则安装成功。在
使用
普通
所有小部件通用的功能
强制参数
注意:必须按顺序提供强制参数。
Parameter | Description | Type |
---|---|---|
win | Surface to be displayed on. | pygame.Surface |
x | X-coordinate of top left. | int |
y | Y-coordinate of top left. | int |
width | Width of button in pixels. | int |
height | Height of button in pixels. | int |
按钮
可自定义按钮
示例用法
import pygame
from pygame_widgets import Button
pygame.init()
win = pygame.display.set_mode((600, 600))
button = Button(
win, 100, 100, 300, 150, text='Hello',
fontSize=50, margin=20,
inactiveColour=(255, 0, 0),
pressedColour=(0, 255, 0), radius=20,
onClick=lambda: print('Click')
)
run = True
while run:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
run = False
quit()
win.fill((255, 255, 255))
button.listen(events)
button.draw()
pygame.display.update()
此按钮将放置在(100,100)处,宽度为300,高度为150, 显示字体大小为50的文本“Hello”,留出20的边距和20的半径。 单击时,按钮将从红色变为绿色,并将“单击”打印到控制台。在
可选参数
^{tb2}$按钮雷
类似按钮的集合
示例用法
^{pr2}$h4强制性参数
注意:必须按顺序提供强制参数。
Parameter | Description | Type |
---|---|---|
shape | Number of columns and rows of buttons (columns, rows). | (int, int) |
可选参数
注意:ButtonArray的可选参数与Button的相似。
Parameter | Description | Type | Default |
---|---|---|---|
colour | Background colour of array. | (int, int, int) | (210, 210, 180) |
border | Thickness between buttons and between the edges of array and buttons. | int | 10 |
topBorder | Thickness between top of array and top of button. Overrides border. | int | border |
bottomBorder | Thickness between bottom of array and bottom of button. Overrides border. | int | border |
leftBorder | Thickness between left of array and left of button. Overrides border. | int | border |
rightBorder | Thickness between right of array and right of button. Overrides border. | int | border |
separationThickness | Thickness between buttons. Overrides border. | int | border |
文本框
文本输入或显示的框
示例用法
import pygame
from pygame_widgets import TextBox
def output():
# Get text in the textbox
print(textbox.getText())
pygame.init()
win = pygame.display.set_mode((1000, 600))
textbox = TextBox(win, 100, 100, 800, 80, fontSize=50,
borderColour=(255, 0, 0), textColour=(0, 200, 0),
onSubmit=output, radius=10, borderThickness=5)
run = True
while run:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
run = False
quit()
win.fill((255, 255, 255))
textbox.listen(events)
textbox.draw()
pygame.display.update()
可选参数
Parameter | Description | Type | Default |
---|---|---|---|
colour | Background colour. | (int, int, int) | (220, 220, 220) |
textColour | Colour of text. | (int, int, int) | (0, 0, 0) |
borderColour | Colour of border. | (int, int, int) | (0, 0, 0) |
borderThickness | Thickness of border. | int | 3 |
radius | Border radius. Set to 0 for no radius. | int | 0 |
onSubmit | Function to be called when return / enter is pressed. | function | None |
onSubmitParams | Parameters to be fed into onSubmit function. | (*any) | () |
placeholderText | Text to be displayed when empty. | str | '' |
fontSize | Size of text. | int | 20 |
font | Font of text. | pygame.font.Font | Calibri |
滑块
用于离散数值选择的滑块
示例用法
pygame.init()
win = pygame.display.set_mode((1000, 600))
slider = Slider(win, 100, 100, 800, 40, min=0, max=99, step=1)
output = TextBox(win, 475, 200, 50, 50, fontSize=30)
run = True
while run:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
run = False
quit()
win.fill((255, 255, 255))
slider.listen(events)
slider.draw()
output.setText(slider.getValue())
output.draw()
pygame.display.update()
如您所见,TextBox也可用于显示文本, 不调用它的listen方法。在
可选参数
Parameter | Description | Type | Default |
---|---|---|---|
min | Minimum value of the slider (left). | int or float | 0 |
max | Maximum value of the slider (right). | int or float | 99 |
step | Value to increment by. | int or float | 1 |
colour | Colour of slider. | (int, int, int) | (200, 200, 200) |
handleColour | Colour of handle. | (int, int, int) | (0, 0, 0) |
initial | Initial value of the slider. | int or float | Average of min and max |
handleRadius | Radius of handle. | int | height / 1.3 |
curved | Add curved ends to the slider. | bool | True |
动画
使用默认的“平移”或“调整大小”创建动画, 从AnimationBase继承,或直接使用AnimationBase。在
示例用法
pygame.init()
win = pygame.display.set_mode((600, 600))
button = Button(win, 100, 100, 300, 150)
animation = Resize(button, 3, 200, 200)
animation.start()
run = True
while run:
events = pygame.event.get()
for event in events:
if event.type == pygame.QUIT:
pygame.quit()
run = False
quit()
win.fill((255, 255, 255))
button.listen(events)
button.draw()
pygame.display.update()
3秒钟后,按钮的宽度从300变为200 它的高度从150到200。因为它是在单独的线程上执行的, 该按钮在动画期间仍然可以工作。在
- 项目
标签: