用于Pygame的小部件

pygame-widgets的Python项目详细描述


Pygame小部件

一个帮助模块,用于使用Pygame开发应用程序时可能需要的通用小部件。 它支持完全自定义的按钮、按钮集合、文本框和滑块。 如果您希望看到添加的任何小工具,请创建一个问题!在

新功能

  • 动画:创建一个动画,在一段时间内更改小部件属性,运行在一个单独的线程上

先决条件

安装

确保安装了python3和pip并将其添加到环境路径中。在

python -m pip install pygame-widgets

打开Python控制台并运行以下命令。在

import pygame_widgets

如果没有收到错误,则安装成功。在

使用

普通

所有小部件通用的功能

强制参数

注意:必须按顺序提供强制参数。

ParameterDescriptionType
winSurface to be displayed on.pygame.Surface
xX-coordinate of top left.int
yY-coordinate of top left.int
widthWidth of button in pixels.int
heightHeight 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强制性参数

注意:必须按顺序提供强制参数。

ParameterDescriptionType
shapeNumber of columns and rows of buttons (columns, rows).(int, int)

可选参数

注意:ButtonArray的可选参数与Button的相似。

ParameterDescriptionTypeDefault
colourBackground colour of array.(int, int, int)(210, 210, 180)
borderThickness between buttons and between the edges of array and buttons.int10
topBorderThickness between top of array and top of button. Overrides border.intborder
bottomBorderThickness between bottom of array and bottom of button. Overrides border.intborder
leftBorderThickness between left of array and left of button. Overrides border.intborder
rightBorderThickness between right of array and right of button. Overrides border.intborder
separationThicknessThickness between buttons. Overrides border.intborder

文本框

文本输入或显示的框

示例用法

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()

可选参数

ParameterDescriptionTypeDefault
colourBackground colour.(int, int, int)(220, 220, 220)
textColourColour of text.(int, int, int)(0, 0, 0)
borderColourColour of border.(int, int, int)(0, 0, 0)
borderThicknessThickness of border.int3
radiusBorder radius. Set to 0 for no radius.int0
onSubmitFunction to be called when return / enter is pressed.functionNone
onSubmitParamsParameters to be fed into onSubmit function.(*any)()
placeholderTextText to be displayed when empty.str''
fontSizeSize of text.int20
fontFont of text.pygame.font.FontCalibri

滑块

用于离散数值选择的滑块

示例用法

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方法。在

可选参数

ParameterDescriptionTypeDefault
minMinimum value of the slider (left).int or float0
maxMaximum value of the slider (right).int or float99
stepValue to increment by.int or float1
colourColour of slider.(int, int, int)(200, 200, 200)
handleColourColour of handle.(int, int, int)(0, 0, 0)
initialInitial value of the slider.int or floatAverage of min and max
handleRadiusRadius of handle.intheight / 1.3
curvedAdd curved ends to the slider.boolTrue

动画

使用默认的“平移”或“调整大小”创建动画, 从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。因为它是在单独的线程上执行的, 该按钮在动画期间仍然可以工作。在

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
ws-consumer服务地址中的java动态属性   java如何比较整数列表,然后按升序排序?   javascript我正在使用java脚本调用一个函数,但它没有调用代码下面的方法,也没有调用secretitnames()函数   在文本窗格中多次使用Java insertIcon图标   JavaMSAL安卓。AuthenticationActivity完成,但用于身份验证请求的线程池线程仍处于等待状态   if语句中的java多范围比较   java toString()表示输出   java如何在jcstrest测试中生成指令重新排序   java我怎样才能运行它?   web应用程序中使用Hibernate和Spring的java问题   如何将字符串数据写入Java文本文件   如何在java命令提示符下运行已签名的jar文件?   java从我的菜单调用RCP应用程序   java如何等待Canvas/GraphicsContext完成任务,然后再继续执行代码块?