pygame做界面之文本框

效果图

在这里插入图片描述

主要代码

bf_label.py

# -*- coding=utf-8 -*-
import threading
import pygame
from pygame.locals import MOUSEBUTTONDOWN
from bf_common import BFControlId,BFBase,DEFAULT_FONT,TEXT_ALIGN_LEFT,TEXT_ALIGN_MIDDLE,TEXT_ALIGN_RIGHT

CLICK_EFFECT_TIME = 100
PADDING = 4
class BFLabel(BFBase):
    def __init__(self, parent, rect, text='Label', click=None):
        super().__init__()
        self.x,self.y,self.width,self.height = rect
        self.bg_color = (225,225,225)
        self.parent = parent
        self.surface = parent.subsurface(rect)
        self.is_hover = False
        self.in_click = False
        self.click_loss_time = 0
        self.click_event_id = -1
        self.ctl_id = BFControlId().instance().get_new_id()
        self._text = text
        self._click = click
        self.init_font()

    def init_font(self):
        font = DEFAULT_FONT
        white = 100, 100, 100
        self.textImage = font.render(self._text, True, white)
        w, h = self.textImage.get_size()
        self._ty = (self.height - h) / 2
        if self._text_align == TEXT_ALIGN_LEFT:
            self._tx = PADDING
        elif self._text_align == TEXT_ALIGN_MIDDLE:
            self._tx = (self.width - PADDING * 2 - w) / 2
        else:
            self._tx = (self.width - PADDING - w) 

    @property
    def text(self):
        return self._text

    @text.setter
    def text(self, value):
        self._text = value
        self.init_font()

    @property
    def click(self):
        return self._click

    @click.setter
    def click(self, value):
        self._click = value

    def clear_hover(self):
        self.is_hover = False

    def update(self, event):
        if self.in_click and event.type == pygame.USEREVENT+1 and BFControlId().instance().click_id == self.ctl_id:
            if self._click: self._click(self)
            self.click_event_id = -1
            return True

        x, y = pygame.mouse.get_pos()
        if x > self.x and x < self.x + self.width and y > self.y and y < self.y + self.height:
            if self.panel: self.panel.clear_hover()
            self.is_hover = True
            if event.type == MOUSEBUTTONDOWN:
                pressed_array = pygame.mouse.get_pressed()
                if pressed_array[0]:
                    self.in_click = True
                    if self.panel: self.panel.clear_foucs()
                    self.click_loss_time = pygame.time.get_ticks() + CLICK_EFFECT_TIME
                    BFControlId().instance().click_id = self.ctl_id
                    pygame.time.set_timer(pygame.USEREVENT+1,CLICK_EFFECT_TIME-10)
            return True
        else:
            self.is_hover = False
        return False

    def draw(self):
        if self.in_click:
            if self.click_loss_time < pygame.time.get_ticks():
                self.in_click = False
        if not self._visible:
            return

        self.surface.blit(self.textImage, (self._tx, self._ty))

使用方式

bf_lebel = BFLabel(screen, (20,80,160,40),text='文字显示')
bf_lebel.text_align = TEXT_ALIGN_RIGHT
btn_panel.add_control(bf_lebel)

其他完整的代码已经上传github
https://github.com/zhangenter/bf_control

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰风漫天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值